我正在为Android创建一个“滑入”(facebook风格)菜单,这个菜单将使用onItemClickListener呈现为可点击的ListView。但是,当我为列表视图使用自定义适配器时,它什么也不显示。
运行它的活动是我的“根活动”,这意味着所有其他活动都扩展了它。如果我将相同的代码放在另一个活动中,它可以使用这个适配器来渲染列表视图。此外, .setOnItemClickListener 似乎在根活动中什么都不做。创建此视图时,有些东西没有像应有的那样设置,有什么想法吗?这是代码:
public class RootActivity extends Activity {
public static final String KEY_TITLE = "title";
public static final String KEY_ID = "id";
ActionBar actionBar;
ListView slideMenu;
ListMenuAdapter adapter;
ArrayList<HashMap<String, String>> menuOptions;
private String[] menuItems={"Budget","Charts","By category"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rootview);
menuOptions = new ArrayList<HashMap<String, String>>();
slideMenu = (ListView) findViewById(R.id.slideMenuList);
slideMenu.setSaveEnabled(false);
HashMap<String, String> map;
for(int i = 0 ; i<menuItems.length ; i++ ) {
map = new HashMap<String, String>();
map.put(KEY_TITLE, menuItems[i]);
map.put(KEY_ID, ""+i);
menuOptions.add(map);
}
adapter = new ListMenuAdapter(this, menuOptions);
slideMenu.setAdapter(adapter);
Log.println(9, "slidemendeb2", ""+slideMenu.getAdapter().getCount());
slideMenu.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.println(9, "Testitemclick", ""+arg0);
}
});
}}
扩展 RootActivity 的另一个活动的示例:
public class SpentMain extends RootActivity {
public static final String KEY_TITLE = "title";
public static final String KEY_ID = "id";
ArrayList<Tag> tags;
ArrayList<Integer> tagInts = new ArrayList<Integer>();
TagDataSource tagDataSource;
Button costButton;
EditText sum;
Boolean removed = false;
ActionBar actionBar;
CheckBox checkBox;
ListView list;
ListCheckboxAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rootview);
costButton = (Button) findViewById(R.id.costadd);
sum = (EditText) findViewById(R.id.editText1);
ArrayList<HashMap<String, String>> tagsList = new ArrayList<HashMap<String, String>>();
tagDataSource = new TagDataSource(this);
tagDataSource.open();
tags = tagDataSource.getAllTags();
tagDataSource.close();
HashMap<String, String> map;
for(int i = 0 ; tags.size()>i ; i++ ) {
map = new HashMap<String, String>();
map.put(KEY_TITLE, tags.get(i).getTitle().toString());
map.put(KEY_ID, tags.get(i).getId().toString());
tagsList.add(map);
}
list=(ListView)findViewById(R.id.list);
adapter = new ListCheckboxAdapter(this, tagsList);
list.setAdapter(adapter);
// Row click event
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
for(int i = 0; tagInts.size()>i; i++) {
if(tagInts.get(i)==position) {
tagInts.remove(i);
removed = true;
}
}
if(removed==false) {
tagInts.add(position+1);
}
removed = false;
for(int i=0; i<((ViewGroup)view).getChildCount(); ++i) {
View nextChild = ((ViewGroup)view).getChildAt(i);
if(nextChild.getClass()==android.widget.CheckBox.class) {
checkBox = (CheckBox) nextChild;
if(checkBox.isChecked()==true) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(true);
}
}
}
}
});}
正如我所说,如果我使用这个适配器在 SpentMain 中渲染它,它可以正常工作,但不能在 RootActivity 中。任何帮助是极大的赞赏。