我应该将onClickListener放在自定义ListView上的哪个位置?

时间:2012-05-16 11:14:24

标签: android android-listview onclicklistener

我正在制作包含ListViewCheckBox的自定义TextView行。在我使用自定义ListViews与SimpleCursorAdapter之前,我的onListItemClick()工作正常。

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

我仍在延长ListActivity并将Adapter传递给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给出的答案。我onListItemClick的{​​{1}}就足够了。我只需要设置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; } 指的是非最终变量,如果我更改它,则无法分配v.getTag()。 解决这个问题的最佳方法是什么?我从未真正了解整个最终协议。

5 个答案:

答案 0 :(得分:3)

如果您想在TextView中的任意行点击CheckBox或/和ListView时添加特殊操作,请添加OnCLickListener自定义Views getView方法中的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; }

View

如果您想在点击行时执行操作(无论点击该行的OnItemClickListener是什么(如果点击了一行)),只需使用ListView上的onListItemClick即可(或ListActivity)的回调android:focusable="false"

此外,我希望您为CheckBoxR.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); }

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); } //... 方法中的新活动:

ListedPuzzle

要实现此功能,您必须修改PuzzlesDbAdapter.KEY_ROWID以在puzzlesCursor方法中添加fetchData()光标中的//... 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); //... 列:

{{1}}

答案 1 :(得分:2)

您可以在适配器中分配onClickListener,但这是不好的做法。

你应该做的是,像这样实施onItemClick

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    TextView text = parent.getItemAtPosition(position);
    // DO SOMETHING or in your case 
    //startActivity(new Intent(<the correct intent>);
}

答案 2 :(得分:0)

您应该在ListView上实施onItemClickListener

ListView lv = getListView();


  lv.setOnItemClickListener(new OnItemClickListener() {


  });

答案 3 :(得分:0)

仅在适配器中使用onClick Listner。在您的适配器中,您将返回v,它是对象的视图kiond。把你的onCLickListener放在那里。

e.g。 v.setOnCLickListener。

我记得您想要点击视图打开活动。是的,如果您的ListedPuzzle类是可序列化的或可分区的,您可以使用putextra intent来转发整个对象。

如果您不清楚答案,请告诉我,我将为您提供一些小码代码

答案 4 :(得分:0)

 lstviewemojis.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
      }