Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有两个以上值的ListView_Java_Android_Hashmap - Fatal编程技术网

Java 具有两个以上值的ListView

Java 具有两个以上值的ListView,java,android,hashmap,Java,Android,Hashmap,我有一个对象,其中包含必要的数据,它们是四个,我想将它们设置在一个列表中,列表中还有四个TextView。我使用了带有两个值的HashMap,但无法用两个以上的值计算出来 医学课程 public class Medicine { private String id; private String name; private String doctor; private String date; private String dose; publi

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

医学
课程

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;
    }
}
药品的碎片

<?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>
在以前的实现中,当我有两个值和两个文本视图时,我使用
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);
List listItems=new ArrayList();
SimpleAdapter SimpleAdapter=新SimpleAdapter(
getActivity(),listItems,R.layout.list_项,
新字符串[]{“name”,“id”},
新的int[]{R.id.text1,R.id.text2});
迭代器it=patientInfoForList.entrySet().Iterator();
while(it.hasNext()){
HashMap resultsMap=新HashMap();
Map.Entry对=(Map.Entry)it.next();
resultsMap.put(“name”,pair.getKey().toString());
resultsMap.put(“id”,pair.getValue().toString());
添加(resultsMap);
}
patientsList.setAdapter(simpleAdapter);
但是有四个值,我无法理解。我尝试使用
HashMap
,但无法
将这四个值放入映射。一个廉价的解决方案是将四个字符串连接成两个,并将文本视图从四个减少到两个。但这将是我最后的选择

使用而不是SimpleAdapter

即:

公共类MedicinesAdapter扩展了ArrayAdapter{
公共药品附录(上下文,ArrayList medicines){
超级(上下文,0,药物);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
医学=获取项目(位置);
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.list_项,父项,false);
}
TextView medicineTextView=(TextView)convertView.findViewById(R.id.medicineTextView);
TextView doseTextView=(TextView)convertView.findViewById(R.id.doseTextView);
..//查找其余视图以进行数据填充
medicineTextView.setText(medicine.getName());
doseTextView.setText(medicine.getDose());
…//填充其余视图
返回视图;
}
}

在这个问题上,这里有一个很好的指南。确保读者阅读了有关的部分。

正是我所需要的
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);
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;
    }
}