重新加载ListView,.notifyDataSetChanged不起作用

时间:2015-03-23 12:42:27

标签: android listview listadapter

我知道这里已经被问过这么多次了,但似乎没有什么事情可行。

我有一个ListView,正在加载它,然后在按钮单击时,列表正在文件中更改,然后我需要重新加载新数据。目前,它只是将文件内容添加到旧列表的末尾,而不是创建新的。我不知道我做错了什么,我已经尝试了boxAdapter.notifyDataSetChanged();,其中boxAdapter链接到我的ListAdapter。

我有一个方法,我打电话来加载listview

public void setUpPage(){

    fillData();

    boxAdapter = new ListAdapter(this, events);
    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(boxAdapter);
    //boxAdapter.notifyDataSetChanged();
}

然后在我的public void removeClick(View v)我正在呼叫setUpPage();我尝试将boxAdapter,notifyDataSetChanged();放在我的按钮方法的末尾,但这也没有帮助。任何帮助将不胜感激,也许我应该把notifydatasettts置于其他地方?

编辑: 完整listadapter:

public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Event> objects;

ListAdapter(Context context, ArrayList<Event> events) {
    ctx = context;
    objects = events;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void updateList(){
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.list_item, parent, false);
    }

    Event p = getProduct(position);

    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
    ((TextView) view.findViewById(R.id.description)).setText(p.description);
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);

    CheckBox selectCB = (CheckBox) view.findViewById(R.id.selectBox);
    selectCB.setButtonDrawable(R.drawable.unchecked);
    selectCB.setOnCheckedChangeListener(myCheckChangList);
    selectCB.setTag(position);
    selectCB.setChecked(p.box);
    return view;
}

Event getProduct(int position) {
    return ((Event) getItem(position));
}

ArrayList<Event> getBox() {
    ArrayList<Event> box = new ArrayList<Event>();
    for (Event p : objects) {
        if (p.box)
            box.add(p);

    }
    return box;
}

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        getProduct((Integer) buttonView.getTag()).box = isChecked;

        if(buttonView.isChecked()) {
            buttonView.setButtonDrawable(R.drawable.checked);
        }
        else{
            buttonView.setButtonDrawable(R.drawable.unchecked);
        }
    }
};
}

填写数据:

void fillData() {
    String ret = "";
    try {
        InputStream inputStream = openFileInput("UserEvents.txt");

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) {
                stringBuilder.append(receiveString);

                    String[] separated = receiveString.split("~");
                    String event = separated[0]; // this will contain "name"
                    String eventloc = separated[1]; // this will contain the rest
                try {
                    JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
                    if (mainNode != null) //puts the values into an array
                    {
                        for (int i = 0; i < mainNode.length(); i++) {
                            JSONObject eachObject = mainNode.getJSONObject(i);

                            String location = eachObject.getString("eventLocation");
                            String eventtime = eachObject.getString("eventTime");
                            String loctime = location + "     " + eventtime;
                            if(loctime.equals(eventloc))
                            {
                                String eventdesc = eachObject.getString("eventDescription");
                                int img = R.drawable.ic_launcher; //from here use event name as to which image to show
                                events.add(new Event(event, eventloc, eventdesc, img, false));
                            }

                        }
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
            inputStream.close();
            ret = stringBuilder.toString();
        }
    } catch (FileNotFoundException e) {
        Log.e("h", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("h", "Can not read file: " + e.toString());
    }
}

3 个答案:

答案 0 :(得分:0)

我建议您在public static上创建一个updateList ListAdapter方法,例如:

public void updateList(){
   yourAdapter.notifyDataSetChanged();
}

并在您想要更新list

时调用此方法

答案 1 :(得分:0)

试试这个..

  runOnUiThread(new Runnable() {
        @Override
        public void run() {
           boxAdapter,notifyDataSetChanged(); 
        }
    });

答案 2 :(得分:0)

您好,我建议您提出的建议。每次调用setUpPage()时都不要初始化下面的代码。

boxAdapter = new ListAdapter(this, events);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);

只需初始化一次创建活动或片段的方法。然后当你有原始数据时。在您的情况下,您从&#34; UserEvents.txt&#34;中读取数据。 收集完所有数据后,只需添加您创建的数组列表即可。并查看打击代码。

void fillData() { 
    String ret = "";
    try { 
        InputStream inputStream = openFileInput("UserEvents.txt");

        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((receiveString = bufferedReader.readLine()) != null) 
            {
                stringBuilder.append(receiveString);

                    String[] separated = receiveString.split("~");
                    String event = separated[0]; // this will contain "name"
                    String eventloc = separated[1]; // this will contain the rest

                try { 
                        JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
                        if (mainNode != null) //puts the values into an array
                        { 
                            for (int i = 0; i < mainNode.length(); i++) {
                                JSONObject eachObject = mainNode.getJSONObject(i);

                                String location = eachObject.getString("eventLocation");
                                String eventtime = eachObject.getString("eventTime");
                                String loctime = location + "     " + eventtime;
                                if(loctime.equals(eventloc))
                                { 
                                    String eventdesc = eachObject.getString("eventDescription");
                                    int img = R.drawable.ic_launcher; //from here use event name as to which image to show
                                    events.add(new Event(event, eventloc, eventdesc, img, false));
                                } 

                            } 
                        } 
                } 
                catch (JSONException e)
                { 
                    e.printStackTrace();
                } 
            } 

            inputStream.close();

            ret = stringBuilder.toString();
        } 
    } catch (FileNotFoundException e) {
        Log.e("h", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("h", "Can not read file: " + e.toString());
    } 

    boxAdapter.notifyDataSetChanged();
} 

您也可以告诉我们您的基本适配器如何使用数组列表或内容列表。如果你展示适配器会更好。

谢谢