首先这是我的json。
[{"MemberId":"MI001","MemberName":"Member 1"},
{"MemberId":"MI002","MemberName":"Member 2"},
{"MemberId":"MI003","MemberName":"Member 3"}]
在这里我如何将json放入适配器
@Override
public void onResponse(JSONArray response) {
try {
Model_member m_member = new Model_member();
List<String> list = new ArrayList<String>();
for(int i = 0; i < response.length(); i++){
m_member.setMember_id(response.getJSONObject(i).getString("MemberId"));
m_member.setMember_name(response.getJSONObject(i).getString("MemberName"));
Log.d("ASDF", response.getJSONObject(i).getString("MemberName"));
memberList.add(m_member);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
这是我的适配器。
public class Adapter_member extends ArrayAdapter{
private ArrayList<Model_member> mData;
public Adapter_member(Context context, ArrayList<Model_member> m_member) {
super(context, 0, m_member);
this.mData = m_member;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_member_layout, parent, false);
}
Model_member m_member = getItem(position);
TextView memberName = convertView.findViewById(R.id.membername);
memberName.setText(m_member.getMember_name());
return convertView;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Model_member getItem(int position) {
return mData.get(position);
}
}
这是我的情话
public class Model_member {
String member_id,member_name;
public String getMember_id() {
return member_id;
}
public void setMember_id(String member_id) {
this.member_id = member_id;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
@Override
public String toString() {
return getMember_name().toString();
}
}
所以我的问题是,当我输入MI00时,结果是这样的
我只看到MI003
。数据是looping
,我的意思是所有结果都是MI003
。
我如何使其正常工作?我错过了什么吗?