0
我对android编码还很陌生,并试图弄清楚事情的真相。我如下所示动态创建一个listview(然后动态地禁用项目) - 您会注意到,活动本身没有xml文件,仅用于listitem。Android:将标题添加到动态列表
我想要做的是添加一个静态头到页面。有人可以向我解释我可以如何修改下面的代码,以便将它编程添加到java文件中,在listView之前,或者编辑下面的代码,以便它在xml文件中定位一个listView!
帮助将非常感谢!
public class Start extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBaseHelper myDbHelper = new DataBaseHelper(null);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
ArrayList<String> categoryList = new ArrayList<String>();
Cursor cur = myDbHelper.getAllCategories();
cur.moveToFirst();
while (cur.isAfterLast() == false) {
if (!categoryList.contains(cur.getString(1))) {
categoryList.add(cur.getString(1));
}
cur.moveToNext();
}
cur.close();
Collections.sort(categoryList);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, categoryList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if(Arrays.asList(checkArray3).contains(String.valueOf(position))){
view.setEnabled(false);
} else {
view.setEnabled(true);
}
return view;
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(v.isEnabled()) {
String clickedCat = l.getItemAtPosition(position).toString();
Toast.makeText(this, clickedCat, Toast.LENGTH_SHORT).show();
finish();
Intent myIntent = new Intent(getApplicationContext(), Questions.class);
myIntent.putExtra("passedCategory", clickedCat);
myIntent.putExtra("startTrigger", "go");
startActivity(myIntent);
}
}
}