在ListView中打印数据而不是吐司

时间:2012-08-22 13:05:15

标签: android android-listview

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

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

3 个答案:

答案 0 :(得分:3)

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

有关详情,请参阅此处: http://www.vogella.com/articles/AndroidListView/article.html

答案 1 :(得分:1)

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

ListView listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this.getApplicationContext(), R.layout.list_view_item, fruitSelection);

答案 2 :(得分: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);