gpt4 book ai didi

android - 从适配器内部的 View 中删除项目

转载 作者:搜寻专家 更新时间:2023-11-01 08:32:03 25 4
gpt4 key购买 nike

我试图在其标志变为 4 时从 View 中删除一个项目。我尝试了 mObjects.remove(position),然后是 notifyDataSetChanged()。但它没有用。我们尝试了以下所有方法

  if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {


remove(position);
adapter.notifyDataSetChanged();
matcheslistview.setAdapter(adapter);
also this one
// mObjects.remove(position)
// notifyDataSetChanged();

and this one
// mObjects.remove(position);
//remove(position);
//mainObjects.remove(position);
//notifyDataSetChanged();

and this one
// Object toRemove = adapter.getItem(position);
// mObjects.remove(toRemove);
// mObjects.clear();

我们一直得到 java.lang.IndexOutOfBoundsException:无效索引 1,大小为 0。这是完整的适配器类

 private class MatchedDataAdapter extends BaseAdapter implements Filterable {
private AQuery aQuery;
private Activity mActivity;
private LayoutInflater mInflater;
private SessionManager sessionManager;

private int uflag;
MyFilter mfilter;
DatabaseHandler db;
ArrayList<LikeMatcheddataForListview> mObjects;
ArrayList<LikeMatcheddataForListview> mainObjects;

Context context;

public MatchedDataAdapter(Activity context,
ArrayList<LikeMatcheddataForListview> objects,
int imageHeigthAndWidth[]) {

this.mObjects = objects;
mainObjects = objects;
//Log.e("size", Integer.toString(mObjects.size()));

this.mActivity = context;
try {
mInflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
catch (Exception e)

{
e.printStackTrace();
}

aQuery = new AQuery(context);
db = new DatabaseHandler(context);

}

@Override
public int getCount() {
return mObjects.size();
}

@Override
public LikeMatcheddataForListview getItem(int position) {
return mObjects.get(position);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.matchedlistviewitem,
null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.userimage);
holder.textview = (TextView) convertView
.findViewById(R.id.userName);
holder.lastMasage = (TextView) convertView
.findViewById(R.id.lastmessage);

holder.imgStatus = (ImageView) convertView
.findViewById(R.id.imgStatus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}


holder.textview.setText(getItem(position).getUserName());



if (getItem(position).getFlag().trim().equalsIgnoreCase("4")) {


mObjects.remove(position);
adapter.notifyDataSetChanged();
matcheslistview.setAdapter(adapter);

我们想删除标记为 4 的项目,我们正在使用来自 db 的服务读取此标记,然后我们调用类 DisplayContentTask,如下所示

 class GetLikeMatchedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
new DisplayContentTask(intent).execute();
}
}

我们如何获得项目位置以删除带有标志 4 的项目...或者我是另一种删除带有标志 4 的项目的方法我们不知道,但感谢您对此的帮助

  class DisplayContentTask extends AsyncTask<Void, Void, Void> {

Intent intent;

private Ultilities mUltilities = new Ultilities();
private List<NameValuePair> getuserparameter;
private String likedmatchedata, Unmatchedata;
int match1;
private LikedMatcheData matcheData;
private ArrayList<com.appdupe.flamer.pojo.Likes> likesList;
private LikeMatcheddataForListview matcheddataForListview;
DatabaseHandler mDatabaseHandler = new DatabaseHandler(getActivity());
private boolean isResponseSuccess = true;
ArrayList<LikeMatcheddataForListview> tempArray = new ArrayList<LikeMatcheddataForListview>();


@Override
protected void onPreExecute() {
super.onPreExecute();
AppLog.Log(TAG, "BackgroundTaskForFindLikeMatched onPreExecute ");


}

DisplayContentTask(Intent intent) {
this.intent = intent;
}

@Override
protected Void doInBackground(Void... voids) {


try {

File appDirectory = mUltilities
.createAppDirectoy(getResources().getString(
R.string.appdirectory));
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground appDirectory "
+ appDirectory);
File _picDir = new File(appDirectory, getResources().getString(
R.string.imagedirematchuserdirectory));

AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground ");

// getuserparameter = mUltilities.getUserLikedParameter(params);


likedmatchedata = intent.getStringExtra("GET_MATCHED_RESPONSE");
// Unmatchedata = intent.getStringExtra("GET_UNMATCHED_RESPONSE");//hadi

AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likedmatchedata "
+ likedmatchedata);

Gson gson = new Gson();
matcheData = gson.fromJson(likedmatchedata,
LikedMatcheData.class);

AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground matcheData "
+ matcheData);

// "errNum": "51",
// "errFlag": "0",
// "errMsg": "Matches found!",

if (matcheData.getErrFlag() == 0) {

likesList = matcheData.getLikes();
AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likesList "
+ likesList);
if (tempArray != null) {
tempArray.clear();
}

AppLog.Log(TAG,
"BackgroundTaskForFindLikeMatched doInBackground likesList sized "
+ likesList.size());
Log.v("Matches", "" + likesList.size());
match1 = likesList.size();

for (int i = 0; i < likesList.size(); i++) {
Log.d("likelist", likesList.toString());
matcheddataForListview = new LikeMatcheddataForListview();
String userName = likesList.get(i).getfName();
String facebookid = likesList.get(i).getFbId();
// Log.i(TAG, "Background facebookid......"+facebookid);
String picturl = likesList.get(i).getpPic();
int falg = likesList.get(i).getFlag();
// if (likesList.get(i).getFlag()==4) {
// likesList.remove(getId());
// }

Log.i("komak10",""+likesList.get(i).getFlag());
String latd = likesList.get(i).getLadt();
matcheddataForListview.setFacebookid(facebookid);
matcheddataForListview.setUserName(userName);
matcheddataForListview.setImageUrl(picturl);
matcheddataForListview.setFlag("" + falg);
matcheddataForListview.setladt(latd);
// matcheddataForListview.setFilePath(filePath);
File imageFile = mUltilities.createFileInSideDirectory(
_picDir, userName + facebookid + ".jpg");
// logDebug("BackGroundTaskForUserProfile doInBackground imageFile is profile "+imageFile.isFile());
Utility.addBitmapToSdCardFromURL(likesList.get(i)
.getpPic().replaceAll(" ", "%20"), imageFile);
matcheddataForListview.setFilePath(imageFile
.getAbsolutePath());

if (!preferences.getString(Constant.FACEBOOK_ID, "")
.equals(facebookid)) {
tempArray.add(matcheddataForListview);

}

}
DatabaseHandler mDatabaseHandler = new DatabaseHandler(
getActivity());
// SessionManager mSessionManager = new SessionManager(
// MainActivity.this);
String userFacebookid = preferences.getString(
Constant.FACEBOOK_ID, "");

//
boolean isdataiserted = mDatabaseHandler.insertMatchList(
tempArray, userFacebookid);

} else if (matcheData.getErrFlag() == 1) {
if(tempArray!=null)
{
tempArray.clear();
}

} else {
// do nothing
}

} catch (Exception e) {
AppLog.handleException(
"BackgroundTaskForFindLikeMatched doInBackground Exception ",
e);
// some thing wrong happend
isResponseSuccess = false;
}



return null;
}

最佳答案

不要在 getview 中删除对象,如果您必须过滤它,请在发送到适配器之前过滤它。可能在创建 subview 时第一个单元格有标签“4”,现在 View 没有创建(因为没有调用 return)但是你试图删除它的位置,所以它肯定会给你 IndexOutOfBoundsException。

我最好的解决方案是,使用

设置适配器
new ArrayList<LikeMatcheddataForListview>() 

每当您启动屏幕时。一旦你的 AsyncTask 完成过滤掉带有标签“4”的 child (最好只在 asynctask 中过滤掉它,ui 线程中的任务更少)然后刷新适配器,比如

public void refresh(ArrayList<LikeMatcheddataForListview>() arrObjects){
objects = arrObjects;
notifyDataSetChanged();
}

检查一下,它应该可以解决问题

关于android - 从适配器内部的 View 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39698200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com