将标题部分添加到我的listview ...如何根据我的代码添加这些部分

时间:2014-03-31 14:16:53

标签: android android-listview baseadapter

我有一个Android应用程序,它有一个带有图像和文本的自定义列表视图,并且可以正常工作,但我想添加一个标题部分来分隔两个项目。

我一直在寻找好几天而没有任何成功可以帮助我吗?

这是我的清单:enter image description here

我需要在第二个标题部分将位于西班牙国旗“B组”之前的第一个项目之前添加值为“A组”的标题部分

所以有人可以帮助我吗?

这是列表视图的xml FILE

(activity_group_list.xml)

**<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GroupList" >
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>**
项目行的

xml文件

row_list_group

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/algeria_flag" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:ems="10" />

</RelativeLayout>

CustomAdapter.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    private static ArrayList<ItemDetails> itemDetailsarrayList;

    LayoutInflater layoutInflater;
    String[] teamName;
    int[] teamFlag;
    Context context;

    public CustomAdapter(ArrayList<ItemDetails> result, Context c) {
        itemDetailsarrayList = result;
        context = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemDetailsarrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return itemDetailsarrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflater.inflate(R.layout.row_list_group, parent,
                false);
        TextView txt = (TextView) row.findViewById(R.id.textView1);
        ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
        txt.setText(itemDetailsarrayList.get(position).getName());
        imgv.setImageResource(itemDetailsarrayList.get(position).getImage());

        return row;
    }

}

//在GroupList文件中我需要为组添加String数组作为名称的String数组

GroupList.java

package com.devleb.expandablelistdemo3;

import java.lang.reflect.Array;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.view.Menu;
import android.widget.ListView;

public class GroupList extends Activity {

    int[] img = { R.drawable.brazil_flag, R.drawable.croatian_flag,
            R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
            R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
            R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
            R.drawable.cote_divoire_flag, R.drawable.japan,
            R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
            R.drawable.england_flag, R.drawable.italy_flag,
            R.drawable.switzerland, R.drawable.ecuador_flag,
            R.drawable.france_flag, R.drawable.honduras_flag,
            R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
            R.drawable.nigeria_flag, R.drawable.germany_flag,
            R.drawable.portugal, R.drawable.ghana_flag,
            R.drawable.united_states_flag, R.drawable.belgium_flag,
            R.drawable.algeria_flag, R.drawable.russia_flag,
            R.drawable.korea_flag };
    String[] name = { "BRA", "CRO", "MEX", "CMR", "ESP", "NED", "CHI", "AUS",
            "COL", "GRE", "CIV", "JPN", "URU", "CRC", "ENG", "ITA", "SUI",
            "ECU", "FRA", "HON", "ARG", "BIH", "IRN", "NGA", "GER", "POR",
            "GHA", "USA", "BEL", "ALG", "RUS", "KOR" };

    ItemDetails item_details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        ArrayList<ItemDetails> result = getList();
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(new CustomAdapter(result, getApplicationContext()));

    }

    private ArrayList<ItemDetails> getList() {
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
        for (int i = 0; i < name.length; i++) {
            item_details = new ItemDetails();
            item_details.setName(name[i]);
            item_details.setImage(img[i]);
            results.add(item_details);
        }
        return results;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.group_list, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:2)

使用您当前的代码,尝试稍微修改您的数组:

int[] img = { null, R.drawable.brazil_flag, R.drawable.croatian_flag,
        R.drawable.mexico_flag, R.drawable.cameroon_flag, null, R.drawable.spain,
        R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
        R.drawable.australia, null, ... };

String[] name = { "Group A", "BRA", "CRO", "MEX", "CMR", "Group B","ESP", "NED", "CHI", "AUS", ... };

然后在适配器的getView方法中执行以下操作:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = null;
    /// inflate a header view here
    if (itemDetailsarrayList.get(position).getImage()==null) {
            row = layoutInflater.inflate(R.layout.row_group_header, parent,
            false);
            TextView txt = (TextView) row.findViewById(R.id.groupTitle);
            txt.setText(itemDetailsarrayList.get(position).getName());
    /// Inflate your regular item row here.
    } else {
            row = layoutInflater.inflate(R.layout.row_list_group, parent,
            false);
            TextView txt = (TextView) row.findViewById(R.id.textView1);
            ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
            txt.setText(itemDetailsarrayList.get(position).getName());
            imgv.setImageResource(itemDetailsarrayList.get(position).getImage());
    }
    return row;
}

修改

/// FILE: res/layout/row_group_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#000"
    android:padding="5dip" >

    <TextView
        android:id="@+id/groupTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:textColor="#FFF"
        android:ems="10" />

</RelativeLayout>

注意图像数组上的Null和名称数组中的“Group X”。