关于android setlistadapter的建议,并结合两个.xml布局

时间:2015-04-21 16:58:46

标签: java android xml android-layout listadapter

我正在使用两个列表适配器,如下面的代码所示。 resultList和resultList2使用new ArrayList <HashMap<String,String>>();

进行初始化
ListAdapter adapter, adapter2;
adapter =new SimpleAdapter(
                getActivity(), resultList, 
                R.layout.list_item1, new String[]{TAG_ID, TAG_FRUIT, TAG_SPECIES},
                new int[]{R.id.Id,R.id.fruit,R.id.species});
adapter2=new SimpleAdapter(
                getActivity(), resultList2, 
                R.layout.list_item_append, new String[]{ TAG_NAME, TAG_ADDRESS, TAG_EMAIL},
                new int[]{ R.id.name, R.id.address, R.id.email});
setListAdapter(adapter);
setListAdapter(adapter2);    //this line has overriden setListAdapter(adapter)

我有两个名为list_item1.xml和list_item2.xml的布局。

//list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >

     <TextView
        android:id="@+id/Id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold"
        android:descendantFocusability="blocksDescendants"
        android:visibility="gone"/><!--android:visibility="gone"  --> 

    <TextView
        android:id="@+id/fruit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/species"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="17sp"
        android:textStyle="bold" />
</LinearLayout>

第二个布局:list_item2.xml
        list_item.xml
             

 <TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/email"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:textSize="17sp"
    android:textStyle="bold" />

我尝试在新布局中使用<include><merge>,但这不是我想要的结果。结果显示了adapter2中的所有元素。但是在每个adapter2元素之前都有空列表元素。

我的预期结果是:
适配器....
适配器....
适配器...
适配器2 ...
适配器2 ...
适配器2 ...

我怎样才能做到这一点?我认为setListAdapter是导致问题的原因。

我使用两个listadapter的原因是因为我正在尝试两次从我的数据库中获取两组数据。数据来自名为tblfruit和tblperson的表。

    $response["results"] = array();
    $response["results2"] = array();
    // temp user array
    $results = array();
    $results2=array();

    while ($row = mysql_fetch_array($result)){
        $results["id"] = $row["id"];
        $results["fruit"] = $row["fruit"];
        $results["species"] = $row["species"];
        array_push($response["results"], $results);
    }
    while ($row = mysql_fetch_array($result2)){
        $results2["name"] = $row["name"];
        $results2["address"] = $row["address"];
        $results2["email"] = $row["email"];
        array_push($response["results2"], $results2);
    }

2 个答案:

答案 0 :(得分:0)

您不需要为要实现的任何内容合并list_item1和resultList2布局。而不是扩展ListActivity或ListFragment,使用普通活动和setContentView,其布局在其xml中有两个listview,如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>

<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>

然后在你的java文件中

    ListView listview1 = (ListView) findViewById(R.id.listView1);
    ListView listview2 = (ListView) findViewById(R.id.listView2);
    listview1.setAdapter(adapter);
    listview2.setAdapter(adapter2);

我不确定这是否符合您的要求。如果它没有,我希望它有所帮助。

答案 1 :(得分:0)

扩展ListAdapter并使用适配器View.getView()方法中的所需布局为每一行充气。对于ListView中的每一行(或适配器内的记录,如果您想要特定的),此方法被调用一次。它用于为每条记录设置(或者,充分膨胀)视图,并执行诸如设置TextView的值ImageView&#39; s,为每一行隐藏不必要的组件等等。我自己没有尝试过两种不同的布局资源,但我想这可以通过这种方法来完成。

public class CustomListAdapter extends ArrayAdapter<CustomObject> {

    private Context context;
    private int layoutResourceId_1;
    private int layoutResourceId_2
    private List<CustomObject> list;

    public CustomListAdapter(Context context, int layoutResourceId_1, int layoutResourceId_2, List<CustomObject> list) {
        super(context, layoutResourceId_1, list);
        this.context = context;
        this.layoutResourceId_1 = layoutResourceId_1;
        this.layoutResourceId_2 = layoutResourceId_2;
        this.list = list;
    }

    ...

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

        LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();

        if(condition_layout_1()) {
            row = layoutInflater.inflate(layoutResourceId_1, parent, false);
            // Use a holder to prepare item
            ...
        } else {
            row = layoutInflater.inflate(layoutResourceId_2, parent, false);
            // Use a holder to prepare item
            ...
        }

        return row;
    }

    ...
}
相关问题