0

我有问题,我从一项活动中获取我的数据。我想使用列表视图打印它。我该怎么做。?

    Intent intent = getIntent();
    ArrayList<String> fruitSelection = intent.getStringArrayListExtra("fruits");

    if (fruitSelection != null) {
        // use a toast to display selected fruits
        if (fruitSelection.size() > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Your selection of fruits\n\n");
            for (String s : fruitSelection) {
                sb.append("* ").append(s).append("\n");
            }
            Toast toast = Toast.makeText(this, sb.toString().trim(), Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            Toast toast = Toast.makeText(this, "Nothing selected", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();}
        }
    }       
}
4

3 回答 3

3

创建一个扩展BaseAdapter的类并在 ListView 上使用 .setAdapter()。

有关更多信息,请参见: http ://www.vogella.com/articles/AndroidListView/article.html

于 2012-08-22T13:09:23.943 回答
1

假设您正在另一个活动中查看“水果”数据,请将第二个活动设置为扩展ListActivityAdapter在 ListActivity 的 ListView 上设置以访问水果数组。

ListView listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this.getApplicationContext(), R.layout.list_view_item, fruitSelection);
于 2012-08-22T13:11:24.527 回答
0
Intent intent = getIntent();
    ArrayList<String> fruitSelection = intent.getStringArrayListExtra("fruits");

ListView  listView = (ListView) findViewById(R.id.listView);


        ArrayAdapter<String> adapter; adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, fruitSelection);

        listView.setAdapter(adapter); 

    listView = (ListView) findViewById(R.id.listView);



            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, values);

            listView.setAdapter(adapter); 
于 2012-08-22T13:12:20.573 回答