具有两个以上值的ListView

时间:2017-11-15 15:41:15

标签: java android hashmap

我有一个对象,其中包含四个必要的数据,我想将它们设置在一个包含四个TextView的列表中。我使用了HashMap两个值,但无法用两个以上的数字来表示。

Medicine上课

public class Medicine {
    private String id;
    private String name;
    private String doctor;
    private String date;
    private String dose;

    public Medicine() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDoctor() {
        return doctor;
    }

    public void setDoctor(String doctor) {
        this.doctor = doctor;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDose() {
        return dose;
    }

    public void setDose(String dose) {
        this.dose = dose;
    }
}

fragment药物

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/medicinesList"
            android:layout_height="0dip"
            android:layout_width="match_parent"
            android:layout_weight="1" />

        <Button android:id="@+id/btn"
            android:text="@string/addmedButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</RelativeLayout>

list包含四个文字视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/medicineTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/doseTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/doctorTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/dateTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />
    </LinearLayout>


</LinearLayout>

在我之前的实施中,当我有两个值和两个文本视图时,我使用HashMap做了类似的事情

List<HashMap<String,String>> listItems = new ArrayList<>();
SimpleAdapter simpleAdapter = new SimpleAdapter(
    getActivity(),listItems,R.layout.list_item,
    new String[]{"name","id"},
    new int[]{R.id.text1,R.id.text2});

Iterator it = patientInfoForList.entrySet().iterator();
while (it.hasNext()) {
  HashMap<String,String> resultsMap = new HashMap<>();
  Map.Entry pair = (Map.Entry) it.next();
  resultsMap.put("name",pair.getKey().toString());
  resultsMap.put("id",pair.getValue().toString());
  listItems.add(resultsMap);
}

patientsList.setAdapter(simpleAdapter);

但有四个值,我无法弄明白。我尝试使用HashMap<String,Medicine>,但我无法put地图上的四个值。一个廉价的解决方案是将四个字符串连接成两个,并将文本视图从四个减少到两个。但这将是我的最后选择。

1 个答案:

答案 0 :(得分:2)

使用ArrayAdapter代替SimpleAdapter。

I.e:

public class MedicinesAdapter extends ArrayAdapter<Medicine> {
    public MedicinesAdapter(Context context, ArrayList<Medicine> medicines) {
        super(context, 0, medicines);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Medicine medicine = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }
        TextView medicineTextView = (TextView) convertView.findViewById(R.id.medicineTextView);
        TextView doseTextView = (TextView) convertView.findViewById(R.id.doseTextView);
        .... // Lookup  the rest of views for data population

        medicineTextView.setText(medicine.getName());
        doseTextView.setText(medicine.getDose());
        ... // Populate the rest of the views 
        return convertView;
    }
}

这是一个关于这个主题的好指南的link。请务必阅读有关ViewHolder pattern

的部分