Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中添加ListView子项文本_Java_Android_Listview_Android Layout - Fatal编程技术网

Java 在Android中添加ListView子项文本

Java 在Android中添加ListView子项文本,java,android,listview,android-layout,Java,Android,Listview,Android Layout,我创建了一个RSS阅读器,它在listview中列出项目。我还想在每个项目下都有一个日期,但我不知道怎么做。我需要有人的帮助,使子项文本显示从RSS提要检索到的pubDate 这是我为班级准备的代码: public class RSSReader extends Activity implements OnItemClickListener { public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/

我创建了一个RSS阅读器,它在listview中列出项目。我还想在每个项目下都有一个日期,但我不知道怎么做。我需要有人的帮助,使子项文本显示从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);
    }
}
public类RSSReader扩展活动实现了mclicklinklistener
{
公共最终字符串RSSFEEDOFCHOICE=”http://app.calvaryccm.com/mobile/android/v1/devos";
公共最终字符串tag=“RSSReader”;
专用RSSFeed feed=null;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.main);
//去拿我们的饲料!
feed=getFeed(rssfeedof选择);
//显示用户界面
UpdateDisplay();
}
私有RSSFeed getFeed(字符串urlToRssFeed)
{
尝试
{
//设置url
URL=新URL(urlToRssFeed);
//创建工厂
SAXParserFactory=SAXParserFactory.newInstance();
//创建解析器
SAXParser parser=factory.newSAXParser();
//创建读卡器(扫描仪)
XMLReader=parser.getXMLReader();
//实例化我们的处理程序
RSSHandler-theRssHandler=新的RSSHandler();
//指派我们的处理人
setContentHandler(ThersHandler);
//通过url类获取我们的数据
InputSource is=新的InputSource(url.openStream());
//执行同步解析
parse(is);
//获取结果-应为完全填充的RSSFeed实例,或错误时为null
返回theRssHandler.getFeed();
}
捕获(异常ee)
{
//如果有问题,只需返回null
System.out.println(ee.getMessage());
System.out.println(ee.getStackTrace());
System.out.println(ee.getCause());
返回null;
}
}
公共布尔onCreateOptions菜单(菜单)
{
super.onCreateOptions菜单(菜单);
menu.add(menu.NONE,0,0,“刷新”);
Log.i(标记“onCreateOptions菜单”);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例0:
Log.i(标签,“设置RSS提要”);
返回true;
案例1:
Log.i(标签,“刷新RSS提要”);
返回true;
}
返回false;
}
私有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(“没有可用的RSS源”);
返回;
}
如果(feedtitle!=null)
feedtitle.setText(feed.getTitle());
如果(feedpubdate!=null)
feedpubdate.setText(feed.getPubDate());
ArrayAdapter=newArrayAdapter(这是android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(适配器);
itemlist.setOnItemClickListener(此);
itemlist.setSelection(0);
}
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
//Log.i(标记“单击的项”[“+feed.getItem(position.getTitle()+”]);
Intent itemintent=新的Intent(这个,showsdescription.class);
Bundle b=新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?

您应该有一个如下的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>

在方法getview的listadapter中:

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;
View行=convertView;
LayoutFlater充气器=((活动)mContext.getLayoutFlater();
行=充气机。充气(R.layout.item_列表,父项,false);
TextView listTitle=(TextView)row.findViewById(R.id.page\u title);
listTitle.setText(标题);
TextView日期=(TextView)row.findViewById(R.id.page\u日期);
date.setText();
返回行;

这应该够了

创建自定义listView

由于要将多个数据项映射到多个视图,因此不能使用ArrayAdapter。你得改用a


另外,我注意到您在主UI线程上获得了RSS提要。这将导致您的应用程序高度不响应。如果你不知道我在说什么,那就对文章(我认为它是任何Android开发者都必须阅读的)。你应该做的是使用一个。它们是专为你这种情况设计的。虽然它们直到API-10才被引入,但您仍然可以通过在较低的API级别上使用它们。

最简单的解决方案可能是用
ArrayAdapter
android.R.layout.simple\u list\u item\u 1
android.R.layout.simple\u list\u item\u 2
预定义布局。此布局由两个
TextView
组成,id分别为
android.R.id.text1
(“项”)和
android.R.id.text2
(“子项”),您需要将其作为
SimpleAdapter
工作的参考

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