我有3 String[] array
DateArray[]={"17/09/2012","18/09/2012","19/09/2012"};
Visit[]={"4","10","2"};
Distance[]={"30","100","45"};
我想在ListView中显示这个数组,我已经制作了XML我只想填充这3个值这是ListActivity
我试过
我该怎么做
用于单击我正在使用的ListView
listView.setAdapter(new ObjAdapter(this, R.layout.claimlistview, items));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = listView.getItemAtPosition(position);
TextView t1=(TextView)findViewById(R.id.ClaimDate);
if(t1!=null){
ClaimListBean mSelected;
int idx=position;
mSelected=m_adapter.getItem(idx);
String Date=mSelected.getDate();
//StringTokenizer tokens = new StringTokenizer(Date, "(");
//String first = tokens.nextToken();
//String second = tokens.nextToken();
String Visit=mSelected.getVisit();
String Distance=mSelected.getDistance();
//String EditedSecond = second.replace(")","");
Intent intent=new Intent(DRSTClaimList.this,DRSTClaimDetail.class);
intent.putExtra("Date", Date);
//intent.putExtra("Date", first);
//intent.putExtra("Day", EditedSecond);
intent.putExtra("Distance", Distance);
intent.putExtra("Visit", Visit);
startActivity(intent);
}
答案 0 :(得分:2)
您必须使用Custom listview
创建custom adapter
这些链接可以帮助您创建Listview。
答案 1 :(得分:2)
您需要使用这3个值创建对象,例如:
public class Obj{
private String date;
private String visits;
private String distance;
public Obj(String date , String visits, String distance){
this.date = date;
this.visits = visits;
this.distance = distance;
}
... getters and setters stuff...
}
然后你必须扩展arrayadapter类
public class ObjAdapter extends ArrayAdapter<Obj>{
private Context context;
private ArrayList<Obj> items;
public ObjAdapter(Context context, int layoutResId, ArrayList<Obj> data) {
super(context, layoutResd, data);
this.context = context;
this.items = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Obj o = items.get(position);
if (o != null) {
TextView date = (TextView) v.findViewById(R.id.date);
TextView visits = (TextView) v.findViewById(R.id.visits);
TextView distance = (TextView) v.findViewById(R.id.distance);
if (visits != null) {
visits.setText("Name: "+o.getVists()); }
if(date != null){
date.setText("Status: "+ o.getDate());
}
}
return v;
}
最后你必须在listView :)上调用方法setListAdapter
编辑: 在标准活动中是这样的(抱歉错误):
public void onCreate(Bundle icycle)
{
super.onCreate(icycle);
setContentView(R.layout.listactivity);
ListView listView = (ListView)findViewById(R.id.listview);
ArrayList<Obj> items = new ArrayList<Obj>();
items.add(new Obj("date", "visits"," ");
items.add(new Obj("date", "visits"," "):
items.add(new Obj("date", "visits"," "):
listView.setListAdapter(new ObjAdapter(this, R.layout.list_row, items));
}
在此之后,您在列表中有3个项目。
检查此链接: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
)
答案 2 :(得分:1)
您需要为列表视图设置自定义适配器。首先设置适配器,然后在活动中设置,但作为自己的类,设置自定义适配器
示例:
这将设置行项目(替换R.layout.layout_row
)与您的布局
listView.setAdapter(new CustomListAdapter(this, R.layout.layout_row, Visit));
然后添加此项并根据需要进行调整
private class CustomListAdapter extends ArrayAdapter<String> {
public CustomListAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
this.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.layout_row,
parent, false);
TextView dateText = (TextView) row.findViewById(whatever);
TextView visitText = (TextView) row.findViewById(whatever);
TextView distanceText = (TextView) row.findViewById(R.id.whatever);
dateText.setText(DateArray[position]);
// do the same for the other 2
return row;
}
}
应该工作得很好......
答案 3 :(得分:0)
您可以使用自定义ArrayAdapter来实现您想要的效果。
查看官方文档here。