我有一个ListAdapter,其中getview返回的位置重复而不是增加, 如果列表的大小是4,则getview()应该从0到3但是它变为0 1 0 1,这里是代码:
public class PractitionersAdapter extends BaseAdapter {
public static class ViewHolder {
TextView header;
public TextView Pname;
public TextView otherDetails;
ImageView image;
Button switchPractitioner;
LinearLayout detailsLayout;
}
private ArrayList<PractitionersData> current = new ArrayList<>();
private ArrayList<PractitionersData> previous = new ArrayList<>();
private ArrayList<PractitionersData> mergedList = new ArrayList<>();
private LayoutInflater mInflater;
public PractitionersAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
current.add(0, new PractitionersData("Current Practitioners"));
previous.add(0, new PractitionersData("Previous Practitioners"));
}
public void addItemToCurrent(final PractitionersData item) {
current.add(item);
}
public void addItemToPrevious(final PractitionersData item) {
previous.add(item);
}
@Override
public int getCount() {
return mergedList.size();
}
@Override
public PractitionersData getItem(int position) {
return mergedList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
Log.d("position getview", String.valueOf(position));
if (convertView == null) {
v = new ViewHolder();
convertView = mInflater.inflate(R.layout.practitioners_list_item, null);
v.header = (TextView) convertView.findViewById(R.id.header_type);
v.Pname = (TextView) convertView.findViewById(R.id.practitionername);
v.otherDetails = (TextView) convertView.findViewById(R.id.practitionerdetails);
v.image = (ImageView) convertView.findViewById(R.id.practitioner_image);
v.switchPractitioner = (Button) convertView.findViewById(R.id.switch_practitioner);
v.detailsLayout = (LinearLayout) convertView.findViewById(R.id.details_layout);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
if (position != current.size() + 1 && position != 0) {
v.detailsLayout.setVisibility(View.VISIBLE);
v.Pname.setText(mergedList.get(position).name);
v.otherDetails.setText(mergedList.get(position).details);
v.image.setBackgroundResource(R.drawable.userblue);
v.switchPractitioner.setBackgroundResource(R.drawable.switch_green);
v.header.setVisibility(View.GONE);
} else {
v.header.setVisibility(View.VISIBLE);
v.detailsLayout.setVisibility(View.GONE);
v.header.setText(mergedList.get(position).name);
}
return convertView;
}
public void addItemsToMerged() {
mergedList.addAll(current);
notifyDataSetChanged();
mergedList.addAll(previous);
notifyDataSetChanged();
}
}
我在volley jsonObjectRequest的onResponse()里面的活动中调用了适配器。
practionerList= (ListView) findViewById(R.id.practitioner_list);
String url = Config.practitioners;
Log.d("url", url);
RequestQueue requestQueue = VolleySingleton.getsInstance().getmRequestQueue();
// CustomJORequest jsonObjectRequest = null;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray activePractitioners;
JSONArray previousPractitioners;
try {
activePractitioners = response.getJSONArray("active_practitioners");
for (int i = 0; i < activePractitioners.length(); i++) {
JSONObject obj = activePractitioners.getJSONObject(i);
String name = obj.getString("name");
String pract_id= obj.getString("pract_id");
Log.d("response", response.toString());
PractitionersData practitionersData = new PractitionersData(true,"sid","other details of practiotioner 1");
practitionersDatas.add(practitionersData);
}
previousPractitioners= response.getJSONArray("inactive_practitioners");
for (int i = 0; i < previousPractitioners.length(); i++) {
JSONObject obj = previousPractitioners.getJSONObject(i);
String name = obj.getString("name");
String pract_id= obj.getString("pract_id");
Log.d("response", response.toString());
PractitionersData practitionersData = new PractitionersData(false,"dis","other details of practiotioner 1");
practitionersDatas.add(practitionersData);
}
mAdapter = new PractitionersAdapter(Practitioners.this);
for(int i=0;i<practitionersDatas.size();i++){
if(practitionersDatas.get(i).isActive) {
mAdapter.addItemToCurrent(practitionersDatas.get(i));
}else{
mAdapter.addItemToPrevious(practitionersDatas.get(i));
}
}
mAdapter.addItemsToMerged();
practionerList.setAdapter(mAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("response", error.toString());
}
});
requestQueue.add(jsonObjectRequest);
答案 0 :(得分:1)
在 onResponse(JSONObject响应)内,替换
mAdapter = new PractitionersAdapter(Practitioners.this);
for(int i=0;i<practitionersDatas.size();i++){
if(practitionersDatas.get(i).isActive) {
mAdapter.addItemToCurrent(practitionersDatas.get(i));
}else{
mAdapter.addItemToPrevious(practitionersDatas.get(i));
}
}
mAdapter.addItemsToMerged();
practionerList.setAdapter(mAdapter);
带
ArrayList<PractitionersData> current = new ArrayList<>();
ArrayList<PractitionersData> previous = new ArrayList<>();
mergedList = new ArrayList<>();
for(int i=0;i<practitionersDatas.size();i++){
if(practitionersDatas.get(i).isActive) {
current.add(practitionersDatas.get(i));
}else{
previous.add(practitionersDatas.get(i));
}
}
current.add(0, new PractitionersData("Current Practitioners"));
previous.add(0, new PractitionersData("Previous Practitioners"));
mergedList.addAll(current);
mergedList.addAll(previous);
mAdapter = new PractitionersAdapter(Practitioners.this, mergedList);
practionerList.setAdapter(mAdapter);
并在适配器类中删除下面的额外代码
private ArrayList<PractitionersData> current = new ArrayList<>();
private ArrayList<PractitionersData> previous = new ArrayList<>();
public void addItemToCurrent(final PractitionersData item) {
current.add(item);
}
public void addItemToPrevious(final PractitionersData item) {
previous.add(item);
}
public void addItemsToMerged() {
mergedList.addAll(current);
notifyDataSetChanged();
mergedList.addAll(previous);
notifyDataSetChanged();
}
并将适配器构造函数更改为
public PractitionersAdapter(Context context, ArrayList<PractitionersData> mergedList) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mergedList = mergedList;
}
希望它会帮助你。