我曾尝试使用arraylist删除listview项,如果单击项的位置,但总是错误地删除了项的位置。为什么删除的项目从底部开始?如果我单击列表视图位置中的任何位置,仍从底部删除。任何机构都可以帮助解决这个问题?
这就是我从数据库中获取数据的方式
public static ArrayList<Integer> arrIdJob = new ArrayList<Integer>();
myJSON = json.getJSONArray(TAG_record);
for (int i = 0; i < myJSON.length(); i++) {
JSONObject c = myJSON.getJSONObject(i);
arrIdJob.add(Integer.parseInt(c.getString("id_jobpost")));
}
我正在通过setOnItemClickListener在onCreatView中使用片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
arrIdJob.remove(i);
adapterListHome.notifyDataSetChanged();
}
});
这是我的适配器扩展BaseAdapter
public class AdapterListHome extends BaseAdapter {
private Activity activity;
private ArrayList<CustomRowClass> listData;
private LayoutInflater layoutInflater;
private Context context;
public AdapterListHome(Context context, ArrayList<CustomRowClass> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
}
public AdapterListHome(Activity act) {
this.activity = act;
}
public int getCount() {
return HomeFragment.arrIdJob.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@SuppressLint("SetTextI18n")
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.desain_view, null);
holder = new ViewHolder();
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tvJobTitle = (TextView) convertView.findViewById(R.id.tv_jobtitile);
holder.tvCompanyName = (TextView) convertView.findViewById(R.id.tv_companyname);
holder.tvState = (TextView) convertView.findViewById(R.id.tv_state);
holder.tvSalary = (TextView) convertView.findViewById(R.id.tv_minsal);
//for key
holder.keyIdUser = (TextView) convertView.findViewById(R.id.key_iduser);
holder.keyIdJob = (TextView) convertView.findViewById(R.id.key_idjob);
holder.keyIdCompany = (TextView) convertView.findViewById(R.id.key_idcompany);
holder.keyQualification = (TextView) convertView.findViewById(R.id.key_qualification);
holder.keyDesc = (TextView) convertView.findViewById(R.id.key_desc);
//set
holder.tvJobTitle.setText(HomeFragment.arrJobTitle.get(position));
holder.tvCompanyName.setText(HomeFragment.arrCompanyName.get(position));
holder.tvState.setText(HomeFragment.arrState.get(position));
holder.tvSalary.setText(toRupiah(HomeFragment.arrMinSal.get(position))+" - "+toRupiah(HomeFragment.arrMaxSal.get(position)));
//set for key
holder.keyIdJob.setText(HomeFragment.arrIdJobStr.get(position));
holder.keyIdCompany.setText(HomeFragment.arrIdCompany.get(position));
holder.keyQualification.setText(HomeFragment.arrQualification.get(position));
holder.keyDesc.setText(HomeFragment.arrDesc.get(position));
// Picasso.get().load(Config.PATH_URL_IMG_CAT+ ListMenuActivity.IMAGE.get(position)).into(holder.imgThumb);
return convertView;
}
static class ViewHolder {
TextView tvJobTitle, tvCompanyName, tvState, tvSalary;
TextView keyIdUser, keyIdJob, keyIdCompany, keyQualification, keyDesc;
String data;
//ImageView imgThumb;
}
private String toRupiah(String nominal){
String hasil = "";
DecimalFormat toRupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols formatAngka = new DecimalFormatSymbols();
formatAngka.setCurrencySymbol("Rp. ");
formatAngka.setMonetaryDecimalSeparator(',');
toRupiah.setDecimalFormatSymbols(formatAngka);
hasil = toRupiah.format(Double.valueOf(nominal));
return hasil;
}
}
用[替换适配器中的getItem(int position)
public Object getItem(int position) {
return listData.get(position);
}
此外,请确保您引用的是适配器和ItemClickListener
中的相同列表。