0

我有 3String[] 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);
                    }
4

4 回答 4

2

您必须Custom listview使用custom adapter这些链接创建一个将帮助您创建 Listview 的。

1.Android中ListView使用自定义ListAdapter

2.自定义列表视图

于 2012-09-26T13:42:08.713 回答
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/

;)

于 2012-09-26T13:43:12.607 回答
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;
    }
}

应该可以正常工作...

于 2012-09-26T13:40:07.397 回答
0

您可以使用自定义 ArrayAdapter 来实现您想要的。

在这里查看官方文档。

于 2012-09-26T13:39:44.540 回答