4

我正在自定义ListView包含 aCheckBox和 a的行TextView。在我将自定义ListViews与 SimpleCursorAdapter 一起使用之前,我的onListItemClick() 工作正常。

我读过我必须添加一个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);
    }

编辑:我的问题是使自定义可点击的整个项目,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;

        }

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

4

5 回答 5

4

如果您想在单击TextView或/和CheckBox从您的任何行中添加一个特殊操作,然后为您的自定义方法中的那些添加ListView一个:OnCLickListenerViewsgetViewAdapter

 @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您的(或在 a 的情况下ListView的回调)。onListItemClickListActivity

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

编辑 :

如果您想启动新活动,无论用户在哪里单击一行,都可以在(在 的情况下)回调中启动ActivityonListItemClick活动: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单击时启动新活动,则从我上面的代码中的方法中启动新活动:TextViewListViewonClick

//...
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);
//...
于 2012-05-16T11:27:21.417 回答
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>);
}
于 2012-05-16T11:18:17.023 回答
0

您应该在 ListView 上实现onItemClickListener

ListView lv = getListView();


  lv.setOnItemClickListener(new OnItemClickListener() {


  });
于 2012-05-16T11:25:41.917 回答
0

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

egvsetOnCLickListener。

我记住你想在点击查看时打开一个活动。是的,如果您的 ListedPuzzle 类是可序列化的或可打包的,您可以使用同一意图的 putextra 方法转发整个对象。

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

于 2012-05-16T11:25:45.200 回答
0
 lstviewemojis.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
      }
于 2012-05-16T11:49:07.213 回答