首先,我是android编程的初学者。所以不要太苛刻:P
无论如何,我有一个回收 ListView,每个列表项包含一个图像和一个文本。假设有 100 个不同的项目。单击的每个项目都会将我带到带有“detail_layout.xml”的活动“DetailActivity.class”。现在我想根据单击的项目配置此布局。
测试活动.java:
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i <= 99; i++) {
list.add(detail_array[i]);
//detail_array[] contains Strings which are used to add Items to the list.
}
private ListView myList = (ListView)findViewById(R.id.list);
myList.setAdapter(new MyCustomAdapter(TestActivity.this,list));
MyCustomAdapter.java:
public class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mListItemsTV;
private LayoutInflater mLayoutInflater;
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = mLayoutInflater.inflate(R.layout.list_item, null);
holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
String stringItem = mListItemsTV.get(position);
if (stringItem != null) {
if (holder.itemName != null) {
holder.itemName.setText(stringItem);
ImageView imageView = (ImageView) view.findViewById(R.id.list_item_image_view);
if(holder.itemName.getText() == "SomeName") {
imageView.setImageResource(R.drawable.somedrawable);
}
return view;
}
老实说,我从某种教程中得到了这些,我想我知道那里发生了什么^^ 现在,我怎样才能设法获得点击了哪个项目?我用“setOnClickListener”试了几次,但似乎从来没有用过。我该如何使用它?我应该使用这个吗?我必须在哪里使用它?