在视图持有人文本视图中设置文本从arraylist

时间:2012-09-24 06:36:16

标签: android arraylist hashmap baseadapter

我正在实现列表视图,我有用户BaseAdapter class.i我试图从数组列表中的holder文本视图字段中设置文本但没有任何成功.Problem在getView方法的HashMap<String, ArrayList<String>> map = list.get(position); holder.txtFirst.setText(map.get(FIRST_COLUMN).toString());部分。 /> 修改 我的主要活动类( postExecute方法),我在其中设置列表适配器类的键和值。代码如下:

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

但下面是我的适配器类代码:

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

}

我还附上了运行该应用程序的实际截图:
out put list view

任何建议?
感谢

2 个答案:

答案 0 :(得分:5)

我强烈建议你用课程替换你的人字形图和arraylists。 Java使用地图列表处理结构的能力很差。它在java中看起来很难看。您可以创建包含2个字段的类Conceptiddescription。在postExecute方法中,您可以将列表转换为概念列表。像这样:

概念类:

public class Concept {

    public String id;
    public String description;
}

onPostExecute方法:

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

}

答案 1 :(得分:1)

是因为这些行而发生错误。

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