我用5个textviews制作了goal_item.xml。
我有目标课,几乎是空白。它有要填充的ListView。
public class Goal extends Activity {
ListView lvgoal;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Цели");
setContentView(R.layout.goal);
lvgoal= (ListView) findViewById(R.id.lv_goal);
}
goal.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="vertical" >
<ListView
android:id="@+id/lv_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
我有5个String数组来填充listview的textviews:
String [] name,sum,member,date,id。
如何用这5个填充ListView?
答案 0 :(得分:2)
您必须实现自己的适配器。
首先使用5个textviews创建一个新布局。 (example.xml
)
然后实现适配器类。
如果5个数组相关,我建议您执行以下操作:
class Data {
public String name;
public String sum;
public String member;
public String date;
public String id;
}
并将数据保存在一个独特的数组中:
Data[] data = new Data[x];
data[0] = new Data(); //you have naturally to initialize each element :)
data[0].name = "abc";
data[0].sum = "100";
//...etc...
创建适配器会更简单。
class MyAdapter extends ArrayAdapter<Data> {
public MyAdapter(Context context, int textViewResourceId, Data[] objects) {
super(context, textViewResourceId, objects);
}
public MyAdapter(Context context, int textViewResourceId, List<Data> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.example, null);
TextView t1 = (TextView)convertView.findViewById(R.id.t1);
TextView t2 = (TextView)convertView.findViewById(R.id.t2);
TextView t3 = (TextView)convertView.findViewById(R.id.t3);
TextView t4 = (TextView)convertView.findViewById(R.id.t4);
TextView t5 = (TextView)convertView.findViewById(R.id.t5);
Data d = getItem(position);
t1.setText(d.name);
t2.setText(d.sum);
t3.setText(d.member);
t4.setText(d.date);
t5.setText(d.id);
return convertView;
}
}
然后你必须填写列表视图:
Data[] data;
ListView list = (ListView)findViewById(R.id.lv_goal);
//...
MyAdapter adapter = new MyAdapter(context, R.layout.example,data);
list.setAdapter(adapter);
答案 1 :(得分:1)