我知道这里已经问了很多次了,但似乎什么都不起作用。
我有一个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();
放在按钮方法的末尾,但这也没有帮助。如果有任何帮助,我们将不胜感激,也许我应该把通知数据等放在其他地方?
编辑:完整列表适配器:
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());
}
}
试试这个。。
runOnUiThread(new Runnable() {
@Override
public void run() {
boxAdapter,notifyDataSetChanged();
}
});
你好,我在你的观点中有什么建议。不要每次调用setUpPage()时都初始化以下代码。
boxAdapter = new ListAdapter(this, events);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
只需在活动或片段的Create方法中初始化一次。然后当你有原始数据可用时。在您的案例中,您从"UserEvents.txt"中读取数据。收集完所有数据后,只需添加您创建的数组列表即可。看看破解密码。
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();
}
您还可以告诉我们您的基本适配器是如何使用数组列表或内容列表的吗。如果你展示适配器会更好。
感谢
我建议您在ListAdapter
上创建一个public static
updateList
方法,如:
public void updateList(){
yourAdapter.notifyDataSetChanged();
}
并在想要更新list
时调用此方法