[android]在 getView() 的位置重复后两个职位,并跳过最后两个职位

标签: listview Android
发布时间: 2016/10/10 21:36:42
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 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();
}
}

我打电话 adapteractivity 里面的凌空 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);

解决方法 1:

onResponse(JSONObject response)内, 替换

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);

里面你 adapter 类删除下面 extra 代码

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();
}

和改变 adapter 的构造函数

public PractitionersAdapter(Context context, ArrayList<PractitionersData> mergedList) {
mInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mergedList = mergedList;
}

希望它将会帮助你。

官方微信
官方QQ群
31647020