raghav chopra
raghav chopra

Reputation: 837

Android Fill ListView With String Array

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

Upvotes: 0

Views: 8977

Answers (4)

RyanInBinary
RyanInBinary

Reputation: 1547

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

Upvotes: 1

Peter Moskala
Peter Moskala

Reputation: 360

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/

;)

Upvotes: 2

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

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

Upvotes: 2

zeiger
zeiger

Reputation: 700

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

Have a look at the official documentation here.

Upvotes: 0

Related Questions