0

我正在实现列表视图,我有用户 BaseAdapter 类。我正在尝试从数组列表中在持有者的文本视图字段中设置文本,但没有任何成功。问题出在 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;
}

}

我还附上了我实际运行应用程序的屏幕截图:
输出列表视图

有什么建议吗?
谢谢

4

2 回答 2

6

我强烈建议你用类替换你的 hasmaps 和 arraylists。Java 使用映射列表处理结构的能力很差。它在java中看起来很丑。您可以创建Concept包含 2 个字段的类:iddescription. 在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;
    }

}
于 2012-09-24T07:27:31.383 回答
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.
*/
于 2012-09-24T06:47:06.387 回答