我知道有很多关于使用onItemClickListener和列表视图的信息,但我是Android开发的新手,似乎无法让它工作。
我不太确定应该在哪里添加听众,所以我真的很感激一些帮助和指导。
我有两个文件,主要活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
和 ListAdapter文件:
public class GroceryListAdapter extends ArrayAdapter<GroceryList> {
private ArrayList<GroceryList> grocerylists;
private Activity activity;
public ImageManager imageManager;
public GroceryListAdapter(Activity a, int textViewResourceId, ArrayList<GroceryList> grocerylists) {
super(a, textViewResourceId, grocerylists);
this.grocerylists = grocerylists;
activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView message;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.categorymenu, null);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.categoryname);
holder.message = (TextView) v.findViewById(R.id.message);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final GroceryList grocerylist = grocerylists.get(position);
if (alcohollist != null) {
holder.name.setText(grocerylist.name);
holder.message.setText(grocerylist.message);
}
return v;
}
如果我问一个已经回答的问题,我很抱歉,但我花了很多时间试图为自己解决这个问题,但没有成功。
我希望有一个比我更有经验的人能够告诉我应该在哪里以及如何添加onItemClickListen方法。
谢谢!
答案 0 :(得分:1)
从外观上看,你使用的是常规活动,所以你应该添加:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
Toast.LENGTH_SHORT).show();
}
});
之前主要活动中的.setAdapter
。这应该有用。
答案 1 :(得分:0)
在主要活动
中listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
// startActivity( new Intent());
Intent i = new Intent(this,"Next_Activity_Name".class);
i.putExtra("selected",(int)selected_position);
final int resultCode = 2;
startActivityForResult(i,resultCode);
}
您必须在onCreate()方法之后将这段代码添加到主活动中。
答案 3 :(得分:0)
您已使用自定义适配器,因此您可以在touchListner
布局中定义的特定小部件中设置categorymenu
。这样做很容易,但是
如果您使用简单的列表视图,那么
在listview中添加参数:
lview.setOnItemClickListener(this);
public void onListItemClick(ListView parent, View v, int position, long id) {
// do with list-view item Position
}
简单列表视图
http://www.androidpeople.com/android-listview-onclick
http://developer.android.com/resources/tutorials/views/hello-listview.html
http://mobile.tutsplus.com/tutorials/android/android-listview/
Paresh Mayani
Custome Listview
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
how to customize listview row android
http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/
答案 4 :(得分:0)
设置适配器后记下以下代码。
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}});
在onItemClick方法下处理您想要执行的功能。
答案 5 :(得分:0)
这里有很多答案,但似乎你并不明白他们在说什么。我也会尝试回答。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// do whatever you want on clicking any list itm
}
});
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
我希望你能理解它。