0

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 enter image description here

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);
                    }
raghav chopra
  • 827
  • 3
  • 9
  • 25
  • what have you tried? what is the relationship with XML? why do your variable start with a capital letter, they look like classes? what kind of data structure is that? why don't you have, like, an object containing a date, a number of visit and a distance? – njzk2 Sep 26 '12 at 13:35
  • i m Editing My answer and will tell u – raghav chopra Sep 26 '12 at 13:36

4 Answers4

2

You have to create a Custom listview with custom adapter these links will help you for creating Listview's.

1.ListView in Android using custom ListAdapter

2.Custom listview

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
2

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/

;)

Peter Moskala
  • 360
  • 1
  • 9
  • Here you are setting 3 String Values But i have # String Array – raghav chopra Sep 26 '12 at 13:48
  • i Didnt get u. can you provide me the getter and setter class along with constructer parameters so that it would be mor clear – raghav chopra Sep 26 '12 at 13:55
  • I've edited my post. I hope its more clear now. Sorry for language mistakes. :) – Peter Moskala Sep 26 '12 at 14:09
  • M But in your On Create function you are string value but i have String[] for all of three how can i treat with that ? – raghav chopra Sep 26 '12 at 18:00
  • do we got sure that all of three arrays always have the same size? If yes, and if first value of first array corresponds to first value in second and third array, and you can just use "for" loop. for(int i = 0; i < sizeOfAllArrays; i++) { items.add(new Obj(date[i], visits[i], distance[i]();} I assume, if You got blank field on your list, You have "" value instead of null. :) – Peter Moskala Sep 26 '12 at 18:29
  • @pPeter Yea they have the same size and corresponds to each other likse 1st value is same i will check it out Thanks for youe help ! and let u know – raghav chopra Sep 26 '12 at 18:40
  • how do u take SizeofAllArray its better to take size of 1 array because all our array's are of same size what do u say about this .. ? – raghav chopra Sep 26 '12 at 18:42
  • Its language mistake, sorry, ofc I meant to say that you have to take size of one array, because all of them are the same. Whichever array you choose to get size , it will be good. – Peter Moskala Sep 26 '12 at 19:09
  • M Hey i have succeded thanks for your concern and your help you are a great teacher :) – raghav chopra Sep 27 '12 at 02:59
  • i have more Query if i want to click the Listview i am using\ public void onListItemClick(ListView parent,View v,int position,long id){} But it is not working – raghav chopra Sep 27 '12 at 06:37
  • did u use @Override annotation ? :) – Peter Moskala Sep 27 '12 at 07:53
1

You need to setup a custom adapter for your listview. Set the adapter first, and then inside your activity, but as its own class, setup the custom adapter

Example:

This will setup the row item (replace the R.layout.layout_row) with whatever your layout is

listView.setAdapter(new CustomListAdapter(this, R.layout.layout_row, Visit));

Then add this and adjust as necessary

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

Should work just fine...

RyanInBinary
  • 1,533
  • 3
  • 19
  • 47
0

You can use a custom ArrayAdapter to achieve what you want.

Have a look at the official documentation here.

zeiger
  • 700
  • 5
  • 16