ListView的getChildAt()保持为空,但如果在的onclick它的工作原理它的、工作原理、为空、ListView

2023-09-06 17:38:41 作者:yi颗訫为迩而跳

我有填充有ArrayAdapter件一个ListView。现在我想打出来时,请单击并检查在开始的时候,如果他们已经通过撞击(调用外部API等)。但它只能在onItemClickListener它上面的方法没有。更好地了解它,这里是一些code:

I have a ListView that is filled with items of an ArrayAdapter. Now I want to strike them out when click and also check in the beginning if they are already struck through(calling an external api etc). But it only works in the onItemClickListener the method above it doesn't. to understand it better, here is some code:

public void machListe() { 
   listViewArrayAdapter = new ArrayAdapter(getApplicationContext(),R.layout.task_item, ti);
   taskListe.setAdapter(listViewArrayAdapter);
   TextView ab=(TextView) taskListe.getChildAt(0);

所以,如果我现在Debugg我看到AB为null。

So if I Debugg now I see that ab is null.

taskListe.setOnItemClickListener(new OnItemClickListener() { 

   public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {

     TextView ab=(TextView) taskListe.getChildAt(0);

如果我在这里调试,AB不为null。

If I debug here, ab is NOT null.

推荐答案

您正试图SAFT您的意见里面的工作状态。

You are trying to saft the state of the tasks inside your Views.

您应该尝试在这一点上独立的模型和视图。创建包含一个任务,然后扩展 ArrayAdapter 并覆盖模型对象 getView 。在getView您检索从任务列表中正确的任务。为任务创建TextView的,如果在你的模型做过任务标记取消出来的文字。

You should try to separate Model and View at this point. Create a Model Object that contains a single task then extend ArrayAdapter and overwrite getView. In the getView you retrieve the correct task from the task list. Create the textview for the task and if the task is marked as done in your model you cancel out the text.

如果您尝试更改视图列表中被创建之后,你会有这个列表停止滚动,因为该列表将只持有childviews为显示在屏幕上的项目都必须这样做,其他的意见滚动以节省内存中创建。

If you try to change the views in a list after they are created you would have to do this every time the list stops scrolling because the list will only hold childviews for the items that are shown on the screen, the other views are created during scrolling to save memory.

要更改ListView项的唯一正确的地方是在相应的适配器的getView方法。

The only correct place to change a listview Item is in the getView method of the corresponding Adapter.

有一个看 ListsView教程vogella.de 了解详情

 
精彩推荐
图片推荐