2013-05-31 69 views
2

比方說,我有一個ListView與一些項目。我想這樣做,當用戶點擊一個項目,應用程序彈出一個Toast包含項目的名稱。例如,當用戶點擊「蘋果」時,他們會看到敬酒:「你吃蘋果。」我該怎麼做?Android ListView - 如何應對物品點擊?

回答

0

您可以設置項目點擊監聽器的列表視圖即

listvw=(ListView)findViewById(R.id.listviewid); 

listvw.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 

      Toast.makeText(ActivityName.this, "Text message", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
+0

同樣在這裏。如何獲得按下的項目的名稱? – Arielle

+0

你有你的列表視圖項目的視圖。只需找到視圖的文本即'TextView text =(TextView)view.findViewById(R.id.text_viewId); ' 'Toast.makeText(ActivityName.this,text.getText(),Toast.LENGTH_SHORT).show();' –

+0

好的,謝謝。我用'TextView text =(TextView)arg1.findViewById(arg1.getId()); Toast.makeText(getApplicationContext(),text.getText(),Toast.LENGTH.SHORT).show();' – Arielle

0

使用這樣的

ActivityListView的.java

package com.sunil; 

    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.Toast; 

    public class ActivityListView extends ListActivity { 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Create an array of Strings, that will be put to our ListActivity 
    String[] namesArray = new String[] { "Linux", "Windows7", "Eclipse", 
     "Suse", "Ubuntu", "Solaris", "Android", "iPhone" }; 

    /* Create an ArrayAdapter, that will actually make the Strings above 
     appear in the ListView */ 

    this.setListAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, namesArray)); 
    } 
     @Override 
     protected void onListItemClick(ListView l, View v, 
     int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    // Get the item that was clicked 

    Object o = this.getListAdapter().getItem(position); 
    String keyword = o.toString(); 
    Toast.makeText(this, "You selected: " + keyword, 
     Toast.LENGTH_SHORT).show(); 
    } 
    } 

main.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"> 

    <TextView 

     android:layout_width="fill_parent" 

     android:layout_height="wrap_content" 

     android:text="@string/hello" /> 

    </LinearLayout> 
+0

我得到一個'java.lang.RuntimeException:你的內容必須有一個ListView,它的id屬性是'android.R.id.list''。也許讓我們擴展標準的'Activity'類,而不是'ListActivity'? – Arielle

+0

在我的設備中工作的代碼相同。不要擴展Activity,也不要setcontentview。它會再次正常工作檢查 –

+0

也許我的設備與此代碼不兼容? – Arielle

0

試試這個代碼 listview =(ListView)findViewById(R.id.listid);

listview.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) { 

     // to set your list item or name 

     //here list is your list that you set in your adapter  
      list.get(pos); 

    } 
});