Android: Adding header to dynamic listView
Posted
by
cg5572
on Stack Overflow
See other posts from Stack Overflow
or by cg5572
Published on 2012-07-02T19:57:15Z
Indexed on
2012/07/02
21:16 UTC
Read the original article
Hit count: 196
I'm still pretty new to android coding, and trying to figure things out. I'm creating a listview dynamically as shown below (and then disabling items dynamically also) - you'll notice that there's no xml file for the activity itself, just for the listitem.
What I'd like to do is add a static header to the page. Could someone explain to me how I can modify the code below to EITHER add this programatically within the java file, before the listView, OR edit the code below so that it targets a listView within an xml file!
Help would be much appreciated!!!
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);
}
}
}
© Stack Overflow or respective owner