我有一个 ListView,我在其中设置Adapter onCreate。但我想将 setAdapter 放在一个静态方法中,并在另一个类中的按钮的 onClick 中调用它。这可能吗?现在它说“无法对非静态 setAdapter 方法进行静态引用”。下面是我的代码:我在代码中提到了我希望在我的静态方法“addItems()”中包含它的部分。欢迎提出建议。提前致谢
public class LogListView extends ListActivity {
/** Called when the activity is first created. */
private static String newString;
private static EntryAdapter adapter;
int clickCounter = 0;
static ArrayList<Item> items = new ArrayList<Item>();
static SharedPreferences preferences = null;
private static Context context = null;
StringTokenizer tokens;
String first;
String second;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
/*I WANT THIS LOOP TO BE IN "ADDITEMS() METHOD*/
preferences = context.getSharedPreferences("LOG", android.content.Context.MODE_PRIVATE);
newString = preferences.getString("log", "");
//items.add(new SectionItem("Log Report"));
items.add(new EntryItem("", newString));
adapter = new EntryAdapter(this, items);
setListAdapter(adapter);
/*I WANT THIS LOOP TO BE IN "ADDITEMS() METHOD ENDS*/
}
//Method which will handle dynamic insertion
public static void addItems() {
items.add(new EntryItem("", newString));
adapter.notifyDataSetChanged();
}
//Method which will handle dynamic insertion ends
@Override
public void onResume(){
super.onResume();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(!items.get(position).isSection()) {
EntryItem item = (EntryItem)items.get(position);
//Toast.makeText(this, "You clicked " + item.title , Toast.LENGTH_SHORT).show();
Toast.makeText(this, "You clicked " + position , Toast.LENGTH_SHORT).show();
}
if (position == 9){
}
super.onListItemClick(l, v, position, id);
}
}