如何通过点击里面的按钮来获取一个列表项的位置?按钮、里面、位置、列表

2023-09-13 00:22:46 作者:狂是我本性

其实我读过一些previous问题,这个...

actually I've read some previous questions about this...

这是我用code

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

and class 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);
        }
    };

然而,当我按一下按钮,我还是得到了很多的错误是这样的:

However, when I click the button, I still get a lot of errors like this:

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)

这就是它! 我希望它不会那么难以想通了!

That's it! I hope it won't be so difficult to be figured out!

谢谢!

推荐答案

ListView的可点击的按钮!

嗯......,这里的粗糙的方法来解决我的问题,所以远....

Well...., here's the rough method to solve my problem SO FAR....

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
<TextView android:id="@+id/showTv"
      android:layout_alignParentLeft="true"
      android:gravity="center_vertical"
      android:textSize="24dip"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content" /> 
 <RelativeLayout android:id="@+id/jjjj"
                 android:layout_alignParentRight="true"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content">        
      <Button android:id="@+id/gointoBt"
               android:focusable="false"
               android:layout_alignParentRight="true"
               android:text="abc"
               android:layout_height="wrap_content"
               android:layout_width="wrap_content"/> 
      <Button android:id="@+id/chooseBt"
              android:layout_toLeftOf="@id/gointoBt"
              android:layout_marginRight="10dip"
              android:text="text"
              android:focusable="false"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>
  </RelativeLayout> 

MySimpleAdapter:

MySimpleAdapter:

import ........;

public class MySimpleAdapter extends SimpleAdapter {

private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;

public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
   super(context, data, resource, from, to);
   this.context=context;
   this.data=(List<Map<String, Object>>) 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<String, Object> 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:

Activty:

import .......;

public class   ActivityMain extends Activity {

ListView listview;
List<Map<String,Object>> 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<String,Object>>();
   Map<String,Object> item;
   item=new HashMap<String,Object>();
   item.put("uu", "hello");
   data.add(item);
   item=new HashMap<String,Object>();
   item.put("uu", "myyou");
   data.add(item);
   item=new HashMap<String,Object>();
   item.put("uu", "piero");
   data.add(item);
}

}

感谢您的男人是这样一种提供这个真棒但最强大的教程.... 在这里,任何人都无法给予的菜鸟......

Thanks for that Man that is so kind to provide this AWESOME yet Mightiest tutorial.... which anyone here couldn't give to the noobs....