刷新列表视图中的特定项目

时间:2016-08-18 11:26:27

标签: android listview listviewitem partial-page-refresh

我尝试更新列表视图中的特定项目而不刷新整个列表视图(下面清楚地解释)。对于这个要求,我几乎所有想法都可以在网上找到,但我没有得到解决方案

说明:

我在列表视图中有不同的项目(请参阅附图)。对于此列表视图,我使用模型类(POJO类)存储id和其他相关数据。在同一个项目中,我使用了推送通知。在此推送通知中,我获取了列表视图中的项目ID。根据id我必须在列表视图中显示一些图像。对于此要求,如果我刷新整个列表视图,我可以更改图像,但我不想刷新整个列表视图。 因此,我想根据我从推送通知获得的ID更改列表视图中的特定项目的图像。

图片

enter image description here

示例代码(我使用下面的示例代码填充列表视图)

活动

public class EventMyEventHosting extends AppCompatActivity {

    ArrayList<EventMyEventHostingItems> itemsaArrayList;
    EventMyEventHostingAdapter2 itemsAdapter;
    TextView tvNoEvents;
    ListView listView;
    boolean connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapt_refresh);

        itemsaArrayList = new ArrayList<EventMyEventHostingItems>();
        itemsAdapter = new EventMyEventHostingAdapter2(EventMyEventHosting.this, R.layout.lv_my_events_hosting_adapter, itemsaArrayList);
                // (EventMyEventHosting.this,itemsaArrayList);
        listView = (ListView) findViewById(R.id.lv_events_my_event_hosting);

        listView.setAdapter(itemsAdapter);

        tvNoEvents = (TextView) findViewById(R.id.tv_events_my_event_hosting_no_events);

        loadDetailsVolley();

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


                    /*Intent intent = new Intent(EventMyEventHosting.this, EventMyEventDetail.class);
                    intent.putExtra("eventID", itemsaArrayList.get(position).getEventID());
                    startActivity(intent);*/


            }

        });
    }



    public void loadDetailsVolley(){


        final ProgressDialog dialog = ProgressDialog.show(EventMyEventHosting.this, null, null);
        ProgressBar spinner = new android.widget.ProgressBar(EventMyEventHosting.this, null, android.R.attr.progressBarStyle);
        spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(spinner);
        dialog.setCancelable(false);
        dialog.show();

        String eventMyEventHostingURL = "MyApi";
        Log.e("eventMyEventHostingURL", eventMyEventHostingURL);

        StringRequest request = new StringRequest(Request.Method.POST, eventMyEventHostingURL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                dialog.dismiss();

                if(response != null && !response.startsWith("<HTML>")){

                    Log.e("eventHostingURLOnResponse", response);

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        if(jsonArray.length() > 0){

                            for(int i = 0; i < jsonArray.length(); i++){
                                JSONObject singleJsonObj = jsonArray.getJSONObject(i);

                                String postID = singleJsonObj.getString("id");
                                String title = singleJsonObj.getString("name");
                                String dateAndTime = singleJsonObj.getString("event_time");
                                String address = singleJsonObj.getString("address");
                                String isActive = singleJsonObj.getString("is_active");
                                String bgImage = singleJsonObj.getJSONObject("event_cover").getString("url");

                                EventMyEventHostingItems items = new EventMyEventHostingItems();

                                items.setEventID(postID);
                                items.setIsActive(isActive);
                                items.setTitleMyEvent(title);
                                items.setTimeMyEvent(dateAndTime);
                                items.setLocationMyEvent(address);
                                items.setImageMyEvent(bgImage);

                                itemsaArrayList.add(items);
                                itemsAdapter.notifyDataSetChanged();
                            }

                        }else {
                            tvNoEvents.setVisibility(View.VISIBLE);
                            listView.setVisibility(View.GONE);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }else{


                    Toast.makeText(EventMyEventHosting.this, "Please check your Internet connection...",Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                dialog.dismiss();

                if(error != null){

                    Log.e("error", error.toString());
                }else{


                }
            }
        });

        RequestQueue queue = Volley.newRequestQueue(EventMyEventHosting.this);
        queue.add(request);
        queue.getCache().remove(eventMyEventHostingURL);

    }
}

适配器

public class EventMyEventHostingAdapter2 extends ArrayAdapter<EventMyEventHostingItems> {
    private Context context;
    @SuppressWarnings("unused")
    private List<EventMyEventHostingItems> items;
    String eventIdForVol;
    private EventMyEventHostingAdapter2 adapter;

    /*
    To remove item from list (Deleting the particular item)

    items.remove(rowItem);
    adapter.notifyDataSetChanged();

    // This is to show the no records found message

    import static com.app.hire.MatchedCandidates.listView;
    import static com.app.hire.MatchedCandidates.tvNoRecords;

    if(items.size() == 0){
        listView.setVisibility(View.GONE);
        tvNoRecords.setVisibility(View.VISIBLE);
    }
    */



    /*
    (This is for calling the Activity or fragment method from adapter)
    ===================================================================

    http://stackoverflow.com/a/24502482

    From your activity/Fragment initialize adapter like
    ---------------------------------------------------

    customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);  //HomeFragment.this for fragment

    In your adapter
    --------------

    public CustomAdapter(Context context, int id, HomeFragment fragment) {
    this.fragment = fragment;
    }

    method calling
    --------------

    fragment.doSomething();
    */

    public EventMyEventHostingAdapter2(Context context, int resource) {
        super(context, resource);
        this.context = context;
    }

    public EventMyEventHostingAdapter2(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
        this.context = context;
    }

    public EventMyEventHostingAdapter2(Context context, int resource, EventMyEventHostingItems[] objects) {
        super(context, resource, objects);
        this.context = context;
    }

    public EventMyEventHostingAdapter2(Context context, int resource, int textViewResourceId, EventMyEventHostingItems[] objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
    }

    public EventMyEventHostingAdapter2(Context context, int resource, List<EventMyEventHostingItems> objects) {
        super(context, resource, objects);
        this.context = context;
        this.items = objects;
        this.adapter = this; 
    }

    public EventMyEventHostingAdapter2(Context context, int resource, int textViewResourceId, List<EventMyEventHostingItems> objects) {
        super(context, resource, textViewResourceId, objects);
        this.context = context;
        this.items = objects;
        this.adapter = this; 
    }

    @SuppressLint("InflateParams") @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        final EventMyEventHostingItems rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.lv_my_events_hosting_adapter, null);
            holder = new ViewHolder();
            holder.eventImage = (ImageView) convertView.findViewById(R.id.iv_my_events_hosting_bg);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_events_hosting_adpt_title);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_events_hosting_adpt_time);
            holder.tvLocation = (TextView) convertView.findViewById(R.id.tv_events_hosting_adpt_city);
            holder.rlEnable = (RelativeLayout) convertView.findViewById(R.id.rl_events_hosting_adpt_enable);
            holder.rlDisable = (RelativeLayout) convertView.findViewById(R.id.rl_events_hosting_adpt_disable);

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(rowItem.getIsActive().equalsIgnoreCase("true")){
            holder.rlDisable.setVisibility(View.VISIBLE);
            holder.rlEnable.setVisibility(View.GONE);
        }else {
            holder.rlDisable.setVisibility(View.GONE);
            holder.rlEnable.setVisibility(View.VISIBLE);
        }

        if(rowItem.getTitleMyEvent().equals(null) || rowItem.getTitleMyEvent().equals("")){
            holder.tvTitle.setText("-");
        }else {
            holder.tvTitle.setText(rowItem.getTitleMyEvent());
        }

        if(rowItem.getTimeMyEvent().equals(null) || rowItem.getTimeMyEvent().equals("")){
            holder.tvTime.setText("-");
        }else {
            holder.tvTime.setText(rowItem.getTimeMyEvent());
        }

        if(rowItem.getLocationMyEvent().equals(null) || rowItem.getLocationMyEvent().equals("")){
            holder.tvLocation.setText("-");
        }else {
            holder.tvLocation.setText(rowItem.getLocationMyEvent());
        }


        if(rowItem.getImageMyEvent().equals(null) || rowItem.getImageMyEvent().equals("")){

        }else {

            Glide.with(context).load(rowItem.getImageMyEvent()).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(holder.eventImage);

        }

        final ViewHolder finalHolder = holder;
        holder.rlDisable.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                eventIdForVol = rowItem.getEventID();

                Log.e("eventIdForVol", eventIdForVol);

                disableVolley(finalHolder);

            }
        });

        holder.rlEnable.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                eventIdForVol = rowItem.getEventID();

                Log.e("eventIdForVol", eventIdForVol);

                enableVolley(finalHolder);

            }
        });

        return convertView;
    }

    private class ViewHolder {
        ImageView eventImage;
        TextView tvTitle;
        TextView tvTime;
        TextView tvLocation;
        RelativeLayout rlEnable;
        RelativeLayout rlDisable;
    }
}

Pojo课程

public class EventMyEventHostingItems {

    private String eventID;
    private String imageMyEvent;
    private String isActive;
    private String titleMyEvent;
    private String timeMyEvent;
    private String locationMyEvent;
    private String interestedMyEvent;





    public String getEventID() {
        return eventID;
    }
    public void setEventID(String eventID) {
        this.eventID = eventID;
    }
    public String getIsActive() {
        return isActive;
    }
    public void setIsActive(String isActive) {
        this.isActive = isActive;
    }
    public String getImageMyEvent() {
        return imageMyEvent;
    }
    public void setImageMyEvent(String imageMyEvent) {
        this.imageMyEvent = imageMyEvent;
    }
    public String getTitleMyEvent() {
        return titleMyEvent;
    }
    public void setTitleMyEvent(String titleMyEvent) {
        this.titleMyEvent = titleMyEvent;
    }
    public String getTimeMyEvent() {
        return timeMyEvent;
    }
    public void setTimeMyEvent(String timeMyEvent) {
        this.timeMyEvent = timeMyEvent;
    }
    public String getLocationMyEvent() {
        return locationMyEvent;
    }
    public void setLocationMyEvent(String locationMyEvent) {
        this.locationMyEvent = locationMyEvent;
    }
    public String getInterestedMyEvent() {
        return interestedMyEvent;
    }
    public void setInterestedMyEvent(String interestedMyEvent) {
        this.interestedMyEvent = interestedMyEvent;
    }
}

2 个答案:

答案 0 :(得分:1)

我努力实现这一目标,但正如我所提到的那样,不可能实现(截至目前)。所以,我实现了这个answer

说明

当我收到推送通知时,我只是调用包含我的ListView值的API并保持ListView位置,如下面的代码。在此API调用期间,我没有显示任何进度对话框

Parcelable state;

@Override
public void onPause() {    
    // Save ListView state @ onPause
    Log.d(TAG, "saving listview state @ onPause");
    state = listView.onSaveInstanceState();
    super.onPause();
}
...

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Set new items
    listView.setAdapter(adapter);
    ...
    // Restore previous state (including selected item index and scroll position)
    if(state != null) {
        Log.d(TAG, "trying to restore listview state..");
        listView.onRestoreInstanceState(state);
    }
}

答案 1 :(得分:0)

您可以使用以下代码:

private void updateView(int index){
    View v = yourListView.getChildAt(index - 
        yourListView.getFirstVisiblePosition());

    if(v == null)
       return;

    // Views you want to change
    TextView someText = (TextView) v.findViewById(R.id.sometextview);
    someText.setText("UPDATE");
}