我如何从ListActivity获取onListItemClick中的控件

时间:2011-07-15 22:54:40

标签: android android-listview

我在Android Project中有ListActivity。它的代码:

public class ListActivityApp extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list); 
    ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;

    map = new HashMap<String, String>();        
    map.put("title", "my note");  
    map.put("img", String.valueOf(R.drawable.paper));
    listItem.add(map);

    map = new HashMap<String, String>();        
    map.put("title", "second");  
    map.put("img", String.valueOf(R.drawable.paper));
    listItem.add(map);

    map = new HashMap<String, String>();        
    map.put("title", "cos tam");  
    map.put("img", String.valueOf(R.drawable.paper));
    listItem.add(map);

    SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list,
            new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});
    setListAdapter(myAdapter);       
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String selection = getListAdapter().getItem(position).toString();
    Toast.makeText(this, selection, 0).show();
}}

我的list.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView  
android:id="@android:id/list"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"    
/>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:text="Empty"   
/>

我的item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> 
<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    />
 <TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    />
</LinearLayout>

我遇到的第一个问题(已解决):在模拟器上我可以看到三个文本为“空”的TextView,但是当我点击它们时,我可以看到Toast的正确“标题”。我做错了什么?在另一个项目中它正常工作。 - 我已经解决了这个问题。在这:

SimpleAdapter myAdapter = new SimpleAdapter (this.getBaseContext(), listItem,       R.layout.list, new String[] {"img", "title"}, new int[] {R.id.img, R.id.title});

我应该作为第三个参数使用R.layout.item,而不是R.layout.list。

第二个问题:如何在onListItemClick(..)方法中获取我的列表项的Object?我当然有位置和身份。 l.getItemAtPosition(位置) getListAdapter()。的getItem(位置) 这些方法返回所有项目(在我的情况下是img和title)。我怎样才能更改TextView中的文本或在onListItemClick(..)中设置我的ImageView不可见?

1 个答案:

答案 0 :(得分:0)

要从点击的项目中获取textview和imageview,请使用onClick()事件中给出的View,v并执行以下操作:

TextView tv = (TextView)v.findViewById(R.id.textview);
tv.setText("example text");
ImageView image = (ImageView)v.findViewById(R.id.image);
image.setVisibility(ImageView.INVISIBLE);