Listview可以分为两个吗?

时间:2011-11-15 09:57:34

标签: android android-listview

我正在创建一个应用程序,它使用list来以这样的方式显示员工的详细信息:

Name                  :   abcd
Age                   :   28
Phone No              :   1234567890.

我设计了2个listviews和我的列表视图(如上所示并排),

<LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/list_unit_main"/>
        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/list_unit_main"/>
    </LinearLayout>

对于第一个列表,我添加了一个arrayadapter,它显示了名称,年龄和电话号码标签。现在我只想知道是否可以将列表分为2个? 或者我是否必须制作另一个显示详细信息的列表?

5 个答案:

答案 0 :(得分:3)

不需要两个列表视图。具有多个列的ListView的一个很好的示例显示在this link

答案 1 :(得分:2)

的java:

ListView list = (ListView) findViewById(R.id.some_list);

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("first", "Name");
map.put("second", ":");
map.put("third", "abcd");
mylist.add(map);
map = new HashMap<String, String>();
map.put("first", "Age");
map.put("second", ":");
map.put("third", "28");
mylist.add(map);

columnAdapter = new SimpleAdapter(this, mylist, R.layout.row,
            new String[] {"first", "second", "third"}, new int[] {R.id.first_cell, R.id.second_cell, R.id.third_cell});
list.setAdapter(columnAdapter);

XML1:

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <TextView android:id="@+id/first_cell"
         android:layout_width="50dip"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/second_cell"
         android:layout_width="70dip"
         android:layout_height="wrap_content" android:layout_weight="1"/>

     <TextView android:id="@+id/third_cell"
         android:layout_width="60dip"
         android:layout_height="wrap_content"  android:layout_weight="1"/>
</LinearLayout>

XML2:

<ListView android:id="@+id/some_list" android:layout_width="wrap_content" android:layout_height="wrap_content">
</ListView>

然后使用某些条件表达式

以编程方式填充mylist

答案 2 :(得分:1)

为此,您不需要2个列表视图。您只需要一个带有自定义适配器的listview来显示您需要的任何内容。你可以参考这个例子。

http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html

答案 3 :(得分:0)

你不需要采取两个listview。您必须使用适配器使用自定义列表。

你必须根据需要设计一个列表元素。

答案 4 :(得分:0)

我得到了答案。这里是。我认为这会对其他人有所帮助。 在我的行布局中,我创建了一个名为 mylist.xml

的新xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    <TextView android:id="@+id/FirstColumn" 
        android:paddingTop="2dip" 
        android:paddingBottom="2dip" 
        android:textSize="7pt"
        android:layout_width="180dip" 
        android:layout_height="wrap_content" />
    <TextView android:id="@+id/SecondColumn" 
        android:paddingTop="2dip" 
        android:paddingBottom="2dip" 
        android:textSize="7pt"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />

</LinearLayout>

我写了一个适配器java文件,详细列出了我的列表视图:

package com.converter;

import static com.converter.Constant.FIRST_COLUMN;
import static com.converter.Constant.SECOND_COLUMN;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;

public class ListViewAdapter extends BaseAdapter
{
    public ArrayList<HashMap<String , String>> list;
    Activity activity;

    public ListViewAdapter(Activity activity, ArrayList<HashMap<String  , String>> list)
    {
        super();
        this.activity=activity;
        this.list=list;
    }

    public int getCount()
    {
        return list.size();
    }

    public Object getItem(int position)
    {
        return list.get(position);
    }

    public long getItemId(int position)
    {
        return 0;
    }

    private class ViewHolder
    {
        TextView txt_first, txt_Second;
    }

    public View getView(int position, View convertview, ViewGroup parent)
    {
        ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if(convertview == null)
        {
            convertview = inflater.inflate(R.layout.mylist, null);
            holder = new ViewHolder();
            holder.txt_first = (TextView)convertview.findViewById(R.id.FirstColumn);
            holder.txt_Second = (TextView)convertview.findViewById(R.id.SecondColumn);
            convertview.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertview.getTag();
        }
        HashMap<String, String> map = list.get(position);
        holder.txt_first.setText(map.get(FIRST_COLUMN));
        holder.txt_Second.setText(map.get(SECOND_COLUMN));

        return convertview;
    }
}

另一个在我的程序中指定常量的类, constant.java

package com.converter;

public class Constant 
{
    public static final String FIRST_COLUMN = "First";
    public static final String SECOND_COLUMN = "Second";
}

在我的 main.xml 文件中,我有

<LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:id="@+id/list_main"/>
    </LinearLayout>

现在我在 mylist.xml 的listview中有自己的布局,需要将该视图放在 main.xml 的列表视图中。

然后,在我的活动文件中,我有:

ListView list_activity = (ListView)findViewById(R.id.list_main);

//got adapter for mylistview(for my first column) 
//and called a function to populate as my needed view....
//do that by urself...

public void populateList(ArrayAdapter<CharSequence> adpt, int pos,int pos2)
    {
        list = new ArrayList<HashMap<String,String>>();
        double[] array = getArray(pos, pos2);//was my for my pgm for a calulation
        for(int i = 0; i < adpt.getCount();i++)
        {
            String str = adpt.getItem(i).toString();

            HashMap<String, String> temp = new HashMap<String, String>();
            temp.put(FIRST_COLUMN, str);
            temp.put(SECOND_COLUMN, Double.toString(array[i]));
            list.add(temp);
        }
        ListViewAdapter adapter = new ListViewAdapter(this, list);
        list_activity.setAdapter(adapter);
    }