简体   繁体   中英

getItem(position) Android issue

I created a custom adapter for a ListView with a CheckBox in each row. Each time I check a Checkbox , I want to trigger an action on the selected item in my ListView , but the getItem(position) always return the last item in the ListView .

Set the adapter:

public void onPostExecuteSearchRequestedCars(JSONArray array){
        List<JSONObject> list = new ArrayList<>(array.length());
        try {
            for(int i = 0 ; i < array.length() ; i++){
                JSONObject temp = array.getJSONObject(i);
                list.add(temp);
            }
        } catch (JSONException e) {
            Log.e(e.getClass().getName(),"There is no JSONObject in the JSONArray", e);
        }

        // Create and set the custom listView.
        adapter = new CustomSpecificCar(this, list, this);
        lv = (ListView) findViewById(R.id.lvCars);
        lv.setAdapter(adapter);
    }


Custom Adapter:

public class CustomSpecificCar extends ArrayAdapter<JSONObject>{
    ListSpecificCars caller;
    private  JSONObject currentJson;
    private CheckBox cbSelectedCar;
    private List<JSONObject> list; 

    public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){
        super(ctxt, R.layout.custom_specific_car_row, list);
        this.caller = caller;
        this.list = list; 
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        final View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false);

        // Set the reference of the layout
        currentJson                       = caller.getItem(position);
        cbSelectedCar                     = (CheckBox)customView.findViewById(R.id.cbSelectedCar);
        TextView tvBrand                  = (TextView)customView.findViewById(R.id.tvBrand);
        TextView tvModel                  = (TextView)customView.findViewById(R.id.tvModel);
        TextView tvOwnerEditable          = (TextView)customView.findViewById(R.id.tvOwnerEditable);
        TextView tvPriceEditable          = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable);


        try {
            tvBrand.setText(currentJson.getString("brand"));
            tvModel.setText(currentJson.getString("model"));
            tvOwnerEditable.setText(currentJson.getString("owner"));
        } catch (JSONException e) {
            Log.e(e.getClass().getName(), "JSONException", e);
        }



        cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    caller.updateClickedUsername(currentJson, true); // Add to the List
                    try {
                        Log.d("Has been checked ", currentJson.getString("brand"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                else
                    caller.updateClickedUsername(currentJson, false); // Delete from the List
            }
        });


        return customView;
    }



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

EDIT I added a getItem in my caller class:

public JSONObject getItem(int position){
        return list.get(position);
    }

I suspect the error is this one: I call getView() each time a JSONObject is added to the list and set the value of this object in the global variables. ( private JSONObject currentJson ).

Can you put me on the right way?

EDIT 2
Finally achieved it, by using each and every time getItem(position) instead of currentJson.

Please try using

caller.getItem(position) 

and override getCount.

@Override
public int getCount() {
    return (caller== null) ? 0 : caller.size();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM