Populating ListView from Cursor in an "Activity"
public class MyNewTasks extends Activity{
private DbAdapter_Task task;
private DbAdapter_Assignment assignment;
private DbAdapter_User user;
LinearLayout linear;
TextView text;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceSta开发者_运维技巧te);
String empid = getIntent().getExtras().getString("EmpID");
assignment = new DbAdapter_Assignment(getBaseContext());
assignment.open();
Cursor cursor = assignment.numOfNewTask(empid);
startManagingCursor(cursor);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText(Html.fromHtml("<H1>" + "You have " + Integer.toString(cursor.getCount()) + " new assignments." + "</H1>"));
list=(ListView)findViewById(R.layout.listlayout);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listlayout, cursor,
new String[] {"TaskID"}, new int[] { R.id.text});
list.setAdapter(adapter);
linear.addView(text);
linear.addView(list);
setContentView(linear);
}
This is not populating list. What am I doing wrong ? I guess, It has to be a Activity not ListActivity
as I have other views too.
listlayout.XML has ListView and listitem has textview (R.id.text)
see the code bellow
Cursor cursor = assignment.numOfNewTask(empid);
startManagingCursor(cursor);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText(Html.fromHtml("<H1>" + "You have " + Integer.toString(cursor.getCount()) + " new assignments." + "</H1>"));
list=new ListView(this);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listitemlayout, cursor,
new String[] {"TaskID"}, new int[] { R.id.text});
list.setAdapter(adapter);
linear.addView(text);
linear.addView(list);
setContentView(linear);
see the constructor description of the SimPleCurSorAdapter
精彩评论