【问题标题】:ListView does not show custom arrayadapter correctly?ListView 没有正确显示自定义数组适配器?
【发布时间】:2010-12-18 20:44:50
【问题描述】:

嗨,我的 android 应用程序有一个 listactivity,这个活动是从另一个带有 Intent 的活动中调用的,在调用 ListActivity 的 onCreate 方法后,创建一个新的 AsynchTask 并调用 Web 服务并将所有来自 Web 服务的数据放入我的 ArrayList 中就可以了,但是当我尝试实现 customArrayAdapter 的 getView 时,事情变得很奇怪,它有时会显示列表的内容,有时不显示,有时当我滚动时。这是我的自定义适配器的 getView 方法。 (布局 = 文本视图 + 列表视图)

class EntryDetailsAdapter extends ArrayAdapter<Entry>{

      private ArrayList<Entry> entryDetails;
      private Entry tempEntry;

      public EntryDetailsAdapter(Context context, int textViewResourceId,
        List<Entry> objects) {
       super(context, textViewResourceId, objects);
       // TODO Auto-generated constructor stub
       entryDetails =  (ArrayList<Entry>) objects;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
       // TODO Auto-generated method stub
       View mView = convertView;

       if(mView == null){

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.entry_rows, null);

       }

       tempEntry = entryDetails.get(position);
       //data is fine but problem is while occur while showing data

       if(tempEntry != null){
        //get the textview from list row xml that i defined
        TextView textView = (TextView) findViewById(R.id.entryRowTextView);
        if(textView != null){
        //i dont know why but this sometimes giving null so check
         textView.setText(tempEntry.getEntryContent());
        }
       }
       return mView;
      }
     }

[注意] 问题可能类似于this question,但我找不到解决方案

【问题讨论】:

    标签: java android listview android-arrayadapter


    【解决方案1】:

    试试:

     TextView textView = (TextView)mView.findViewById(R.id.entryRowTextView);
    

    您在获取特定行的 TextView 字段时缺少 mView。

    【讨论】:

      【解决方案2】:

      既然您说要显示在列表中的数据是从 Web 服务中汇集的,那么数据是否可能尚未加载并且您已经在尝试访问它?您是否在设置列表适配器之前等待从服务中加载数据?

      【讨论】:

      • 没有结果,在你说我检查了已经汇集的数据之后,没有锁,仍然以一种奇怪的方式显示列表,我怀疑问题出在 getView 方法上
      • 正如 dhaag23 所说,您缺少 TextView textView = (TextView)mView.findViewById(R.id.entryRowTextView) 。另外,要获得更有效的列表,请尝试使用 ViewHolder 模式
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多