我应该将 onClickListener 放在自定义 ListView 的哪里?

esk*_*era 4 android android-listview onclicklistener

我正在定制包含 a和 aListView的行。在我使用SimpleCursorAdapter 自定义之前,我的 工作正常。CheckBoxTextViewListViewsonListItemClick()

我读过我必须添加一个onClickListener到我的TextViews但是在哪里?为什么?

我仍在扩展ListActivity并传递Adapterto setListAdapter(listedPuzzleAdapter);,不是吗?

public class PuzzleListActivity extends ListActivity {

    private PuzzlesDbAdapter mDbHelper;
    private Cursor puzzlesCursor;

    private ArrayList<ListedPuzzle> listedPuzzles = null;
    private ListedPuzzleAdapter listedPuzzleAdapter;

    private class ListedPuzzleAdapter extends ArrayAdapter<ListedPuzzle> {

        private ArrayList<ListedPuzzle> items;

        public ListedPuzzleAdapter(Context context, int textViewResourceId,
                ArrayList<ListedPuzzle> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.puzzles_row, null);
            }
            ListedPuzzle lp = items.get(position);
            if (lp != null) {
                TextView title = (TextView) v.findViewById(R.id.listTitles);
                title.setText(lp.getTitle());
                CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                star.setChecked(lp.isStarred());
            }
            return v;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.puzzles_list);

        // Create database helper to open connection
        mDbHelper = new PuzzlesDbAdapter(this);
        mDbHelper.open();

        fetchData();
    }   

    private void fetchData() {
        puzzlesCursor = mDbHelper.fetchAllPuzzles();
        startManagingCursor(puzzlesCursor);

        listedPuzzles = new ArrayList<ListedPuzzle>();
        ListedPuzzle lp;

        puzzlesCursor.moveToFirst();
        while (!puzzlesCursor.isAfterLast()) {
            lp = new ListedPuzzle();
            lp.setTitle(puzzlesCursor.getString(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_TITLE)));
            lp.setStarred(puzzlesCursor.getInt(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_STARRED)) > 0);
            listedPuzzles.add(lp);
            puzzlesCursor.moveToNext();
        }

        listedPuzzleAdapter = new ListedPuzzleAdapter(this,
                R.layout.puzzles_row, listedPuzzles);
        setListAdapter(listedPuzzleAdapter);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, PuzzleQuestionActivity.class);
        i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id);
        startActivity(i);
    }
Run Code Online (Sandbox Code Playgroud)

编辑:我的问题是让整个自定义项目ListView可点击,所以我发现最好的答案是@Luksprog 给出的答案。onListItemClick我的就ListActivity足够了。我只需要设置android:focusable='false'即可使其工作。

现在,CheckBox每个项目上的ListView应该为该项目“加注星标”,这意味着访问数据库。

public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.puzzles_row, null);
            }
            ListedPuzzle lp = items.get(position);
            if (lp != null) {
                TextView title = (TextView) v.findViewById(R.id.listTitles);
                title.setText(lp.getTitle());
                CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                star.setChecked(lp.isStarred());

                star.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        Integer realPosition = (Integer) v.getTag();
                        ListedPuzzle obj = items.get(realPosition);
                        obj.getId();

                    }

                });
            }
            return v;

        }
Run Code Online (Sandbox Code Playgroud)

v.getTag()指的是非最终变量,如果我更改它,则v = vi.inflate(R.layout.puzzles_row, null)无法分配。解决这个问题的最佳方法是什么?我从来没有真正理解整个最终协议。

Luk*_*rog 5

如果您想在单击任何行中的TextView或/和时添加特殊操作,则为自定义方法中的操作添加一个:CheckBoxListViewOnCLickListenerViewsgetViewAdapter

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.puzzles_row, null);
            }
            ListedPuzzle lp = items.get(position);
            if (lp != null) {
                TextView title = (TextView) v.findViewById(R.id.listTitles);
                //set as the tag the position parameter 
                title.setTag(new Integer(position));                    
                title.setOnclickListener(new OnCLickListener(){

                @Override 
                public void onClick(View v) {
                    // Do the stuff you want for the case when the row TextView is clicked
                    // you may want to set as the tag for the TextView the position paremeter of the `getView` method and then retrieve it here
                    Integer realPosition = (Integer) v.getTag();
                    // using realPosition , now you know the row where this TextView was clicked
                }
            }); 
                title.setText(lp.getTitle());
                CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
                star.setChecked(lp.isStarred());
            }
            return v;
        }
Run Code Online (Sandbox Code Playgroud)

如果您想在单击一行时执行操作(无论View单击该行中的哪一行(如果单击了某一行)),只需使用OnItemClickListener您的(或在 a 的情况下使用ListView回调)。onListItemClickListActivity

另外,我希望您设置android:focusable="false"CheckBox(in R.layout.puzzles_row),因为我认为否则onListItemClick不会起作用。

编辑 :

如果您想启动新活动,无论Activity用户单击一行的位置,都可以onListItemClick在(在 )回调中启动新活动:ListActivity

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {          
        Intent i = new Intent(this, PuzzleQuestionActivity.class);
        i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id);
        startActivity(i);
    }
Run Code Online (Sandbox Code Playgroud)

如果出于某种原因,您想Activity在用户单击(例如)TextView行中时启动新活动,则在上面的代码中的方法ListView中启动新活动:onClick

//...
title.setOnclickListener(new OnCLickListener(){

                    @Override 
                    public void onClick(View v) {
                        Integer realPosition = (Integer) v.getTag();
                        ListedPuzzle obj = items.get(realPosition);
                        Intent i = new Intent(this, PuzzleQuestionActivity.class);
                        i.putExtra(PuzzlesDbAdapter.KEY_ROWID, obj.getTheId());//see below
                        startActivity(i);
                    }
//...
Run Code Online (Sandbox Code Playgroud)

为此,您必须进行修改ListedPuzzle以在方法中添加光标PuzzlesDbAdapter.KEY_ROWID中的列:puzzlesCursorfetchData()

//...
while (!puzzlesCursor.isAfterLast()) {
            lp = new ListedPuzzle();
            lp.setTitle(puzzlesCursor.getString(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_TITLE)));
            lp.setStarred(puzzlesCursor.getInt(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_STARRED)) > 0);
            lp.setTheId(puzzlesCursor.getLong(puzzlesCursor
                    .getColumnIndex(PuzzlesDbAdapter.KEY_ROWID)));
            listedPuzzles.add(lp);
//...
Run Code Online (Sandbox Code Playgroud)