1

在我的应用程序中,我正在解析 XML 中的数据。并将它们映射到哈希映射中,最后设置为“ArrayList”。

下面是我的代码:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
      TaplistingParser parser = new TaplistingParser();
      String xml= parser.getXmlFromUrl(URL);
      Document doc=parser.getDomElement(xml);        
//      System.out.println("sssss="+doc);
      NodeList nl=doc.getElementsByTagName("article");
      final String[] url= new String[nl.getLength()];
      for(int i=0; i < nl.getLength(); i++ )
      {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        map.put("Title", parser.getValue(e, "title"));       -------->
        map.put("Date", parser.getValue(e, "create_date"));  -------->Here is the array

        url[i]=parser.getValue(e, "url");
//          map.put("URL", parser.getValue(e, "url"));
        menuItems.add(map);
//          System.out.println("items="+menuItems);
      }  
//      System.out.println("items="+menuItems);
      ListView l1= (ListView)findViewById(R.id.list);
      ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.homelistrow,
            new String[] {"Title"}, new int[] 
                    {
                    R.id.name_label});              
            l1.setAdapter(adapter);        

现在在上面的代码中我有日期和标题。

我必须像这样显示列表:

2012-11-09
qqqqqqqqqqqqqqqq
------------------
2012-11-09
ddddddddddddddd
-----------------

如何在列表中的 2 个文本视图中显示 2 个数组。我是新手,请帮助我。提前致谢。

4

1 回答 1

0

您应该实现自己的适配器,它将扩展 ArrayAdapter。

然后在其中,您的 getView() 方法应如下所示

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;

    if (row == null)
    {
        // ROW INFLATION
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.buddy_list_item, parent, false);
    }

    // Get item
    Buddy buddy = getItem(position);
    buddy.refresh();

    buddyName = (TextView) row.findViewById(R.id.buddy_name);
    buddyName.setText(buddy.toString()); //set the first line somehow

    buddyStatus = (TextView) row.findViewById(R.id.buddy_mood);
    buddyStatus.setText(buddy.getMood());  //set the second line

    return row;
}

然后您需要一个 buddy_list_item.XML 文件,该文件将包含两个 TextView - buddy_name 和 buddy_mood。我认为您可以使用 simple_list_2,但我是这样做的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckedTextView
        android:id="@+id/buddy_name"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:checkMark="?android:attr/textCheckMark"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:text="@string/buddy_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/buddy_mood"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/empty_string"
        android:layout_marginLeft="-350dp"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|bottom"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

现在只需弄清楚如何充分利用它:)

编辑:

PS你知道你可以给适配器任何它需要的东西吗?所以你甚至可以这样做

public YourArrayAdapter(Context context, int textViewResourceId, HashMap yourMap)
{
    super(context, textViewResourceId, objects);
    this.context = context;
    this.buddies = objects;
    //do something with the map or delegate demapping to the adapter
}

然后稍后在 getView() 中,您实际上执行了从地图中获取数据所需的操作,而不是从地图中获取数据并从中制作 ArrayAdapter。

于 2012-11-09T10:43:35.943 回答