繁体   English   中英

使用带有自定义 baseadapter 的 arraylist 错误地删除列表视图项位置

[英]Wrong remove listview item position using arraylist with custom baseadapter

当我单击项目位置时,我尝试使用ArrayList删除ListView项目。 但是,它总是错误地删除项目位置。 为什么从底部删除项目? 如果我单击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")));
}

我在onCreateView使用片段和setOnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        arrIdJob.remove(i);
        adapterListHome.notifyDataSetChanged();
    }
});

这是我扩展BaseAdapter Adapter

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

看起来您正在使用一个包含整数的ArrayList并且您正在尝试通过项目的索引删除项目。 ArrayList.remove()函数获取要从列表中删除的indexelement 在您的情况下,因为它们都是int ,这应该通过此处提供的索引从您的ArrayList删除元素 - arrIdJob.remove(i); . 我认为这很好。

我看到的问题是您的数据结构以及您如何保存数据以便在ListView显示它们。 你有很多ArrayList在你的S HomeFragment及从该数据arrIdJob实际上需要其他ArrayList s到清理自己太。 因为它们都被引用并因此在您的适配器中使用。

因此,在您的情况下, arrIdJob删除 id 是不够的,因为您还必须从其他列表中删除该特定项目才能从列表中完全删除该项目。 因此,您的项目点击侦听器的实现可能如下所示。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        arrIdJob.remove(i);

        // Clear the other lists!
        arrJobTitle.remove(i);
        arrCompanyName.remove(i);
        arrState.remove(i);
        arrMinSal.remove(i);
        arrIdJobStr.remove(i);
        arrQualification.remove(i);
        arrIdCompany.remove(i);
        arrDesc.remove(i);

        adapterListHome.notifyDataSetChanged();
    }
});

删除所有这些实际上应该从列表中完全删除该项目,并且您应该摆脱任何异常行为。

我还建议以面向对象的方式设置您的数据。 话虽如此,我宁愿创建一个如下所示的对象。

public class CompanyAndJob {
    public int jobId; 
    public int companyName; 
    public int jobStr;
    public int qualification;
    // .... Other items
}

然后,我将创建一个要通过适配器的构造函数传递给适配器的CompanyAndJob对象List 这将使整体实现更加模块化。

此外,如果您获得了我建议的对象列表,您可能还需要修改适配器的其他部分。 比如你需要修改getItem如下。

public CompanyAndJob getItem(int position) {
    return listOfCompanyAndJobs.get(position);
}

我希望你能明白。

将 Adapter 中的getItem(int position)替换为

public Object getItem(int position) {
    return listData.get(position);
}

此外,请确保您在适配器和ItemClickListener引用相同的列表。

暂无
暂无

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

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