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);
}
}
}