我从ArrayLists
Contacts
填充了两个Content Provider
:
public class AddBuddiesActivity extends ListActivity
{
public ArrayList<String> allNames;
public ArrayList<String> allNumbers;
protected void onCreate(Bundle savedInstanceState)
{
// initialize and populate these lists
...
我设置了一个xml布局来准备显示两个lists
。
group_addbuddies.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:orientation="horizontal" >
<TextView
android:id="@+id/buddy_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/buddy_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
现在我想为adapter
设置ListActivity
,这样我就可以在两个text views
// inside onCreate()
// somehow create an adapter
setListAdapter(adapter);
如何创建custom list adapter
buddy_name
ArrayList<String> allNames
buddy_number
和ArrayList<String> allNumbers
{/ 1}}?
答案 0 :(得分:2)
您应该将两个数组合并为一个数组。它将为您节省很多麻烦,并且更容易适应框架。只需创建一个简单的类来容纳两个字符串(或使用Pair对象,但它不太适合未来,不推荐)。
基于该自定义数据类创建一个类型化数组适配器,覆盖getView并将每个字符串放在相应的TextView中,您就可以了。
class CustomAdapter extends ArrayAdapter<MyData>{
public CustomAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyData item = getItem(position);
View view = super.getView(position, convertView, parent);
((TextView)view.findViewById(R.id.buddy_name)).setText(item.getName());
((TextView)view.findViewById(R.id.buddy_number)).setText(item.getNumber());
return view;
}
}
答案 1 :(得分:1)
像这样设置ListAdapter
setListAdapter(new CustomListAdapter(getApplicationContext(), allNames, allNumbers));
这是Adapter类
<强> CustomListAdapter.java 强>
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context mContext;
public ArrayList<String> allNames;
public ArrayList<String> allNumbers;
public CustomListAdapter(Context mContext, ArrayList<String> allNames,
ArrayList<String> allNumbers) {
super(mContext, R.layout.group_addbuddies, allNames);
this.mContext = mContext;
this.allNames = allNames;
this.allNumbers = allNumbers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_addbuddies, parent, false);
mHolder = new ViewHolder();
mHolder.buddy_name = (TextView) convertView
.findViewById(R.id.buddy_name);
mHolder.buddy_number = (TextView) convertView
.findViewById(R.id.buddy_number);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.buddy_name.setText(allNames.get(position));
mHolder.buddy_number.setText(allNumbers.get(position));
return convertView;
}
private class ViewHolder {
private TextView buddy_name;
private TextView buddy_number;
}
}