[英]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.