在“活动”中从Cursor填充ListView

时间:2011-09-13 13:26:33

标签: android

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(savedInstanceState);
        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);


 }

这不是填充列表。我究竟做错了什么 ?我想,它必须是一个不是ListActivity的活动,因为我也有其他观点。

listlayout.XML有ListView,listitem有textview(R.id.text)

1 个答案:

答案 0 :(得分:0)

见下面的代码

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

查看SimPleCurSorAdapter

的构造函数说明