android - listView 项目中的 2 行

标签 android

我想在每个 ListView 项中有两行。这是我的代码,但它不起作用。

public class nea extends ListActivity{
    private List<Message> messages; 

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle);
        setContentView(R.layout.main2);
        loadFeed(ParserType.DOM);
    }


    private void loadFeed(ParserType type){
        try{
            Log.i("News", "ParserType="+type.name());
            FeedParser parser = FeedParserFactory.getParser(type);
            long start = System.currentTimeMillis();
            messages = parser.parse();
            long duration = System.currentTimeMillis() - start;
            Log.i("News", "Parser duration=" + duration);
            String xml = writeXml();
            Log.i("News", xml);
            List<String> titles = new ArrayList<String>(messages.size());
            for (Message msg : messages){

                String o = titles.get(messages.size());
                if (o != null) {
                    TextView tt = (TextView) findViewById(R.id.TextView01);
                    TextView bt = (TextView) findViewById(R.id.TextView02);
                    if (tt != null) {
                         titles.add(""+msg.getDate());                            }
                    if(bt != null){
                        titles.add("date: "+ msg.getTitle());
                    }return;
            //titles.add(msg.getDate()+"\n"+msg.getTitle());
                }
            //  titles.add(msg.getTitle()+"  "+msg.getDate());

            }
            ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(this, R.layout.row,titles);
            this.setListAdapter(adapter);
        //  textView.setText(Html.fromHtml(titles.get(position)));

        } catch (Throwable t){
            Log.e("News",t.getMessage(),t);
        }
    }

第二种方式——不工作

    List<String> titles = new ArrayList<String>(messages.size());
                for (Message msg : messages){       

                        String o = titles.get(messages.size());
                    if (o != null) {
                        TextView tt = (TextView) findViewById(R.id.TextView01);
                        TextView bt = (TextView) findViewById(R.id.TextView02);
                        if (tt != null) {
                             titles.add(""+msg.getDate());                            }
                        if(bt != null){
                            titles.add("date: "+ msg.getTitle());
                        }return;
                //titles.add(msg.getDate()+"\n"+msg.getTitle());
                    }
                //  titles.add(msg.getTitle()+"  "+msg.getDate());

                }
                ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String>(this, R.layout.row,titles);
                this.setListAdapter(adapter);
            //  textView.setText(Html.fromHtml(titles.get(position)));

            } catch (Throwable t){


Log.e("News",t.getMessage(),t);
        }

最佳答案

为了在每个项目上创建一个带有两行文本的 ListView,我按以下方式使用了一个 SimpleAdapter:

创建一个 List 来保存您的数据。每个 Map 条目都将具有每个 ListView 项的键(第一行)和值(第二行)。

List<Map<String, String>> data = new ArrayList<Map<String, String>>();

对于我们要放入 ListView 的每一对,创建一个 HashMap 并将其添加到之前声明的 List 中。

Map<String, String> datum = new HashMap<String, String>(2);
                datum.put("First Line", "First line of text");
                datum.put("Second Line","Second line of text");
                data.add(datum);

注意:如果要添加多行,则需要创建另一个类似于上面的datum的对象。

然后声明一个SimpleAdapter。请注意在构造函数中使用了“第一行”和“第二行”字符串。这将告诉适配器如何使用之前声明的 Map 的字符串。还要注意布局 android.R.layout.simple_list_item_2 和文本 ID android.R.id.text1android.R.id.text2 的使用

SimpleAdapter adapter = new SimpleAdapter(this, data,
                    android.R.layout.simple_list_item_2, 
                    new String[] {"First Line", "Second Line" }, 
                    new int[] {android.R.id.text1, android.R.id.text2 });

然后您需要将 adapter 设置为您的 ListView 的适配器

ListView listView = (ListView) findViewById(R.id.theIdOfYourListView);
listView.setAdapter(adapter);

关于android - listView 项目中的 2 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837834/

相关文章:

android - 如何在 RxJava 2 中的 groupBy 之后获取列表?

java - toast 线从未被调用

android - 无法为抽象类 'GoogleServicesTask' 创建代理类。与 'com.google.gms:google-services:4.3.4'

android - SIM 小程序可以做什么?

android - React Native - 从android内容uri获取文件名

android - 在线性布局中以编程方式移动布局位置

android TagHandler 和 CSS

Android 自定义适配器过滤器正常但 ListView 未更新

java - Robolectric String.format() 格式化数字时的无效行为

java - 我想在我的 Homefragment.java 类的 recyclerview 中添加水平滚动的卡片 View