
我已经阅读过我必须在我的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) getSy@R_404_6563@Service(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).
解决这个问题的最佳方法是什么?我从未真正了解整个最终交易.
@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?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)