2014-10-31 64 views
-1

我必须使列表视图项可点击即开始一个活动。我正在使用下面的代码。我不知道如何继续。如何使列表视图项可点击?

package org.example.androidsdk.demo; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 

public class MainActivity extends ListActivity { 

    String [] mTestArray; 

    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     // Create an ArrayAdapter that will contain all list items 
     ArrayAdapter<String> adapter; 

     mTestArray = getResources().getStringArray(R.array.myArray); 

     /* 
     * Assign the name array to that adapter and also choose a simple layout 
     * for the list items 
     */ 
     adapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      mTestArray); 

     // Assign the adapter to this ListActivity 
     setListAdapter(adapter); 
    } 
} 
+0

http://syedasaraahmed.wordpress.com/2013/02/08/make-a-custom-listview-row-与点击的-按钮功能于使用-A-定制的CursorAdapter它可选择-/查看此链接 – 2014-10-31 06:38:44

+0

[http://www.vogella.com/tutorials/AndroidListView/article.html](http://www。 vogella.com/tutorials/AndroidListView/article.html) – 2014-10-31 06:39:19

+0

我告诉你请通过http://stackoverflow.com/help/how-to-ask因为如果你谷歌有点之前问你可能会得到你的答案http: //stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android& http://stackoverflow.com/questions/2468100/android-listview-click-howto http://stackoverflow.com/questions/6188873/event-click-on-listview-extends-listactivity与许多 – MOSO 2014-10-31 06:41:54

回答

0

覆盖onListItemClick,并把它写你的活动开放代码。

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     Intent i = new Intent(this,MyClass.class); 
     startActivity(i); 
    } 
1

只重写onListItemClickActivity

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     String item = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    } 
0

您需要实现OnItemClickListener

并调用getListView().setOnItemClickListener(this)

最后,你想什么overrided onItemClick方法。

+0

如何实现OnItemClickListener – yogmis 2014-10-31 07:42:22

+0

@yogmis嗯...加油的人,谷歌搜索吧...“myActivity扩展活动实现OnItemClickListener” – 2014-10-31 07:47:43

0

尝试下面的代码: -

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG) 
     .show(); 
    } 
}); 

阅读下面的链接了解更多信息: -

http://www.vogella.com/tutorials/AndroidListView/article.html

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

+0

他正在扩展ListActivity类 – Amy 2014-10-31 06:40:03

+0

感谢您的帮助 – yogmis 2014-10-31 06:49:52

相关问题