0

i am working on implementing list view, i have user BaseAdapter class.i am trying to setText in holder's text view field from an array list but without any success.Problem is in getView method's HashMap<String, ArrayList<String>> map = list.get(position); holder.txtFirst.setText(map.get(FIRST_COLUMN).toString()); part.
EDIT: my main activity class(postExecute method) where i am setting up key and values for list adapter class. code is as :

protected void onPostExecute(ArrayList<ArrayList<String>> result) {

        ArrayList<String> conceptID = new ArrayList<String>(result.get(0));
        ArrayList<String> conceptDesc = new ArrayList<String>(result.get(1));

        listForSearchConcepts = new ArrayList<HashMap<String,ArrayList<String>>>();
        HashMap<String, ArrayList<String>> temp = new HashMap<String,ArrayList<String>>();
        temp.put(FIRST_COLUMN,conceptID);
        listForSearchConcepts.add(temp);
        temp.put(SECOND_COLUMN,conceptDesc);
        listForSearchConcepts.add(temp);
        listviewAdapter adapter = new listviewAdapter(MainActivity.this, listForSearchConcepts);
        listView.setAdapter(adapter);
    }`   

However below is my code of adapter class :

public class listviewAdapter extends BaseAdapter
{
public ArrayList< HashMap < String,ArrayList < String > > > list;
Activity activity;

public listviewAdapter(Activity activity, ArrayList<HashMap<String,ArrayList<String>>> listForSearchConcepts) {
    super();
    this.activity = activity;
    this.list = listForSearchConcepts;
}

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

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

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

private class ViewHolder {
       TextView txtFirst;
       TextView txtSecond;
       TextView txtThird;
       TextView txtFourth;
  }


public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    // TODO Auto-generated method stub
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();

            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.listview_row, null);
                holder = new ViewHolder();
                holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
                holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            HashMap<String, ArrayList<String>> map = list.get(position);
            holder.txtFirst.setText(map.get(FIRST_COLUMN).toString());
            //holder.txtSecond.setText(map.get(SECOND_COLUMN).get(1));
            //holder.txtThird.setText(map.get(THIRD_COLUMN));
            //holder.txtFourth.setText(map.get(FOURTH_COLUMN));

        return convertView;
}

}

i am also attaching screenshot of what actually i am getting on running the application:
out put list view

any suggestion?
thanks

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
maddy
  • 4,001
  • 8
  • 42
  • 65
  • What is wrong with screenshot? How should it work? – Mikita Belahlazau Sep 24 '12 at 06:45
  • all thes ids(181000.., 19100..) should form a list row, in first row first column value should be 18000000101, in second row first column value should be 191000000104...and so on.... – maddy Sep 24 '12 at 06:50
  • Can you describe structure of your list? What does hashmap represent and what is array lists inside hasmaps? – Mikita Belahlazau Sep 24 '12 at 06:53
  • please look at my edit i have tried to describe it more clearly.. – maddy Sep 24 '12 at 07:03
  • Ah. So you get 2 lists: list of IDs and list of descriptions. And you want make pair ID - Description and show it in list view, where row shows one ID and description? – Mikita Belahlazau Sep 24 '12 at 07:08
  • @NikitaBeloglazov yes sorry for my dumbness not able to elaborate my problem, i am trying to solve it – maddy Sep 24 '12 at 07:14

2 Answers2

6

I strongly encourage you to replace your hasmaps and arraylists with classes. Java has poor capability to work with structures using map-lists. It just looks ugly in java. You can create class Concept that contain 2 fields: id and description. And in postExecute method you convert your lists to list of concepts. Something like this:

Concept class:

public class Concept {

    public String id;
    public String description;
}

onPostExecute method:

protected void onPostExecute(List<List<String>> result) {

    List<String> conceptID = result.get(0);
    List<String> conceptDesc = result.get(1);

    List<Concept> listForSearchConcepts = new ArrayList<Concept>();
    for (int i = 0; i < conceptID.size(); i++) {
        Concept concept = new Concept();
        concept.id = conceptID.get(i);
        concept.description = conceptDesc.get(i);
        listForSearchConcepts.add(concept);
    }
    ListViewAdapter adapter = new ListViewAdapter(MainActivity.this, listForSearchConcepts);
    listView.setAdapter(adapter);
}

ListViewAdapter class:

class ListviewAdapter extends BaseAdapter
{
    public List<Concept> list;
    Activity activity;

    public ListviewAdapter(Activity activity, List<Concept> listForSearchConcepts) {
        super();
        this.activity = activity;
        this.list = listForSearchConcepts;
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    private class ViewHolder {
        TextView txtFirst;
        TextView txtSecond;
        TextView txtThird;
        TextView txtFourth;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater =  activity.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.listview_row, null);
            holder = new ViewHolder();
            holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
            holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        Concept concept = list.get(position);
        holder.txtFirst.setText(concept.id);
        holder.txtSecond.setText(concept.description);

        return convertView;
    }

}
Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
1

Yeah error is occurred only because of these lines.

HashMap<String, ArrayList<String>> map = list.get(position);      
holder.txtFirst.setText(map.get(FIRST_COLUMN).toString());

HashMap<String, ArrayList<String>> map = list.get(position); 
/* this hashmap will give a hashmap of arraylist So first you have to get the arralist with its related key from the hashmap. Then you can go for getting the column from that arralist. */

/*
holder.txtFirst.setText(map.get(FIRST_COLUMN).toString());
But here you are getting the column from the hashmap that is wrong. you can get only arraylist from the hashmap with key.
*/
Hemant
  • 759
  • 4
  • 6
  • What's wrong with it? Hash map can contain array lists as columns. So `hashmap.put(FIRST_COLUMN, firstList);` And then retrieved via `hashmap.get(FIRST_COLUMN);` – Mikita Belahlazau Sep 24 '12 at 06:52
  • 1
    ya you can get it like that only but you will get arraylist from that hashmap,you are setting arraylist to textview. that is wrong. – Hemant Sep 24 '12 at 06:55
  • 1
    How do you know? :) May be it's his intention to show whole array list in text view. – Mikita Belahlazau Sep 24 '12 at 06:56
  • if he wants like that then it is a wrong way to do this. you can't assign complete arraylist to textview text like this. – Hemant Sep 24 '12 at 06:58
  • @NikitaBeloglazov no i dont want to show whole array list inside the test view.....i want to show in manner :in first row first column value should be 18000000101, in second row first column value should be 191000000104...and so on.... – maddy Sep 24 '12 at 06:59
  • @Maddy: than Hemant seems to be right. You need to get array list, not hash map and than get FIRST_COLUMN, SECOND_COLUMND and etc. But I can't understand what does hashmap contains. – Mikita Belahlazau Sep 24 '12 at 07:02
  • @Maddy Then whats wrong with your screen shot it is showing first row first column value 18000000101, in second row first column value 191000000104...and so on.... den what you really wants?? – Hemant Sep 24 '12 at 07:05
  • @Hemant but that all are in a single text view , i am trying show each values in different text views....(i am trying to use solution provided by you :get array list, not hash map) – maddy Sep 24 '12 at 07:08