简体   繁体   English

在一个listview行中多行文本?

[英]Multiple lines of text in one listview row?

I would like to be able to display two lines of text per row in my listview. 我希望能够在列表视图中每行显示两行文本。 在此处输入图片说明

Under each of those titles, I would like to display another line of text for the date and author name. 在这些标题下,我想显示日期和作者姓名的另一行文本。

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    titleArray = new ArrayList<String>();
    titleArrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item,
        R.id.itemName, titleArray);

}

public void getThreads() throws IOException {
    Thread getThreadsThread = new Thread() {
        public void run() {
                Document doc = null;
                try {
                    doc = Jsoup.connect(Constants.FORUM).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Elements threads = doc.select(".topic_title");
                for (Element thread : threads) {
                    threadTitle = thread.text();
                    titleArray.add(threadTitle);
                }
        } 
    };
    getThreadsThread.start();
    try {
        getThreadsThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    mHandler.sendEmptyMessage(0);
}

final Handler mHandler = new Handler(){ 
    public void handleMessage (Message  msg) {
        switch (msg.what) {
        case 0:
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setListAdapter(titleArrayAdapter);
                }
            });
            break;
        }
    } 
}; 

How would you recommend I add another line to the row? 您如何建议我在行中添加另一行? For now I would just like to add "Author" and "Date" to the second line, I just need a place holder until I have the code to parse out the corresponding fields. 现在,我只想在第二行中添加“作者”和“日期”,我只需要一个占位符,直到我拥有解析相应字段的代码。

像这个基本例子吗?

I would recommend to use a Custom ListAdapter extending the BaseAdapter. 我建议使用自定义ListAdapter扩展BaseAdapter。

Store your data in a ArrayList and make a new GetView() handling the data in the XML-Layout. 将数据存储在ArrayList中,并创建一个新的GetView()来处理XML布局中的数据。

How would you recommend I add another line to the row? 您如何建议我在行中添加另一行?

Add a second TextView to res/layout/list_item.xml , sized and positioned wherever you want it. 将第二个TextView添加到res/layout/list_item.xml ,其大小和位置都可以在任何位置找到。

In addition, you will need to subclass ArrayAdapter and override getView() . 另外,您将需要ArrayAdapter并重写getView() ArrayAdapter only knows how to handle one TextView per row -- if you want more than that, you need to handle the rest yourself. ArrayAdapter仅知道如何处理每行一个TextView -如果您想要更多,则需要自己处理其余的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM