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);
                    }
2
  • 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? Commented Sep 26, 2012 at 13:35
  • i m Editing My answer and will tell u Commented Sep 26, 2012 at 13:36

4 Answers 4

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/

;)

Sign up to request clarification or add additional context in comments.

11 Comments

Here you are setting 3 String Values But i have # String Array
i Didnt get u. can you provide me the getter and setter class along with constructer parameters so that it would be mor clear
I've edited my post. I hope its more clear now. Sorry for language mistakes. :)
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 ?
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. :)
|
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

Comments

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

Comments

0

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

Have a look at the official documentation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.