【问题标题】:Adding ListView Sub Item Text in Android在 Android 中添加 ListView 子项文本
【发布时间】:2011-12-16 12:57:41
【问题描述】:

我创建了一个 RSS 阅读器,可以在列表视图中列出项目。我还想要每个项目下方的日期,但我不知道该怎么做。我需要有人帮助才能使子项文本显示从 RSS 提要中检索到的 pubDate。

这是我班级的代码:

public class RSSReader extends Activity implements OnItemClickListener
{
    public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos";

    public final String tag = "RSSReader";
    private RSSFeed feed = null;

    /** Called when the activity is first created. */

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        // go get our feed!
        feed = getFeed(RSSFEEDOFCHOICE);

        // display UI
        UpdateDisplay();
    }

    private RSSFeed getFeed(String urlToRssFeed)
    {
        try
        {
            // setup the url
           URL url = new URL(urlToRssFeed);

           // create the factory
           SAXParserFactory factory = SAXParserFactory.newInstance();
           // create a parser
           SAXParser parser = factory.newSAXParser();

           // create the reader (scanner)
           XMLReader xmlreader = parser.getXMLReader();
           // instantiate our handler
           RSSHandler theRssHandler = new RSSHandler();
           // assign our handler
           xmlreader.setContentHandler(theRssHandler);
           // get our data via the url class
           InputSource is = new InputSource(url.openStream());
           // perform the synchronous parse           
           xmlreader.parse(is);
           // get the results - should be a fully populated RSSFeed instance, or null on error
           return theRssHandler.getFeed();
        }
        catch (Exception ee)
        {
            // if we have a problem, simply return null
            System.out.println(ee.getMessage());
            System.out.println(ee.getStackTrace());
            System.out.println(ee.getCause());
            return null;
        }
    }
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, 0, 0, "Refresh");
        Log.i(tag,"onCreateOptionsMenu");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case 0:

            Log.i(tag,"Set RSS Feed");
            return true;
        case 1:
            Log.i(tag,"Refreshing RSS Feed");
            return true;
        }
        return false;
    }

    private void UpdateDisplay()
    {
        TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
        TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
        ListView itemlist = (ListView) findViewById(R.id.itemlist);


        if (feed == null)
        {
            feedtitle.setText("No RSS Feed Available");
            return;
        }

        if(feedtitle != null)
            feedtitle.setText(feed.getTitle());
        if(feedpubdate != null)
            feedpubdate.setText(feed.getPubDate());


        ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

        itemlist.setAdapter(adapter);

        itemlist.setOnItemClickListener(this);

        itemlist.setSelection(0);
    }

    @Override
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
        //Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");       

        Intent itemintent = new Intent(this,ShowDescription.class);

        Bundle b = new Bundle();
        b.putString("title", feed.getItem(position).getTitle());
        b.putString("description", feed.getItem(position).getDescription());
        b.putString("link", feed.getItem(position).getLink());
        b.putString("pubdate", feed.getItem(position).getPubDate());

        itemintent.putExtra("android.intent.extra.INTENT", b);

        startActivity(itemintent);
    }
}

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
 <TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Android RSSReader"
android:id="@+id/feedtitle"/>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text=""
android:id="@+id/feedpubdate"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist"

android:fastScrollEnabled="true"/>    
 </LinearLayout>

这就是它现在在 Eclipse 中的样子:

这是运行的样子:

如何使子项文本显示从 RSS 提要中检索到的 pubDate?

【问题讨论】:

    标签: java android listview android-layout


    【解决方案1】:

    最简单的解决方案可能是将您正在使用的ArrayAdapterandroid.R.layout.simple_list_item_1 替换为SimpleAdapterandroid.R.layout.simple_list_item_2 预定义布局。此布局由两个TextViews 组成,id 分别为android.R.id.text1(“项目”)和android.R.id.text2(“子项目”),您需要将其作为SimpleAdapter 的参考到工作。

    looking at the constructor for SimpleAdapter 您会注意到,除了 Context 实例和布局资源的 id 之外,它还需要三个您可能不熟悉的参数:

    • 一个List&lt;? extends Map&lt;String, ?&gt;&gt; 实例,您可以在其中放置您希望ListView 显示的元素。元素采用Map 的形式,即类似于由名称/值对形式的属性组成的结构。例如,您可以分别使用 "title""date" 作为每个 RSS 项目的标题和日期的键。
    • 一个字符串数组,用于在每个映射中放置您想要在ListView 上显示的键的名称。
    • 一个整数数组,您需要将部件的 id 放在列表项视图中,您希望在其中显示由前面的字符串数组中的键引用的单个元素。例如,如果您想分别在“项目”和“子项目”视图中显示 RSS 项目的标题和日期,则使用 new String[] { "title", "date" } 作为字符串数组参数,并使用 new int[] { android.R.id.text1, android.R.id.text2 } 作为此参数。

    一个粗略的代码示例,仅供参考:

    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    for (RSSItem item : feed.getAllItems()) {
        Map<String, String> datum = new HashMap<String, String>(2);
        datum.put("title", item.getTitle());
        datum.put("date", item.getDate().toString());
        data.add(datum);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, data,
                                              android.R.layout.simple_list_item_2,
                                              new String[] {"title", "date"},
                                              new int[] {android.R.id.text1,
                                                         android.R.id.text2});
    itemList.setAdapter(adapter);
    

    文档指出“地图包含每一行的数据,并且应该包括在 from 参数中指定的所有条目”,因此标题和日期都应该始终存在。

    请注意,这完全不是我的想法。我还没有真正测试过所有的代码,所以你很可能会遇到一些需要调整或修复的怪癖或错误。

    【讨论】:

      【解决方案2】:

      你应该有一个像这样的 item_list.xml 文件:

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:padding="6dp" >
      
          <TextView
              android:id="@+id/page_title"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:textColor="#D8000000"
              android:textSize="16sp"
              android:textStyle="bold" />
      
          <TextView
              android:id="@+id/page_date"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_below="@id/page_title"
              android:ellipsize="marquee"
              android:lines="3"
              android:marqueeRepeatLimit="marquee_forever"
              android:textColor="#D8000000"
              android:textSize="12sp" />
      
      </RelativeLayout>
      

      在你的 listadapter 方法中的 getview :

      View row = convertView;
      LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
      row = inflater.inflate(R.layout.item_list, parent, false);
      
      TextView listTitle = (TextView) row.findViewById(R.id.page_title);
       listTitle.setText(title);
      
      TextView date = (TextView) row.findViewById(R.id.page_date); 
      date.setText( <here put your date from RSS feed>);
      
      return row;
      

      应该这样做!

      【讨论】:

      • 很好,但是参数 layout_below 在 LinearLayout 中不起作用。有什么解决办法吗?
      • 我知道为时已晚,但请在 RelativeLayout 中使用 android:orientation="vertical" 并删除 android:layout_below
      【解决方案3】:

      由于您想将多个数据项映射到多个视图,因此不能使用 ArrayAdapter。您必须改用SimpleAdapter

      另外,我注意到您正在主 UI 线程上获取 RSS 提要。这将导致您的应用程序高度无响应。如果你不知道我在说什么,请看一下Painless Threading 文章(我认为它是任何安卓开发者的必读之物)。你应该做的是使用Loader。它们专为您的情况而设计。尽管它们直到 API-10 才引入,但您仍然可以通过 Support Package 在较低的 API 级别上使用它们。

      【讨论】:

      • 我支持将提要下载从 UI 线程移开的建议。只是想添加另一种方法,如果 OP 不想带来支持包的负担,可能是使用AsyncTask
      • 不要害怕支持包!它真的很容易使用,一点负担都没有:)
      • 我在哪里可以得到那篇文章
      【解决方案4】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多