2011-07-15 24 views
1

我在Android項目中有ListActivity。其代碼:我怎麼能從ListActivity獲取onListItemClick中的控件

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> 

的第一個問題(解決)我有:在模擬器我可以看到三個帶有文本「Empty」的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(..)方法中我的列表項的對象?我當然有立場和身份證。 l.getItemAtPosition(position) getListAdapter()。getItem(position) 這些方法返回所有項目(在我的情況下img和標題)。我怎麼可以例如更改TextView中的文本或設置我的ImageView在onListItemClick(..)中不可見?

+1

我可以從TextView中獲取文本在此方法:'HashMap map = new HashMap (); \t \t map =(HashMap )getListAdapter()。getItem(position); String temp =(String)map.get(「title」);'但我不能這樣做:'TextView tv =(TextView)map.get(「title」);'改變文字。我如何從ListActivity中修改ListView中的現有控件? – woyaru

回答

0

您可以通過點擊項目的TextView和ImageView的,使用在onclick()事件的視圖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); 
相關問題