【问题标题】:How to add condition in Android View getView (int position, View convertView, ViewGroup parent)如何在 Android View getView 中添加条件(int position、View convertView、ViewGroup parent)
【发布时间】:2015-05-20 05:26:39
【问题描述】:

我有一个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 方法内添加一个条件。但这让我很困惑。 可以请人建议我该怎么做........

【问题讨论】:

  • 添加一些数组列表的代码..
  • 没有键你怎么能检查这个myArrayList.get(position).get("Topic")?你在哪里设置Topic作为键进入hashmap?
  • 创建您的项目文件,其中包含主题和名称和值的 1 行,然后检查主题值是否相同,然后设置主题的可见性消失,否则可见
  • 每次视图更改时都会回收列表视图中的项目。您需要在 ViewHolder 中保存视图对象。例如:javacodegeeks.com/2013/09/…
  • 'myArrayList' 有值。我检查了调试选项。没关系。我想知道如何在 'getView' 方法中添加这样的条件。

标签: android android-listview android-arrayadapter


【解决方案1】:

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

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;

}

【讨论】:

  • 我试试你的方法。但它不起作用。我觉得你的方法一定行得通。我找不到出了什么问题..
  • 你得到了什么??我也更新了答案,你也不会得到确切的答案,你需要做更多的事情
  • 我终于成功了。它仍然存在一些问题。但我认为我可以做到这一点。谢谢@Sree
【解决方案2】:

试试这个-

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

}

【讨论】:

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