Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 我应该在自定义ListView上放置onClickListener?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作包含checkBox和TextView的行的自定义ListView.在我使用SimplecursorAdapter自定义ListViews之前,我的onListItemClick()工作正常.

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

我仍在扩展ListActivity并将适配器传递给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);
    }

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

现在,ListView的每个项目上的checkBox应该“标记”该项目,这意味着访问数据库.

public View getView(int position,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;

        }

但v.getTag()引用了非final变量,如果我更改它,则无法分配v = vi.inflate(R.layout.puzzles_row,null).
解决这个问题的最佳方法是什么?我从未真正了解整个最终交易.

解决方法

如果要从ListView中的任何行单击TextView或/和checkBox添加特殊操作,请在自定义适配器的getView方法中为这些视图添加OnCLickListener:

@Override
        public View getView(int position,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;
        }

如果要在单击行时执行操作(无论单击该行中的哪个View(如果单击了一个)),只需使用ListView上的OnItemClickListener(或者在ListActivity的情况下使用回调onListItemClick).

另外,我希望你为checkBox设置android:focusable =“false”(在R.layout.puzzles_row中),因为我不认为onListItemClick会起作用.

编辑:

如果要在用户单击行的位置启动新活动,则在onListItemClick(在ListActivity的情况下)回调中启动新活动:

@Override
    protected void onListItemClick(ListView l,long id) {          
        Intent i = new Intent(this,id);
        startActivity(i);
    }

如果出于某种原因,当用户仅单击(例如)ListView行中的TextView时,您希望启动新的Activity,则从上面的代码中启动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);
                    }
//...

为此,您必须修改ListedPuzzle以在fetchData()方法添加puzzlescursor游标中的PuzzlesDbAdapter.KEY_ROWID列:

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

大佬总结

以上是大佬教程为你收集整理的android – 我应该在自定义ListView上放置onClickListener?全部内容,希望文章能够帮你解决android – 我应该在自定义ListView上放置onClickListener?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。