Listview重复动作?

时间:2017-12-16 13:53:22

标签: android listview-adapter

我有清单。一行是内容图像和文本和图标,用于制作行的图标很重要。当我点击图标时,图标的颜色会发生变化。一切都是可行的,但是当我点击一个图标时,多个图标就会受到影响。

我在这里看到更多帖子没有修复问题,我使用查看器类也没有修复,可以任何身体帮助我

此类保存元素

public class ItemsList{
    private String imgUrl , comName ;
    private int isStr ;

    public ItemsList(){

    }

    public ItemsList(String imgUrl,String comName,int isStr) {
        this.imgUrl = imgUrl;
        this.comName = comName;
        this.isStr = isStr;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getComName() {
        return comName;
    }

    public void setComName(String comName) {
        this.comName = comName;
    }

    public int getIsStr() {
        return isStr;
    }

    public void setIsStr(int isStr) {
        this.isStr = isStr;
    }

}

这是我的观点持有者类

public class ViewHolder {
    public NetworkImageView imgInv;
    public TextView comName;
    public ImageView isStr;

}

这是我的适配器

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<ItemsList> itemsList;
    ImageLoader imageLoader = AppController.getmInstance().getmImageLoader();

    public ListAdapter(Activity activity, List<ItemsList> itemsList) {
        this.activity = activity;
        this.itemsList= itemsList;
    }

    @Override
    public int getCount() {
        return itemsList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        if(inflater == null){
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        final ViewHolder viHold ;
        final ItemsList it;
        if(view == null){
            view = inflater.inflate(R.layout.tst_list_row,null);
            viHold = new ViewHolder();
            viHold.comName = view.findViewById(R.id.com_name);
            viHold.imgInv = view.findViewById(R.id.tst_img);
            viHold.isStr = view.findViewById(R.id.img_str);

            view.setTag(viHold);
        }else{
            viHold = (ViewHolderInv) view.getTag();
        }
        if(imageLoader == null){
            imageLoader = AppControllerProf.getmInstance().getmImageLoader();
        }

        it = itemsList.get(position);
        viHold.comName.setText(it.getComName());
        viHold.imgInv.setImageUrl(Constant.URL_INV_IMG+it.getImgUrl(),imageLoader);

        // is Stars
        if (it.getIsStr() == 1) {
            viHold.isStr.setImageResource(R.drawable.ic_star);
        } else {
            viHold.isStr.setImageResource(R.drawable.ic_star_border);
        }

        viHold.isStr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (it.getIsStr() == 1) {
                    viHold.isStr.setImageDrawable(null);
                    viHold.isStr.setImageResource(R.drawable.ic_star_border);
                    it.setIsStr(0);
                } else {
                    viHold.isStr.setImageDrawable(null);
                    viHold.isStr.setBackgroundResource(R.drawable.ic_star);
                    it.setIsStr(1);
                }
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        return view;

    }


}

我使用Volley库来填充列表 我的活动

public class InboxActivity extends AppCompatActivity{
// Log tag
    private static final String TAG = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private List<ItemsList> itemsList= new ArrayList<ItemsList>();
    private ListView listView;
    private ListAdapter listAdapter;

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

        inboxOnCreate();
    }

    private void inboxOnCreate() {
        listView = (ListView) findViewById(R.id.inv_list_inbox);
        listAdapter = new ListAdapter(this, itemsList);
        listView.setAdapter(listAdapter);

        final int idShare = SharedPrefManager.getUserID();

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        Map<String, String> params = new HashMap<String, String>();
        params.put("id", String.valueOf(idShare));

        CustomRequest invReq = new CustomRequest(
                Request.Method.POST,
                Constant.URL_INBOX,
                params,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int count = 0; count < response.length(); count++) {
                            try {
                                JSONObject obj = response.getJSONObject(count);

                                ItemsList Items = new ItemsList();

                                Items.setImgUrl(obj.getString("com_img"));
                                Items.setComName(obj.getString("com_name"));
                                Items.setIsStr(obj.getInt("isStr"));

                                itemsList.add(Items);

                            }// end try
                            catch(JSONException e) {
                                e.printStackTrace();
                            }// end catch
                        }//end for loop

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        listAdapter.notifyDataSetChanged();

                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();
            }
        });

        AppControllerProf.getmInstance().addToRequestQueue(invReq);

    }// end inboxOnCreate
}

1 个答案:

答案 0 :(得分:0)

我认为适配器中的一切都很好。问题可能在您使用此适配器的活动类中,您是否可以在使用此适配器的地方显示活动代码?

<强>更新 您是否尝试view.findViewById(R.id.img_str).setImageResource(...代替viHold.isStr.setImageResource(...