如何在Android视图中添加条件getView(int position, View convertView, ViewGroup parent)

How to add condition in Android View getView (int position, View convertView, ViewGroup parent)

我有一个 Arraylist,其中包含一个 hashmap。 hashmap 包含主题名称、名称和值。例如 hashmap 这样的对象。(我没有为示例添加密钥。)

{"Topic A", "Name 1" , "100"}  
{"Topic A", "Name 2" , "100"}  
{"Topic A", "Name 3" , "100"}  
{"Topic A", "Name 4" , "100"}  
{"Topic B", "Name 1" , "100"}  
{"Topic B", "Name 2" , "100"}  
{"Topic B", "Name 3" , "100"}  
{"Topic B", "Name 4" , "100"}  
{"Topic C", "Name 1" , "100"}  
{"Topic C", "Name 2" , "100"}  
{"Topic C", "Name 3" , "100"}  
{"Topic C", "Name 4" , "100"}  
{"Topic D", "Name 1" , "100"}  
{"Topic D", "Name 2" , "100"}  

我想制作这样的列表视图

Topic A
=========================
name 1                100
name 2                100
name 3                100
name 4                100

Topic B
==========================
name 1                100
name 2                100
name 3                100
name 4                100


Topic C
==========================
name 1                100
name 2                100
name 3                100
name 4                100

Topic D
==========================
name 1                100
name 2                100
name 3                100
name 4                100

这是我的 getView(int position, View convertView, ViewGroup parent) 方法................

public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_view_card_type, null);

    mTopic = (TextView) v.findViewById(R.id.topic);
    mItemname = (TextView) v.findViewById(R.id.name_of_item);
    mValue = (TextView)v.findViewById(R.id.value_of_item); 

    mTopic.setText(myArrayList.get(position).get("Topic"));

    mItemname.setText(myArrayList.get(position).get(
            "Name"));
    mValue.setText(myArrayList.get(position).get("Value"));
    return v;

}

这是我的列表视图。

<?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:layout_alignParentBottom="true"
    android:background="#ffffff"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/topic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />

    <TextView
        android:id="@+id/name_of_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />

    <TextView
        android:id="@+id/value_of_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:textSize="28dp" />


</LinearLayout>

我尝试通过使用 position 值在 getView 方法中添加一个条件来做到这一点。但这让我很困惑。 请有人建议我该怎么做......................

做这样的事情,你可以做出改变,我只是添加一个示例

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_view_card_type, null);

    mTopic = (TextView) v.findViewById(R.id.topic);
    mItemname = (TextView) v.findViewById(R.id.name_of_item);
    mValue = (TextView)v.findViewById(R.id.value_of_item); 

      String topic = "";

if(!(myArrayList.get(position).get("Topic").equals(topic )))
{
     mTopic.setText(myArrayList.get(position).get("Topic"));
      topic = myArrayList.get(position).get("Topic");
}else{
mTopic.setText("");}


    mItemname.setText(myArrayList.get(position).get(
            "Name"));
    mValue.setText(myArrayList.get(position).get("Value"));
    return v;

}

试试这个-

// define this as global
String lastTopic = "";

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
        v = inflater.inflate(R.layout.list_item, null);

    TextValue tv_topic = (TextView) v.findViewById(R.id.tv_topic);
    TextValue tv_item_name = (TextView) v.findViewById(R.id.tv_item_name);
    TextValue tv_item_value = (TextView) v.findViewById(R.id.tv_item_value);

    if (!(myArrayList.get(position).get("Topic").equals(lastTopic))) {

        tvTopic.setText(myArrayList.get(position).get("Topic"));
        lastTopic = myArrayList.get(position).get("Topic");

    } else {

        tvTopic.setVisibility(View.GONE);

    }

    tv_item_name.setText(myArrayList.get(position).get("Name"));
    tv_item_value.setText(myArrayList.get(position).get("Value"));

    return v;

}