Java Android-链接到网站的自定义列表视图
因此,我创建了一个自定义ListView,列表中有两个对象。例如,名单上的第一个是猫,第二个是狗。然后,当点击其中一个时,它将导航到另一个活动屏幕。例如,当用户点击“Dogs”时,它会进入另一个屏幕,屏幕上会出现一个新的列表,列表上有“Corgi”、“Pug”、“Husky”等名称。然后,当点击一个犬种时,它会转发到一个网站。例如,当用户点击“Corgi”时,它会将用户转发到Corgi的Wiki页面 我将如何创建应用程序的第三部分 我的代码如下: main活动:Java Android-链接到网站的自定义列表视图,java,android,Java,Android,因此,我创建了一个自定义ListView,列表中有两个对象。例如,名单上的第一个是猫,第二个是狗。然后,当点击其中一个时,它将导航到另一个活动屏幕。例如,当用户点击“Dogs”时,它会进入另一个屏幕,屏幕上会出现一个新的列表,列表上有“Corgi”、“Pug”、“Husky”等名称。然后,当点击一个犬种时,它会转发到一个网站。例如,当用户点击“Corgi”时,它会将用户转发到Corgi的Wiki页面 我将如何创建应用程序的第三部分 我的代码如下: main活动: public class
public class MainActivity extends ListActivity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] data = { "Dogs", "Cats" };
int[] icons = { R.drawable.dogz, R.drawable.catz };
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data, icons));
/* setOnItemClickListener() Register a callback to be invoked when an item
* in this AdapterView has been clicked.
*/
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(), ChildActivity.class);
// Add extended data to the intent.
intent.putExtra("POSITION", position);
/*
* Launch a new activity. You will not receive any information about when
* the activity exits. This implementation overrides the base version,
* providing information about the activity performing the launch.
*/
startActivity(intent);
}
}
public class ChildActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[][] data = {
{ "Corgi", "Pugs", "Husky" },
{ "Siamese", "Persian", "Maine Coon" } };
int[][] icons = {
{ R.drawable.d, R.drawable.e, R.drawable.f },
{ R.drawable.a, R.drawable.b, R.drawable.c }, };
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data[position],
icons[position]));
}
}
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final int[] icons;
public CustomListAdapter(Context context, String[] values, int[] icons) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
this.icons = icons;
}
@Override
// Get a View that displays the data at the specified position in the data set.
public View getView(int position, View convertView, ViewGroup parent) {
/*
* Instantiates a layout XML file into its corresponding View objects.
* It is never used directly. Instead, use getSystemService(String) to
* retrieve a standard LayoutInflater instance that is already hooked up to
* the current context and correctly configured for the device you are running on.
*/
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*
* Inflate a new view hierarchy from the specified xml resource.
* Throws InflateException if there is an error.
*/
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtStatus);
textView.setText(values[position]);
Drawable draw = context.getResources().getDrawable(icons[position]);
Bitmap bitmap = ((BitmapDrawable) draw).getBitmap();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
Drawable newDraw = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bitmap, 40 * w / h, 40, true));
/*
* Sets the Drawables (if any) to appear to the left of, above, to the right of,
* and below the text. Use 0 if you do not want a Drawable there.
* The Drawables' bounds will be set to their intrinsic bounds.
*/
textView.setCompoundDrawablesWithIntrinsicBounds(newDraw, null, null,
null);
return rowView;
}
}
CustomListAdapter:
public class MainActivity extends ListActivity implements OnItemClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] data = { "Dogs", "Cats" };
int[] icons = { R.drawable.dogz, R.drawable.catz };
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data, icons));
/* setOnItemClickListener() Register a callback to be invoked when an item
* in this AdapterView has been clicked.
*/
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(), ChildActivity.class);
// Add extended data to the intent.
intent.putExtra("POSITION", position);
/*
* Launch a new activity. You will not receive any information about when
* the activity exits. This implementation overrides the base version,
* providing information about the activity performing the launch.
*/
startActivity(intent);
}
}
public class ChildActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[][] data = {
{ "Corgi", "Pugs", "Husky" },
{ "Siamese", "Persian", "Maine Coon" } };
int[][] icons = {
{ R.drawable.d, R.drawable.e, R.drawable.f },
{ R.drawable.a, R.drawable.b, R.drawable.c }, };
Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);
// Provide the cursor for the list view.
setListAdapter(new CustomListAdapter(this, data[position],
icons[position]));
}
}
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private final int[] icons;
public CustomListAdapter(Context context, String[] values, int[] icons) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
this.icons = icons;
}
@Override
// Get a View that displays the data at the specified position in the data set.
public View getView(int position, View convertView, ViewGroup parent) {
/*
* Instantiates a layout XML file into its corresponding View objects.
* It is never used directly. Instead, use getSystemService(String) to
* retrieve a standard LayoutInflater instance that is already hooked up to
* the current context and correctly configured for the device you are running on.
*/
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*
* Inflate a new view hierarchy from the specified xml resource.
* Throws InflateException if there is an error.
*/
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txtStatus);
textView.setText(values[position]);
Drawable draw = context.getResources().getDrawable(icons[position]);
Bitmap bitmap = ((BitmapDrawable) draw).getBitmap();
int h = bitmap.getHeight();
int w = bitmap.getWidth();
Drawable newDraw = new BitmapDrawable(context.getResources(),
Bitmap.createScaledBitmap(bitmap, 40 * w / h, 40, true));
/*
* Sets the Drawables (if any) to appear to the left of, above, to the right of,
* and below the text. Use 0 if you do not want a Drawable there.
* The Drawables' bounds will be set to their intrinsic bounds.
*/
textView.setCompoundDrawablesWithIntrinsicBounds(newDraw, null, null,
null);
return rowView;
}
}
公共类CustomListAdapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
私有最终int[]图标;
公共CustomListAdapter(上下文上下文、字符串[]值、int[]图标){
super(上下文、R.layout.row\u布局、值);
this.context=上下文;
这个值=值;
这个图标=图标;
}
@凌驾
//获取在数据集中指定位置显示数据的视图。
公共视图getView(int位置、视图转换视图、视图组父视图){
/*
*将布局XML文件实例化为其相应的视图对象。
*它从不直接使用。相反,请使用getSystemService(字符串)来
*检索已连接到的标准LayoutFlater实例
*为正在运行的设备正确配置了当前上下文和。
*/
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
/*
*从指定的xml资源展开新的视图层次结构。
*如果出现错误,则抛出InflateException。
*/
视图行视图=充气机。充气(R.layout.row\u布局,父级,false);
TextView TextView=(TextView)rowView.findViewById(R.id.txtStatus);
setText(值[位置]);
Drawable draw=context.getResources().getDrawable(图标[位置]);
位图位图=((BitmapDrawable)draw.getBitmap();
int h=bitmap.getHeight();
int w=bitmap.getWidth();
Drawable newDraw=新的BitmapDrawable(context.getResources(),
创建缩放位图(位图,40*w/h,40,真);
/*
*将可绘图项(如果有)设置为显示在的左侧、上方和右侧,
*和文本下方。如果不希望在此处绘制图形,请使用0。
*可绘制边界将设置为其固有边界。
*/
textView.setCompoundDrawableSwithinInstincBounds(newDraw,null,null,
无效);
返回行视图;
}
}
我知道我必须创建第三个活动,但如果不使用.xml,我不知道如何进行。先谢谢你
Animal
项目制作适配器李>
OnItemClick()
打开一个,并将您的Animal
对象或Animal.getUrl()
-字符串传递给它Animal
项目制作适配器李>
OnItemClick()
打开一个,并将您的Animal
对象或Animal.getUrl()
-字符串传递给它我不会添加新的活动-如果我忽略第三个活动,并添加“browserIntent”代码,请查看启动intent:@russianmario。如何使每个url对每个品种都有特殊性?与其创建字符串数组来列出每个品种,您可能需要创建一个包含品种名称和要访问的url的类品种,然后创建一个列表。您可以继续使用ArrayAdapter(对使用繁殖对象进行一些修改)。然后使用
onItemClick
事件:@russianmario,我肯定要保留字符串数组。但是,与其用url,不如只写一段关于品种的内容?“狗”->“科基”->有关该品种的详细信息Corgi@russianmario谢谢我会在有机会的时候实施你的建议,并会给你一个更新。我不会添加新的活动-如果我忽略第三个活动,并添加“browserIntent”代码,请查看启动意图:@russianmario。如何使每个url对每个品种都有特殊性?与其创建字符串数组来列出每个品种,您可能需要创建一个包含品种名称和要访问的url的类品种,然后创建一个列表。您可以继续使用ArrayAdapter(对使用繁殖对象进行一些修改)。然后使用onItemClick
事件:@russianmario,我肯定要保留字符串数组。但是,与其用url,不如只写一段关于品种的内容?“狗”->“科基”->有关该品种的详细信息Corgi@russianmario谢谢我会在有机会的时候实施你的建议,并会给你更新。