简体   繁体   English

getItem(position)Android问题

[英]getItem(position) Android issue

I created a custom adapter for a ListView with a CheckBox in each row. 我为ListView创建了一个自定义适配器,在每行中都有一个CheckBox 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 . 每次我选中一个Checkbox ,我都想对ListView的所选项目触发操作,但是getItem(position)始终返回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: 编辑我在调用者类中添加了一个getItem:

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

Can you put me on the right way? 你能使我走对路吗?

EDIT 2 编辑2
Finally achieved it, by using each and every time getItem(position) instead of currentJson. 最终通过使用每次getItem(position)而不是currentJson来实现。

Please try using 请尝试使用

caller.getItem(position) 

and override getCount. 并覆盖getCount。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 始终以等于0的位置调用Android BaseAdapter方法getItem - Android BaseAdapter method getItem is always called with position equals 0 FragmentPagerAdapter Fragment getItem位置错误? - FragmentPagerAdapter Fragment getItem wrong position? 适配器的getItem(position)和setBackgroundResource - Adapter's getItem(position) and setBackgroundResource Adapter.getItem(position)使应用程序崩溃 - Adapter.getItem(position) crashes application 如何评估? (犯罪)(getListAdapter())。getItem(位置) - How is this get evaluated? (Crime)(getListAdapter()).getItem(position) getItem()如何在ViewPager的Android中工作? - How does getItem() work in Android in ViewPager? 这一行中的错误“String forecastStr = mForecastAdapter.getItem(position);” 在阳光项目中 - Error in this line " String forecastStr = mForecastAdapter.getItem(position);" in the sunshine project 为什么getItem(position)可用于新实例? (不应该为零?) - Why getItem(position) works with a new instance? (should not be zero?) 带有PagerAdapter.getItem的Android java.lang.VerifyError - Android java.lang.VerifyError with PagerAdapter.getItem ArrayList.get(int index)和ArrayAdapter.getItem(int position)方法之间有什么区别? - What is is the difference between ArrayList.get(int index) and ArrayAdapter.getItem(int position) methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM