如何在ListView中的同一行放置多个项目?

时间:2012-01-16 04:12:46

标签: android listview layout adapter

我有一个链接到自定义适配器的项目的ArrayList。这些项目按字母顺序排列。我希望我的ListView在ListView的每一行上最多有四个项目,并按字母顺序分隔行。

例如:

[Hi] [医院] [酒店] [HotSpot]

[House]

[不可能] [不大可能]

因此,多个项目将位于ListView的同一行。

有没有办法在自定义Adapter类的getView()方法中使用position变量来实现这个目的?

这是一个例子,抱歉质量: People Screen

在图像中,后备存储(如ArrayList)将包含按字母顺序排列的名称。

3 个答案:

答案 0 :(得分:5)

您必须实现CustomListView。在对象的ArrayAdapter的GetView方法上。

见下面的例子:

See this link

它真的会帮助你。

享受。 :)

答案 1 :(得分:1)

是的,在getView()方法中,其中一个参数是一个可用于发送相关视图的位置

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

答案 2 :(得分:0)

在listview的布局中放置4个textviews,并设置其值,如下面的hashMap中的getView()方法。 您可以使用getView的第一个参数来获取listview所选项的位置。 如果不想将设置值以外的任何内容打印为textview.setText(“”)

private List<HashMap<String, String>> myData = new ArrayList<HashMap<String, String>>();

    public MyArrayAdapter(Context context, int textViewResourceId, List<HashMap<String, String>> objects)
    {
        super(context, textViewResourceId, objects);
        try 
        {
            context = getContext();
            myData = objects;
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

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

    if(v == null)
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_message_give_away_volunteers, null);
    }

    HashMap<String, String> hasValues = myData.get(position);

    if(hasValues != null)
    {

        TextView txt_VolunteerName = (TextView)v.findViewById(R.id.txtVolunteerName);
        txt_VolunteerName.setText(hasValues.get(DatabaseConstant.key_USER_NAME));

        TextView txt_SkillLevel = (TextView)v.findViewById(R.id.txtSkillLevel);
        txt_SkillLevel.setText(hasValues.get(DatabaseConstant.key_SKILL_LEVEL));

    }
}