Java 使用带有自定义baseadapter的arraylist删除listview项位置时出错
单击项目位置时,我尝试使用Java 使用带有自定义baseadapter的arraylist删除listview项位置时出错,java,android,arraylist,android-listview,Java,Android,Arraylist,Android Listview,单击项目位置时,我尝试使用ArrayList删除ListView项目。然而,它总是得到错误的删除项目的位置。为什么从底部删除该项目?如果我单击列表视图中的任意位置,该位置仍然会从底部删除该项。有人能帮我解决这个问题吗 这就是我从数据库中获取数据的方式 public static ArrayList<Integer> arrIdJob = new ArrayList<Integer>(); myJSON = json.getJSONArray(TAG_record);
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")));
}
这是我的Adapter
,它扩展了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;
}
}
}
公共类适配器ListHome扩展BaseAdapter{
私人活动;
私有数组列表数据;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
私人语境;
公共AdapterListHome(上下文,ArrayList listData){
this.listData=listData;
layoutInflater=layoutInflater.from(上下文);
this.context=上下文;
}
公共适配器列表(活动法){
这个活动=行动;
}
public int getCount(){
返回HomeFragment.arrIdJob.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
@凌驾
public int getViewTypeCount(){
返回getCount();
}
@凌驾
public int getItemViewType(int位置){
返回位置;
}
@SuppressLint(“SetTextI18n”)
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机充气(R.layout.desain_视图,空);
holder=新的ViewHolder();
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.tvJobTitle=(TextView)convertView.findViewById(R.id.tv_jobTitle);
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);
//钥匙
holder.keyIdUser=(TextView)convertView.findViewById(R.id.key\u iduser);
holder.keyIdJob=(TextView)convertView.findViewById(R.id.key\u idjob);
holder.keyIdCompany=(TextView)convertView.findViewById(R.id.key\u idcompany);
holder.keyQualification=(TextView)convertView.findViewById(R.id.key\u qualification);
holder.keyDesc=(TextView)convertView.findViewById(R.id.key\u desc);
//设置
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(homepragment.arrMinSal.get(position))+“-”+toRupiah(homepragment.arrMaxSal.get(position));
//设置为键
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\u URL\u IMG\u CAT+ListMenuActivity.IMAGE.get(position)).to(holder.imgThumb);
返回视图;
}
静态类视窗夹{
TextView tvJobTitle、tvCompanyName、tvState、tvSalary;
text查看keyIdUser、keyIdJob、keyIdCompany、keyidqualification、keyDesc;
字符串数据;
//ImageView imgThumb;
}
专用字符串toRupiah(字符串标称值){
字符串hasil=“”;
DecimalFormat toRupiah=(DecimalFormat)DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols formatAngka=新的DecimalFormatSymbols();
formatAngka.setCurrencySymbol(“Rp”);
formatAngka.SetMonetaryCimalSeparator(',');
toRupiah.setDecimalFormatSymbols(formatAngka);
hasil=toRupiah.format(Double.valueOf(标称));
返回哈西尔;
}
}
}
用替换适配器中的getItem(int位置)
public Object getItem(int position) {
return listData.get(position);
}
另外,请确保您在适配器和项目ClickListener
中引用了相同的列表,并且在项目ClickListener中,看起来您使用的是保存整数的数组列表
,并且您正试图通过项目的索引删除项目。ArrayList.remove()
函数从列表中删除索引和元素。在您的情况下,由于它们都是int
,这应该通过此处提供的索引从ArrayList
中删除元素-arrIdJob.remove(i)代码>。我想这很好
我看到的问题是您的数据结构以及如何保存数据以便在列表视图中显示它们。您的HomeFragment
中有很多ArrayList
,从arrIdJob
中删除数据实际上也需要其他ArrayList
自己清理。因为它们都被引用,因此在适配器中使用
因此,在您的情况下,仅从arrIdJob
中删除id是不够的,因为您也必须从其他列表中删除该特定项,以便从列表中完全删除该项。因此,项目click侦听器的实现可能如下所示
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
对象的列表
,通过适配器的构造函数传递给适配器。这将使整个实现更加模块化
此外,如果您得到我建议的对象列表,您可能需要修改您的文档的其他部分
public class CompanyAndJob {
public int jobId;
public int companyName;
public int jobStr;
public int qualification;
// .... Other items
}
public CompanyAndJob getItem(int position) {
return listOfCompanyAndJobs.get(position);
}