【问题标题】:Listview with 2 textviews in a single row单行有 2 个文本视图的列表视图
【发布时间】:2012-11-09 10:10:01
【问题描述】:

在我的应用程序中,我正在解析 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 个数组。我是新手,请帮助我。提前致谢。

【问题讨论】:

  • 你需要什么 2 阵列?只需创建一些具有两个属性的类 - 标题、日期,然后创建 List,将 XML 中的数据解析到该对象,并使用 List 创建一些适配器的子类。
  • 谢谢骗子..但是在我上面的代码中..你能说我必须做什么来完成这个..
  • 您必须有一个扩展基本适配器的自定义适配器.....然后将该自定义适配器用于您的列表视图..它不应该是一个简单的列表适配器...+1 到@Nunu跨度>
  • 考虑为列表中的每一行创建一个DataItem 类来将相关的数据片段保存在一起。然后将DataItem 对象的数组/集合与ListView 的适配器一起使用。

标签: android list


【解决方案1】:

您应该实现自己的适配器,它将扩展 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>

现在就想办法好好利用它吧:)

编辑:

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

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2013-02-02
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多