Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

android - how to get a list item position by clicking the button inside it?

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

this is the code that I use

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);

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!

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, I used the same approach - just added casting the parent layout and I got the position w/o any 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);
        }
}

My layout for listview row :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.bbox.application"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.markupartist.android.widget.ActionBar
        android:id="@+id/actionbar" app:title="@string/ac_title" style="@style/ActionBar" />
    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="5"
        android:background="@drawable/categrory_bckgr" />
    <TextView android:id="@+id/android:empty"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/main_no_items" />

</LinearLayout>

I hope it'll help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...