【问题标题】:The content of adapter has changed but listview did not receive a notification适配器的内容发生了变化但listview没有收到通知
【发布时间】:2014-09-05 06:27:42
【问题描述】:

我对此进行了很多研究,但没有什么能让我满意。每个人都给我一个解决方案,将 adapter.notifyDataSetChanged() 放在我已经做过的 runOnUiThread 中,但没有给我解决方案。 我的代码如下 公共类 FragmentActivity3 扩展 Fragment {

// Hashmap for ListView
ArrayList<Item> imageArry = new ArrayList<Item>();
CustomQuoteAdapter adapter;
String jsonstring;
@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView=inflater.inflate(R.layout.activity_fragment_activity1, container,false);
    jsonstring=JsonClass.readData();
    Log.d("JSon String ",jsonstring);
    new GetQuotesInfo().execute();
    adapter = new CustomQuoteAdapter(getActivity(), R.layout.list, imageArry);
    ListView dataList = (ListView) rootView. findViewById(R.id.list);
    dataList.setAdapter(adapter);
    return rootView;
}

//Async Task to load  data
    class GetQuotesInfo extends AsyncTask<Void, Void, Void>
    {
        int count;
        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            try {
                JSONArray jsonarr=new JSONArray(Globals.jsonText);
                count=jsonarr.length();
                for(int i=0;i<count;i++)
                {
                    HashMap<String, String> quoteinfo = new HashMap<String, String>();
                    String author="";
                    Log.d("Json","Reading");
                    JSONObject jsonobj=jsonarr.getJSONObject(i);
                    String name=jsonobj.getString("name");
                    quoteinfo.put("name", name);
                    JSONArray jarr=jsonobj.getJSONArray("quotes");
                    String[] myarr=new String[jarr.length()];
                    String[] myarr1=new String[jarr.length()];
                    for(int j=0;j<jarr.length();j++)
                    {


                        JSONObject jobj=jarr.getJSONObject(j);
                        author=jobj.getString("author");
                        String text=jobj.getString("text");
                        Log.d("Author ",author);
                        Log.d("Text ",text);
                        myarr[j]=text;
                        myarr1[j]=author;
                    }

                    imageArry.add(new Item(name,myarr1, myarr));

                }

            }
            catch(Exception ex)
            {

        }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            getActivity().runOnUiThread(new Runnable(){
                public void run() {
                   adapter.notifyDataSetChanged();
                }
            });
        }

    }

}

我的适配器类在下面

public class CustomQuoteAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    LinearLayout linearMain;
    ArrayList<Item> data = new ArrayList<Item>();

    public CustomQuoteAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = ((FragmentActivity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
            Item myImage = data.get(position);
            TextView txtview=new TextView(context);
            String heading=myImage.heading;
            txtview.setText(heading);
            txtview.setTextColor(Color.BLUE);
            txtview.setTextSize(20);
            linearMain.addView(txtview);

            for (int j = 0; j < myImage.getName().length; j++) {
                TextView label1 = new TextView(context);
                label1.setText(myImage.author[j]);
                linearMain.addView(label1);
                TextView label2 = new TextView(context);
                label2.setText(myImage.name[j]);
                linearMain.addView(label2);
            }
//          ImageView image = new ImageView(context);
//          int outImage = myImage.image;
//          image.setImageResource(outImage);
//          linearMain.addView(image);
        }

        return row;

    }

}

谁能给我解决我的错误的解决方案

【问题讨论】:

  • 从您的 asynctask 中删除不需要的 runOnUIthread 并在您的 json 中使用 ex.printStackTrace()。无效的 json 可能是原因
  • 您应该避免在 onPostExecute 中使用 notifyDataSetChanged。更喜欢创建一个专门用于更新适配器的回调方法。在某些情况下,getActivity() 也可以为 null,使用时要小心。

标签: android


【解决方案1】:

onPostExecute() 会在 UI 线程上自动调用,因此您不需要 runOnUIThread()

您是否检查过以确保列表中的数据确实发生了变化?此外,我没有仔细研究您的整个适配器的逻辑,但这条线看起来有一个错字。我猜如果您的资源文件中也没有那个错字,那么您的 IDE 会发现它 linearMain = (LinearLayout) row.findViewById(R.id.lineraMain); 不知道那个错字R.id.lineraMain 是否给你带来了问题。

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多