getItem(position)Android问题

时间:2015-03-31 09:43:38

标签: java android listview checkbox

我为ListView创建了一个自定义适配器,每行CheckBox。每次检查Checkbox时,我都想对ListView中的所选项触发操作,但getItem(position)始终返回ListView中的最后一项。

设置适配器:

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


自定义适配器:

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

修改 我在调用者类中添加了一个getItem:

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

我怀疑错误是这样的:每次将getView()添加到列表中并在全局变量中设置此对象的值时,我会调用JSONObject。 (private JSONObject currentJson)。

你能把我放在正确的路上吗?

编辑2
最后通过使用getItem(position)而不是currentJson来实现它。

1 个答案:

答案 0 :(得分:1)

请尝试使用

caller.getItem(position) 

并覆盖getCount。

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