Android的填充的ListView用字符串数组

Android的填充的ListView用字符串数组

本文介绍了Android的填充的ListView用字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个 String []数组

  DateArray [] = {17/09/2012,18/09/2012,19/09/2012};访问[] = {4,10,2};距离[] = {30,100,45};

我要显示此数组中这样我所做的XML我只想填充这些3个值这是一个 ListActivity 一个ListView

我试图

我如何能做到这一点。

有关点击ListView中我使用

  listView.setAdapter(新ObjAdapter(这一点,R.layout.claimlistview,物品));           listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){               @覆盖
               公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3){                 对象o = listView.getItemAtPosition(位置);                    TextView的T1 =(的TextView)findViewById(R.id.C​​laimDate);                    如果(T1!= NULL){
                        ClaimListBean mSelected;
                        INT IDX =位置;
                        mSelected = m_adapter.getItem(IDX);                        字符串日期= mSelected.getDate();                        // StringTokenizer的令牌=新的StringTokenizer(日期,();
                        //字符串第一= tokens.nextToken();
                        //字符串第二= tokens.nextToken();
                        串访问= mSelected.getVisit();
                        字符串距离= mSelected.getDistance();
                        //串EditedSecond = second.replace(),);                    意向意图=新意图(DRSTClaimList.this,DRSTClaimDetail.class);
                    intent.putExtra(日期,日期);
                    //intent.putExtra(\"Date,第一);
                    //intent.putExtra(\"Day,EditedSecond);
                    intent.putExtra(距离,距离);
                    intent.putExtra(访问,访问);
                    startActivity(意向);
                    }


解决方案

您需要与那些3的值创建对象,例如:

 公共类的OBJ {
   私人字符串日期;
   私人字符串考察;
   私人字符串距离;
   公开的OBJ(日期字符串,字符串访问,字符串的距离){
      this.date =日期;
      this.visits =考察;
      this.distance =距离;
   }
   ... getter和setter的东西...
}

然后你必须扩展arrayadapter类

 公共类ObjAdapter扩展ArrayAdapter<&的OBJ GT; {    私人上下文的背景下;
    私人的ArrayList<&的OBJ GT;项目;    公共ObjAdapter(上下文的背景下,诠释layoutResId,ArrayList的<&的OBJ GT;数据){
        超级(上下文,layoutResd,数据);
        this.context =背景;
        this.items =数据;
    }   @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                视图V = convertView;
                如果(V == NULL){
                    LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    V = vi.inflate(R.layout.row,NULL);
                }
                OBJ O = items.get(位置);
                如果(O!= NULL){
                        TextView的日期=(TextView中)v.findViewById(R.id.date);
                        TextView的访问=(TextView中)v.findViewById(R.id.visits);
                        TextView的距离=(TextView中)v.findViewById(R.id.distance);
                        如果(考察!= NULL){
                              visits.setText(姓名:+ o.getVists()); }
                        如果(日期!= NULL){
                              date.setText(状态:+ o.getDate());
                        }
                }
                返回伏;
        }

在你到底有没有打电话给你的ListView :)

方法setListAdapter

编辑:
在标准活动是这样的(抱歉错误):

 公共无效的onCreate(捆绑icycle)
{
   super.onCreate(icycle);
   的setContentView(R.layout.listactivity);
   ListView控件的ListView =(ListView控件)findViewById(R.id.listview);
   ArrayList的<&的OBJ GT;项目=新的ArrayList<&的OBJ GT;();
   items.add(新的OBJ(日期,访问,);
   items.add(新的OBJ(日期,访问,):
   items.add(新的OBJ(日期,访问,):
   listView.setListAdapter(新ObjAdapter(这一点,R.layout.list_row,物品));
}

在此,你在名单上有3项。

检查此链接:

I have 3 String[] array

DateArray[]={"17/09/2012","18/09/2012","19/09/2012"};

Visit[]={"4","10","2"};

Distance[]={"30","100","45"};

I want to show this Array in a ListView like this i have made the XML i just want to populate these 3 values This is a ListActivity

i have tried to

How Can i do That

for Clicking the ListView i am using

           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);
                    }
解决方案

You need to create object with those 3 values, for example:

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...
}

and then you have to extend arrayadapter class

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;
        }

At the end you have to call method setListAdapter on your listView :)

EDIT:In standard activity is like this (sorry for bugs):

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));
}

After this, You got 3 items on the list.

Check this link:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

;)

这篇关于Android的填充的ListView用字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:15