关于 android setlistadapter 和组合两个 .xml 布局的建议
Advice on android setlistadapter and combining two .xml layout
我正在使用两个列表适配器,如下面的代码所示。 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 是问题的原因。
我使用两个列表适配器的原因是因为我正在尝试从我的数据库中获取两组数据。数据是从名为 tblfruit 和 tblperson 的 table 中获取的。
$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);
}
无论您尝试 achieve.Instead 扩展 ListActivity 或 ListFragment,都不需要合并 list_item1 和 resultList2 布局,使用普通 activity 和 setContentView 布局xml 中有两个列表视图,如下所示
<?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);
我不确定这是否符合您的要求requirement.Sorry如果不符合't.I希望它有所帮助。
扩展 ListAdapter
并在适配器的 View.getView()
方法中用您想要的布局填充每一行。为 ListView 中的每一行调用此方法一次(或者记录在适配器中,如果你想具体一点的话)。它用于为每条记录设置(或者,膨胀)视图,并执行诸如设置 TextView
、ImageView
的值、为每一行隐藏不必要的组件等操作等。我自己还没有用两种不同的布局资源尝试过,但我想可以用这种方法来完成。
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;
}
...
}
我正在使用两个列表适配器,如下面的代码所示。 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 是问题的原因。
我使用两个列表适配器的原因是因为我正在尝试从我的数据库中获取两组数据。数据是从名为 tblfruit 和 tblperson 的 table 中获取的。
$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);
}
无论您尝试 achieve.Instead 扩展 ListActivity 或 ListFragment,都不需要合并 list_item1 和 resultList2 布局,使用普通 activity 和 setContentView 布局xml 中有两个列表视图,如下所示
<?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);
我不确定这是否符合您的要求requirement.Sorry如果不符合't.I希望它有所帮助。
扩展 ListAdapter
并在适配器的 View.getView()
方法中用您想要的布局填充每一行。为 ListView 中的每一行调用此方法一次(或者记录在适配器中,如果你想具体一点的话)。它用于为每条记录设置(或者,膨胀)视图,并执行诸如设置 TextView
、ImageView
的值、为每一行隐藏不必要的组件等操作等。我自己还没有用两种不同的布局资源尝试过,但我想可以用这种方法来完成。
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;
}
...
}