2013-05-31 58 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); 

    } 
});