前人的精力我在哪里放置onClickListener上自定义的ListView? [英] Where sould I place the onClickListener on a Custom ListView?

查看:111
本文介绍了前人的精力我在哪里放置onClickListener上自定义的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个自定义的的ListView A 复选框的TextView 。之前我使用的自定义列表视图与SimpleCursorAdapter,我的 onListItemClick()工作的罚款。

I am making a custom ListView of rows containing a CheckBox and a TextView. Before I used custom ListViews with SimpleCursorAdapter, my onListItemClick() worked fine.

我读过我有一个 onClickListener 添加到我的 TextViews 哪来?为什么?

I've read I have to add an onClickListener to my TextViews but WHERE? And WHY?

我还在延长 ListActivity 并传递一个适配器 setListAdapter(listedPuzzleAdapter); ,我是不是?

I am still extending ListActivity and passing an Adapter to setListAdapter(listedPuzzleAdapter);, am I not?

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给出。该 onListItemClick 从我的 ListActivity 就足够了。我只是需要设置机器人:可调焦=假,使其工作

My question was towards making the whole item of a custom ListView clickable so I found the best answer was the one given by @Luksprog. The onListItemClick from my ListActivity was enough. I just needed to set the android:focusable='false' to make it work.

现在,在复选框的ListView每个项目应明星的项目,这意味着,accesing数据库。

Now, the CheckBox on each item of the ListView should "star" that item, which means, accesing the DB.

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;

        }

v.getTag()是指非final的变量,如果我改变了 V = vi.inflate(R。 layout.puzzles_row,空)不能分配。 什么是解决这个问题的最好方法是什么?我从来没有真正了解整个最终协议。

But the v.getTag() refers to a non-final variable and if I change it the v = vi.inflate(R.layout.puzzles_row, null) cannot be assigned. What's the best way to solve this? I never really understood the whole final deal.

推荐答案

如果您要添加,当您点击了的TextView 和/或<$ C专项行动$ C>复选框从任何行中的的ListView 再加入 OnCLickListener 的那些浏览在自定义适配器 getView 方法:

If you want to add a special action for when you click the TextView or/and CheckBox from any of the rows in your ListView then add a OnCLickListener for those Views in the getView method of your custom Adapter:

 @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;
        }

如果你想要做一个动作时,一排被点击(无论是什么查看该行被点击(如果是点击)),只需使用 OnItemClickListener 的ListView (或回调 onListItemClick 中的情况下,一个 ListActivity )。

If you want to do an action when a row is clicked(no matter what View from that row was clicked(if one was clicked)) just use the OnItemClickListener on your ListView(or the callback onListItemClick in the case of a ListActivity).

另外,我希望你设置机器人:可调焦=假复选框(在 R.layout.puzzles_row ),因为我不认为 onListItemClick 将工作并非如此。

Also, I hope you set android:focusable="false" for the CheckBox(in R.layout.puzzles_row) because I don't think onListItemClick will work otherwise.

编辑:

您开始新的活动 onListItemClick (在的情况下, ListActivity )的回调,如果你想开始新的活动无论用户点击某行

You start the new Activity in the onListItemClick(in the case of the ListActivity) callback if you want to start the new activity no matter where the user clicks a row:

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

如果由于某种原因,你要开始新的活动当用户点击(例如)的TextView 的ListView 行再启动新的活动从的onClick 方法我的code以上:

If, for some reason, you want to start the new Activity when the user clicks only(for example) the TextView in a ListView row then start the new activity in the onClick method from my code above:

//...
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 也从添加 PuzzlesDbAdapter.KEY_ROWID puzzlesCursor 光标在 fetchData()方法:

For this to work you'll have to modify ListedPuzzle to also add the PuzzlesDbAdapter.KEY_ROWID column from the puzzlesCursor cursor in the fetchData() method:

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

这篇关于前人的精力我在哪里放置onClickListener上自定义的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆