我有一个listview,我想在listitems超过200时刷新。我试过下面的代码,但它没有用。有人可以提出别的建议。我正在从其他类设置的Sharedpreference中检索listvalues。我是否还需要在计数后刷新首选项,以便我不在listview中再次设置这些值?如果是,那么基本问题如何获得正确的计数?
/*if (adapter.getCount() >= 200)
{
Log.d("FLUSHED", "flushed items");
items.clear();
adapter.notifyDataSetChanged();
Toast.makeText(context, "As Value Crossed 10, Flushing List", Toast.LENGTH_SHORT).show();}*/
以下是我的完整代码:
public class LogListView extends ListActivity {
/** Called when the activity is first created. */
static String newString;
private static EntryAdapterLog adapter;
int clickCounter = 0;
static ArrayList<Item> items = new ArrayList<Item>();
static SharedPreferences preferences = null;
private static Context context = null;
static StringTokenizer tokens;
static String first;
private static String second;
private JSONArray jsonarry = null;
static String saveitems;
private JSONObject jsonobject = null;
private String subtitle;
static String title;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
//startRepeatingTimer();
adapter = new EntryAdapterLog(this, items);
// items.add(new SectionItem("Log Report"));
setListAdapter(adapter);
if (adapter.getCount() != 0) {
// Do nothing Adapter has value
}
/*if (adapter.getCount() >= 200)
{
Log.d("FLUSHED", "flushed items");
items.clear();
adapter.notifyDataSetChanged();
Toast.makeText(context, "As Value Crossed 10, Flushing List", Toast.LENGTH_SHORT).show();
}*/
else {
retreiveItems();
}
}
@Override
protected void onStart() {
super.onStart();
}
// Method which will handle dynamic insertion
public static void addItems(Context context) {
//Get MM Failed Status
preferences = context.getSharedPreferences("LOG",android.content.Context.MODE_PRIVATE);
newString = preferences.getString("log", "");
tokens = new StringTokenizer(newString, ",");
first = tokens.nextToken();
second = tokens.nextToken();
if (first != null && second != null){
items.add(new EntryItem(first, second));
}
//Get MM Failed Status ends
adapter.notifyDataSetChanged();
}
// Method which will handle dynamic insertion ends
@Override
protected void onDestroy() {
super.onDestroy();
saveItems();
}
// Save ListItems if restarted
protected static void saveItems() {
SharedPreferences prefs = context.getSharedPreferences("prefName",Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("myList", new Gson().toJson(items).toString());
editor.apply();
Log.d("LOG", "Saved Items : " + items);
}
// Save ListItems if restarted ends
// Retrieve ListItems if restarted
protected void retreiveItems() {
preferences = context.getSharedPreferences("prefName",android.content.Context.MODE_PRIVATE);
saveitems = preferences.getString("myList", "");
Log.d("LOG", "Retreived Items : " + saveitems);
try {
jsonarry = new JSONArray(saveitems);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonarry == null || jsonarry.length() == 0) {
return; //This checks before setting adapter onCreate if adapter is null
}
for (int i = 0; i < jsonarry.length(); i++) {
try {
jsonobject = jsonarry.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
// get all values here from JSONObject
title = jsonobject.optString("title");
subtitle = jsonobject.optString("subtitle");
items.add(new EntryItem(title, subtitle));
adapter.notifyDataSetChanged();
}
}
// Retrieve ListItems if restarted ends
// Counter for amount of period of time before flusing adapter
protected void flushList(){
}
// Counter for amount of period of time before flusing adapter ends
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (!items.get(position).isSection()) {
items.get(position);
Toast.makeText(this, "You clicked " + position, Toast.LENGTH_SHORT).show();
}
super.onListItemClick(l, v, position, id);
}
}
答案 0 :(得分:0)
使用ArrayList
items
而不是Adapter