ListView 无法正确显示自定义数组适配器?

发布于 2024-10-08 18:00:12 字数 1738 浏览 7 评论 0原文

您好,我的 Android 应用程序有一个 listactivity,此活动从另一个具有 Intent 的活动调用,在调用 ListActivity 的 onCreate 方法后,创建一个新的 AsynchTask 并调用 Web 服务,并将所有内容放入我的 ArrayList 数据来自 Web 服务很好,但是当我尝试实现customArrayAdapter的getView,事情变得很奇怪,它有时显示列表的内容,有时不显示,有时当我滚动时。这是我的自定义适配器的getView方法。 (布局 = textview + listView)

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;
      }
     }

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

Hi my android app has a listactivity this activity called from another activity with Intent , after calling in onCreate method of the ListActivity create a new AsynchTask and call web service and put all stuff into my ArrayList data come from web service is fine , but when i try to implement getView of the customArrayAdapter things get wierd , it sometimes show content of the list sometimes not or sometimes when i scroll.Here is my getView method of custom adapter. (Layout = textview + listView)

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;
      }
     }

[NOTE] Problem might similar to this question but i couldnt find a solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-10-15 18:00:12

尝试:

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

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

Try:

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

you were missing the mView when getting the particular row's TextView field.

陪我终i 2024-10-15 18:00:12

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

Since you said that your data to be shown in the list is pooled from the web service, is it possible that data is not loaded yet and you are already trying to access it? Are you waiting for data to be loaded from service before setting list adapter?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文