gpt4 book ai didi

android - 在 Android 中更新 GridView

转载 作者:行者123 更新时间:2023-11-29 00:12:45 24 4
gpt4 key购买 nike

我在 gridview 中显示多个图像,并附有复选框。 multiple images .我的要求是,每当我选中任何复选框时,应从 gridview 中删除特定 View ,并且我的 gridview 应使用剩余的图像集进行更新。

例如,如果我检查 gridView 的第二个元素,该特定元素应从 gridview 中完全删除,其余图像应在 gridview 中重新排列。即第 3 个元素应该排在第 2 个位置,第 4 个元素应该排在第 3 个位置。 second modification

同样,如果我检查更新后的 gridView 的第三个元素,该特定元素应该从 gridview 中完全删除,其余图像应该在 gridview 中重新排列。 third ![modification][3]ExistingDetailedActivity.java

public class ExistingDetailedActivity extends Activity {
public String images,audiopath,name;
TextView ringtonename;
public GridView gridView;
public String [] imgpath;
CustomBaseExistAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_detailed);
Intent i = getIntent();

if(i.hasExtra("BNDL")){
Bundle bdl = getIntent().getBundleExtra("BNDL");

if(bdl.get("IMAGEPATH") != null){
images = bdl.getString("IMAGEPATH");
}

if(bdl.get("AUDIOPATH") != null){
audiopath = bdl.getString("AUDIOPATH");
}
if(bdl.get("RINGTONENAME") != null){
name = bdl.getString("RINGTONENAME");
}
}
imgpath=images.split("\\*") ;
ringtonename=(TextView) findViewById(R.id.textView2);
ringtonename.setText(name);
gridView=(GridView) findViewById(R.id.gridview1);
//CustomExistingListAdapter adapter = new CustomExistingListAdapter(this,imgpath);
//CustomBaseExistAdapter adapter = new CustomBaseExistAdapter(this,imgpath);
adapter = new CustomBaseExistAdapter(this,imgpath);
gridView.setAdapter(adapter);
//adapter.notifyDataSetChanged();

}

}

CustomBaseExistAdapter.java

public class CustomBaseExistAdapter extends BaseAdapter{

private final Activity context;
private final String[] imagepath;
private String[] imagepath1=null;
private String[] imagepath2=null;
private String[] imagepath3=null;
public CustomBaseExistAdapter(Activity context,
String[] imagepath) {
this.context = context;
this.imagepath = imagepath;

List<String> nonBlank = new ArrayList<String>();
for(String s: imagepath) {
if (!s.trim().isEmpty()) {
nonBlank.add(s);
}
}
//nonBlank will have all the elements which contain some characters.
imagepath1 = (String[]) nonBlank.toArray( new String[nonBlank.size()] );
imagepath2 = (String[]) nonBlank.toArray( new String[nonBlank.size()] );
//notifyDataSetChanged();
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return imagepath.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = null;

if (convertView == null) {

LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_image_with_checkbox, null);
CheckBox cb=(CheckBox) convertView.findViewById(R.id.checkBox1);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imgThumb);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id = buttonView.getId();
if(isChecked){


List<String> list = new ArrayList<String>(Arrays.asList(imagepath1));
list.remove(imagepath1[position]);
imagepath3 = list.toArray(new String[0]);
//////////////////////////////////
List<String> nonBlank1 = new ArrayList<String>();
for(String s: imagepath3) {
if (!s.trim().isEmpty()) {
nonBlank1.add(s);
}
}
imagepath2=null;
//nonBlank will have all the elements which contain some characters.
imagepath2 = (String[]) nonBlank1.toArray( new String[nonBlank1.size()] );
//imageView.setImageBitmap(BitmapFactory.decodeFile(null));
notifyDataSetChanged();

}

}

});


imageView.setImageBitmap(BitmapFactory.decodeFile(imagepath2[position]));

}
return convertView;
}

最佳答案

你的逻辑有很多问题。通常,对于网格更新,您应该在 UI 主线程上使用以下内容:

 adapter.notifyDataChanged();

有时还有:

 grid.invalidateViews();

但您的问题是,在复选框更改时您没有更新数据源中的数据,这似乎是 imagePath[],因为您将其传递给构造函数并在 getCount 中使用它() 方法检索计数。

因此,如果您希望网格更新,您应该从此数据源中删除该元素。

如果要保留所有 imagePath[] 数据,请使用另一个变量进行备份:

private String[] imagepathBackUp=null;

public CustomBaseExistAdapter(Activity context,
String[] imagepath) {
this.context = context;
this.imagepath = imagepath;

this.imagepathBackUp =imagepath;

List<String> nonBlank = new ArrayList<String>();
for(String s: imagepath) {
if (!s.trim().isEmpty()) {
nonBlank.add(s);
}
}

然后像这样从 imagePath 中删除它:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int id = buttonView.getId();
if(isChecked){


List<String> list = new ArrayList<String>(Arrays.asList(imagepath1));
list.remove(imagepath1[position]);
imagepath3 = list.toArray(new String[0]);
//////////////////////////////////
List<String> nonBlank1 = new ArrayList<String>();
for(String s: imagepath3) {
if (!s.trim().isEmpty()) {
nonBlank1.add(s);
}
}
imagepath2=null;
//nonBlank will have all the elements which contain some characters.
imagepath2 = (String[]) nonBlank1.toArray( new String[nonBlank1.size()] );
//imageView.setImageBitmap(BitmapFactory.decodeFile(null));

/// REMOVE item from itemPath[], something like this:


String newList[] = new String[itemPath.length - 1];
int count = 0;
for (int i = 0; i < itemPath.length; i++) {
if (itemPath.length - 1 > 0) {
if (itemPath[i] == imagepath1[position]) { // itemPath[1] as the range starts from 0, so 1 would be ITEM2
// SKIP IF MATCHES THE ITEM YO WANT TO REMOVE
} else {
newList[count] = itemPath[i];
count++;
}
}
}
itemPath= newList;

notifyDataSetChanged();

}

也请查看此链接,这正是您所需要的:

Android - Remove GridView item from inside getView()?

编辑:

与其使用数组来删除,不如使用列表更好,就像我以前看到的那样:

List<String> newlist = new ArrayList<String>(Arrays.asList(itemPath));          
newlist.remove(imagepath1[position]);
itemPath = newlist.toArray(new String[newlist.size()]);

此外,检查 imagepath1[position] 不会抛出异常,以便 imagepath1.lenght > position。

关于android - 在 Android 中更新 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004180/

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