简体   繁体   English

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

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

I tried to remove the ListView item using ArrayList when I click item position.当我单击项目位置时,我尝试使用ArrayList删除ListView项目。 However, it always gets wrong removed item position.但是,它总是错误地删除项目位置。 Why the item is deleted from the bottom?为什么从底部删除项目? If I click anywhere in ListView , the position still removes from the item from the bottom.如果我单击ListView任意位置,该位置仍会从底部的项目中移除。 Anybody can help me in this problem?任何人都可以帮助我解决这个问题吗?

This is how I fetch data from the database这就是我从数据库中获取数据的方式

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

I'm using fragment in onCreateView with setOnItemClickListener我在onCreateView使用片段和setOnItemClickListener

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

And this is my Adapter that extends BaseAdapter这是我扩展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;
       }
    }
}

Looks like you are using an ArrayList that holds the integer and you are trying to remove the items by the index of the item.看起来您正在使用一个包含整数的ArrayList并且您正在尝试通过项目的索引删除项目。 The ArrayList.remove() function takes both the index and the element to be removed from the list. ArrayList.remove()函数获取要从列表中删除的indexelement In your case, as they both are int , this should remove the element from your ArrayList by the index that is provided here - arrIdJob.remove(i);在您的情况下,因为它们都是int ,这应该通过此处提供的索引从您的ArrayList删除元素 - arrIdJob.remove(i); . . Which is fine I think.我认为这很好。

The problem that I see is your data structure and how you have kept your data in order to show them in the ListView .我看到的问题是您的数据结构以及您如何保存数据以便在ListView显示它们。 You have a lot of ArrayList s in your HomeFragment and removing the data from the arrIdJob actually requires the other ArrayList s to clean up themselves too.你有很多ArrayList在你的S HomeFragment及从该数据arrIdJob实际上需要其他ArrayList s到清理自己太。 Because they all are being referenced and hence used in your adapter.因为它们都被引用并因此在您的适配器中使用。

Hence, only removing the id from the arrIdJob should not suffice in your case, as you have to remove that specific item from the other lists too in order to remove the item completely from the list.因此,在您的情况下, arrIdJob删除 id 是不够的,因为您还必须从其他列表中删除该特定项目才能从列表中完全删除该项目。 Hence your implementation of the item click listener might look like the following.因此,您的项目点击侦听器的实现可能如下所示。

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

Removing all those should completely remove the item from the list actually and you should get rid of any unusual behavior.删除所有这些实际上应该从列表中完全删除该项目,并且您应该摆脱任何异常行为。

I would also suggest setting up your data in an object-oriented manner.我还建议以面向对象的方式设置您的数据。 That being said, I would rather create an object like the following.话虽如此,我宁愿创建一个如下所示的对象。

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

And then, I would create a List of CompanyAndJob object to be passed to the adapter via the constructor of the adapter.然后,我将创建一个要通过适配器的构造函数传递给适配器的CompanyAndJob对象List That would make the overall implementation a lot modular.这将使整体实现更加模块化。

Also, if you are getting a list of objects that I suggested, you may have to modify the other parts of your adapter as well.此外,如果您获得了我建议的对象列表,您可能还需要修改适配器的其他部分。 For example, you need to modify the getItem as follows.比如你需要修改getItem如下。

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

I hope you get the idea.我希望你能明白。

Replace getItem(int position) in your Adapter with将 Adapter 中的getItem(int position)替换为

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

Also, make sure you're referring to the same list in your adapter and in your ItemClickListener .此外,请确保您在适配器和ItemClickListener引用相同的列表。

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

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