我的TextView
每个列表项都有多个ListView
个。我已经学会了编写一个我认为合适的getView
方法,但我不确定我是否使用setAdapter
来调用该方法。
private static String[] project = {"proj1","proj2"};
private static String[] workRequests = {"requirement gathering", "design"};
private static String[] startDate = {"02/21/2012","07/15/2011"};
private static String[] status = {"WIP","DONE"};
ListView mListView;
public class MyDashboardActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydashboard);
final LayoutInflater mInflater = LayoutInflater.from(this);
mListView = (ListView)findViewById(R.id.dashboardList);
mListView.setAdapter(
// How do I set the adapter?
);
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("enters");
if(convertView == null){
convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
}
((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
((TextView) convertView.findViewById(R.id.status)).setText(status[position]);
return convertView;
}
这是xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Include Action Bar -->
<include layout="@layout/actionbar_layout" />
<ListView
android:id="@+id/dashboardList"
style="@style/LeftHeaderText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@drawable/innerdashboard_bg"
android:textColor="@color/textColor" >
<TextView android:id="@+id/project" />
<TextView android:id="@+id/work_request" />
<TextView android:id="@+id/start_date" />
<TextView android:id="@+id/status" />
</ListView>
</LinearLayout>
我尝试了几种方法,但没有一种方法可行。有人可以建议在这种情况下如何设置适配器?谢谢!
答案 0 :(得分:18)
您需要实现自己的适配器。我的方法是定义一个“代表”视图的对象。
下面是一个非常简单的示例,其中包含两个TextViews
以满足您的需求。
表示视图的对象(ListView中的一行):
public class CustomObject {
private String prop1;
private String prop2;
public CustomObject(String prop1, String prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
}
public String getProp1() {
return prop1;
}
public String getProp2() {
return prop2;
}
}
接下来是自定义适配器:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<CustomObject> objects;
private class ViewHolder {
TextView textView1;
TextView textView2;
}
public CustomAdapter(Context context, ArrayList<CustomObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}
public int getCount() {
return objects.size();
}
public CustomObject getItem(int position) {
return objects.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.your_view_layout, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.id_textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.list_id_textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
}
现在,您可以在活动中定义和设置适配器:
ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
CustomAdapter customAdapter = new CustomAdapter(this, objects);
listView.setAdapter(customAdapter);
现在您只需要在对象列表中管理您的CustomObject。
如果要在ListView上重新执行修改,请不要忘记调用customAdapter.notifyDataSetChanged()
。
答案 1 :(得分:4)
您的getView()代码需要进入扩展BaseAdapter或其子类之一的类。
执行此操作的一种方法是在MyDashboardActivity中创建私有类。下面是一个快速示例(需要一些额外的代码)。您可能还希望自定义对象将要显示的所有内容关联到一个列表项中。不是多个数组,而是拥有一个自定义类型的数组,该数组具有您要跟踪的每个值的属性。
还有一件事:你的四个TextView应该进入他们自己的布局文件(参见list_item.xml here)。该项目布局文件通过自定义适配器的构造函数连接起来(我在下面的代码中添加了注释以突出显示此内容)。
protected CustomAdapter mAdapter;
public class MyDashboardActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydashboard);
final LayoutInflater mInflater = LayoutInflater.from(this);
mListView = (ListView)findViewById(R.id.dashboardList);
mAdapter = new CustomAdapter(this, <array to be adapted>);
mListView.setAdapter(mAdapter);
}
private class CustomAdapter extends ArrayAdapter<String> {
protected Context mContext;
protected ArrayList<String> mItems;
public CustomAdapter(Context context, ArrayList<String> items) {
super(context, R.layout.custom_list_item, items); // Use a custom layout file
mContext = context;
mItems = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("enters");
if(convertView == null){
convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
}
// You'll need to use the mItems array to populate these...
((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
((TextView) convertView.findViewById(R.id.status)).setText(status[position]);
return convertView;
}
}
}
答案 2 :(得分:2)
有几种方法可以做到这一点。我将告诉你我的方式包含两个布局,第一个是ListView自己的,另一个是文本应该如何显示每个列表项。
listset.xml
First([num]) OVER (Previous([rid])) as [check]
listitems.xml(您也可以将图片放在这里,这里的想法是控制)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/shipMenu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
上面我还没有艺术品,但我打算为图标添加一个ImageView(你也可以添加更多的TextView)。这是我的自定义适配器类。
ListAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/makerID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="25dp"/>
</LinearLayout>
在您的主要活动档案集
中class ListAdapter extends ArrayAdapter <String>
{
public ListAdapter(Context context, String[] values) {
super(context, R.layout.listitems, values); //set the layout that contains your views (not the one with the ListView)
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.listitems, parent, false); //same here.
String text = getItem(position);
TextView makerID = (TextView) view.findViewById(R.id.makerID);
makerID.setText(text);
return view;
}
}
将其添加到与setContentView()
相同的括号中setContentView (R.layout.listset);
修改强>
我在派对上看起来有点迟了但也许这会对某人有所帮助。