如何通过单击其中的按钮获取列表项位置?

实际上我已经读过一些关于此的问题…

这是我使用的代码

auto = (ListView)findViewById(R.id.auto); String[] projection = new String[] {Browser.BookmarkColumns._ID,Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL}; String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL}; int[] displayViews = new int[] { R.id.text1,R.id.text2 }; Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null); //auto.setAdapter(new SimpleCursorAdapter(this, R.layout.mylist, cur,displayFields, displayViews)); myAdapter apt = new myAdapter(this, R.layout.mylist, cur,displayFields, displayViews); auto.setAdapter(apt); 

和myAdapter类

 class myAdapter extends SimpleCursorAdapter{ private Cursor c; private Context context; public myAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); // TODO Auto-generated constructor stub this.c = c; this.context = context; AutoList att = new AutoList(); mListView = att.auto; } @Override public View getView(int pos, View inView, ViewGroup parent) { View vix = inView; if (vix == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); vix = inflater.inflate(R.layout.mylist, null); } this.c.moveToPosition(pos); String title = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE)); String cont = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.URL)); TextView text1 = (TextView) vix.findViewById(R.id.text1); text1.setText(title); TextView text2 = (TextView) vix.findViewById(R.id.text2); text2.setText(cont); Button butt = (Button) vix.findViewById(R.id.button); butt.setOnClickListener(mButt); return vix; } private OnClickListener mButt = new OnClickListener() { @Override public void onClick(View v) { final int position = mListView.getPositionForView((View) v.getParent()); Log.v("BUTT", "Title clicked, row "+position); } }; 

但是,当我点击按钮时,我仍然会收到很多这样的错误:

 04-10 22:30:55.152: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main 04-10 22:30:55.152: ERROR/AndroidRuntime(695): java.lang.NullPointerException 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.auto2.AutoList$myAdapter$1.onClick(AutoList.java:113) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View.performClick(View.java:2408) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View$PerformClick.run(View.java:8816) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.handleCallback(Handler.java:587) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.dispatchMessage(Handler.java:92) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Looper.loop(Looper.java:123) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invokeNative(Native Method) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invoke(Method.java:521) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-10 22:30:55.152: ERROR/AndroidRuntime(695): at dalvik.system.NativeStart.main(Native Method) 

而已! 我希望不会被弄清楚这么难!

谢谢!

  • Android:删除数据库行后如何重新查询游标刷新ListView?
  • 修改SimpleCursorAdapter的数据
  • 使用Cursor Adapter实现带多个select器的listview
  • Android ListView刷新
  • Android:在自定义SimpleCursorAdapter中使用newView和bindView问题
  • 修改SimpleCursorAdapter的数据
  • 在ListView中保持滚动位置的同时更新SimpleCursorAdapter
  • 为什么cursorLoader没有通知源数据的变化?
  • 实际上,我使用了相同的方法 – 只是添加了转换父布局,我得到了没有任何exception的位置

     public void deleteButtonClick(View v) { //TODO Remove favorite - DB + file system Toast.makeText(this, "Deleting bookmark", Toast.LENGTH_SHORT).show(); final int position = getListView().getPositionForView((LinearLayout)v.getParent()); if (position >= 0) { Favorite o = (Favorite) this.getListAdapter().getItem(position); } } 

    我对listview行的布局:

     < ?xml version="1.0" encoding="utf-8"?>      

    我希望它会有所帮助。

    一个更简单的解决方案可能是当您的Activity实现OnClickListener并将(已转换的)Context作为OnClickListener设置为您在Adapter中所需的任何视图时。 有关更强大的代码,您可以使用instanceof进行检查。

     public class MyActivity implements OnClickListener { // ... } public class MyAdapter extends CursorAdapter { @Override public void bindView(View view, Context context, Cursor cursor) { final TextView tv = (View) view.findViewById(R.id.text1); setOnClickListener((OnClickListener)context); tv.setTag(cursor.getPosition()); } } 

    为方便起见,您可以将适配器中的项目位置设置为视图的标记。 这样您就可以轻松地在适配器中查找整个项目。

    我们可以通过在适配器的getView方法中实现onClickListener来获得该位置。

    适配器:

     public class DetailsListAdapter extends BaseAdapter { Context context; ArrayList data; public DetailsListAdapter(Context context, ArrayList data) { this.context = context; this.data = data; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item_logs_details, null); viewHolder = new ViewHolder(); viewHolder.btn = (Button) convertView.findViewById(R.id.log_details_1_a); viewHolder.btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("statusPosition","position "+position); } }); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } return convertView; } public static class ViewHolder { TextView tv2, tv3, tv3a, tv4, tv4a; Button btn; } } 

    带可点击按钮的ListView !!!

    嗯……,这是解决我的问题的粗略方法。所以….

    item.xml:

     < ?xml version="1.0" encoding="utf-8"?>       

    MySimpleAdapter:

     import ........; public class MySimpleAdapter extends SimpleAdapter { private final Context context; private List> data; private int resource; private String[] from; private int[] to; public MySimpleAdapter(Context context,List< ? extends Map> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.context=context; this.data=(List>) data; this.resource=resource; this.from=from; this.to=to; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); View rowView = inflater.inflate(resource, null, true); Map medMap=data.get(position); final TextView[] showTv=new TextView[from.length]; for (int i = 0; i < from.length; i++) { showTv[i]=(TextView)rowView.findViewById(to[i]); showTv[i].setText(""+medMap.get(from[i])); } Button btn=(Button)rowView.findViewById(R.id.gointoBt); Button.OnClickListener mOkOnClickListener = new Button.OnClickListener() { public void onClick(View v) { Log.v("ttttttt", ""+showTv[0].getText()); Toast.makeText(context,""+showTv[0].getText(), Toast.LENGTH_LONG).show(); } }; btn.setOnClickListener(mOkOnClickListener); Button btn2=(Button)rowView.findViewById(R.id.chooseBt); Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener() { public void onClick(View v) { Log.v("hhhhhhh", ""+showTv[0].getText()); Toast.makeText(context,"abc"+showTv[0].getText(), Toast.LENGTH_LONG).show(); } }; btn2.setOnClickListener(mOkOnClickListener2); return rowView; } } 

    Activty:

     import .......; public class ActivityMain extends Activity { ListView listview; List> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("My work"); prepareData(); listview =new ListView(this); MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.item,new String[] {"uu"},new int[]{R.id.showTv}); listview.setAdapter(adapter); setContentView(listview); } private void prepareData(){ data=new ArrayList>(); Map item; item=new HashMap(); item.put("uu", "hello"); data.add(item); item=new HashMap(); item.put("uu", "myyou"); data.add(item); item=new HashMap(); item.put("uu", "piero"); data.add(item); } } 

    感谢那个非常善良的人提供这个令人敬畏但最强大的教程....这里的任何人都无法给予新人....

    我认为还有一个更好的方法。你可以使用(button.setTag())方法在按钮上添加一个标记作为位置,每当用户点击按钮时,只需要使用此按钮的标记。 它会更容易。

    谢谢和问候Rahul

    很简单。 在您的Adapter类中, getView方法将int位置更改为最终的int位置

     public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row = inflater.inflate(yourcustomizeditemview.xml, parent, false); Button btn= (Button) row.findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, " "+position, Toast.LENGTH_SHORT).show(); } }); return row; } 

    现在单击Button单击Toast显示Button所在的项目的位置。

    onClick()中似乎mListView为null。 有可能

     auto = (ListView)findViewById(R.id.auto); 

    返回null,因为在当前布局中找不到R.id.auto的视图。 我会在该行之后放置一个调试器断点并查看。

    你执行这个吗

     btnNxt = (Button) findViewById(R.id.btnNext); btnNxt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //Here I need to get that position }); 

    在getView方法中? 如果是这样的话很容易

      btnNxt = (Button) findViewById(R.id.btnNext); btnNxt.setTag(position); btnNxt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { int position=(Integer)arg0.getTag(); }); 

    来自: https : //stackoverflow.com/a/20542034