如何在listview中显示数据?

时间:2014-12-03 21:58:56

标签: android

Saver是Button。当我点击它时,我想从EditText获取数据,将其发送到另一个Activity并在ListView中显示;但它什么也没有显示,我不知道该怎么做。

中的

if (v == saver) {
    String textData = reminder.getText().toString();

    Intent intent3 = new Intent(CreateActivity.this, ViewActivity.class);
    intent3.putExtra(textData, "key1");
    startActivity(intent3);
}
列表活动中的

ListView list;
ArrayList<String> taskslist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);
    list = (ListView) findViewById(R.id.listView1);
    taskslist = new ArrayList<String>();
    final ArrayAdapter<String> adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, taskslist);

    list.setAdapter(adapter);
    try {
        String recivedData;

        Intent intentt;
        intentt = getIntent();

        recivedData = intentt.getExtras().getString("key1");
        taskslist = intentt.getStringArrayListExtra(recivedData);

    } catch (Exception e) {
        // TODO: handle exception
    }
    // adapter.notifyDataSetChanged();
}

2 个答案:

答案 0 :(得分:0)

而不是:

taskslist = intentt.getStringArrayListExtra(recivedData);

尝试添加到taskslist并通知适配器:

taskslist.addAll(intentt.getStringArrayListExtra(recivedData));
adapter.notifyDataSetChanged();

答案 1 :(得分:0)

您应该更新数据集,然后致电notifyDataSetChanged更新您的列表,或在设置adapter之前填充您的列表

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);
    list = (ListView) findViewById(R.id.listView1);
    taskslist = new ArrayList<String>();
    taskslist = getIntent().getString("key1").getStringArrayListExtra(recivedData);
    final ArrayAdapter<String> adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, taskslist);

    list.setAdapter(adapter);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);
    list = (ListView) findViewById(R.id.listView1);
    taskslist = new ArrayList<String>();
    final ArrayAdapter<String> adapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, taskslist);

    list.setAdapter(adapter);
    try {
        String recivedData;

        Intent intentt;
        intentt = getIntent();

        recivedData = intentt.getExtras().getString("key1");
        taskslist = intentt.getStringArrayListExtra(recivedData);

    } catch (Exception e) {
        // TODO: handle exception
    }
    adapter.setData(tasklist); // update data set
    adapter.notifyDataSetChanged();
}