Santhosh
Santhosh

Reputation: 5016

How to load data from multiple arrays to listview?

Here is my listview structure:

________________________

Job No : 
Address : 

________________________

Job No : 
Address : 

________________________

Job No : 
Address : 

________________________

all the Job no's are stored in one array and addresses are stored in another array. How do i load those arrays into my listview. Can anyone help me. Please

Upvotes: 0

Views: 711

Answers (3)

Raechel Moermond
Raechel Moermond

Reputation: 80

Create an adapter for your listview, much like sandy suggested, then call the adapter in your activity. Here's an example of one of my adapters:

public class DictionaryListAdapter extends BaseAdapter {

private static ArrayList<Term> termsList;

private LayoutInflater mInflater;

public DictionaryListAdapter (Context ctx, ArrayList<Term> results){
    termsList = results;
    mInflater = LayoutInflater.from(ctx);
}

public int getCount() {
    // TODO Auto-generated method stub
    return termsList.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return termsList.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if (convertView == null){
        convertView = mInflater.inflate(R.layout.dictionarylistinflater, null);
        holder = new ViewHolder();
        holder.tvTerm = (TextView) convertView.findViewById(R.id.tvTerm);
        holder.tvAbbr = (TextView) convertView.findViewById(R.id.tvAbbreviation);

        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvTerm.setText(termsList.get(position).getWord());
    holder.tvAbbr.setText(termsList.get(position).getAbbr1() + "   " +termsList.get(position).getAbbr2());

    return convertView;
}

static class ViewHolder{
    TextView tvTerm;
    TextView tvAbbr;
}

}

And here's how I called it in the activity:

//set the listview
    final ListView lvTerms = getListView();
    lvTerms.setAdapter(new DictionaryListAdapter(this, terms));
    lvTerms.setTextFilterEnabled(true);

I would also take Stefan's advice and combine it into one array. "terms" for me is an array list of terms, and each term has a full name, two abbreviations, a definition, and a formula. Good luck!

Upvotes: 0

Stefan
Stefan

Reputation: 4705

I recommend to first merge the arrays to one array containing objects which hold the complete information for one row of your list view. Alternatively you may use a CursorAdapter if the information comes from a database.

Then create a ViewBinder and connect it to your adapter (setViewBinder()). Which one depends on your adapter. There is e.g. a SimpleCursorAdapater.ViewBinder or a SimpleAdapter.ViewBinder and so on. In the ViewBinder's setViewValue() method you populate the fields of the row in the list view. In your example there are two fields, one for the Job No. and one for the address. For this to work you need to create a custom layout for the list items containing those fields. The layout is usually set in the adapter's constructor.

More information about ViewBinder is available in the Android developer docs.

Upvotes: 0

ngesh
ngesh

Reputation: 13501

Create Custom adapter..

A pseudo code looks like below..

Class Adpter extends BasAdapter{
String[] one;
String[] two;
public Adpter(String[] one, String[] two){
this.one = one;
this.two= two;
}

public getView(convertView){

text1.setText(one[position]);
text12.setText(two[position]);

}

}

Upvotes: 3

Related Questions