Я показываю несколько изображений в gridview с прикрепленным к нему флажком. . Мое требование - всякий раз, когда я проверю флажок, что конкретное представление должно быть удалено из gridview, и мое gridview должно быть обновлено с остальным набором изображений.Обновление GridView в android
Например, если я проверяю второй элемент gridView, этот конкретный элемент должен быть полностью удален из сетки, а оставшиеся изображения должны быть перегруппированы в gridview. т. е. 3-й элемент должен выйти на 2-ю позицию, а 4-й элемент должен прийти к 3-му положению.
Снова Если я проверяю третий элемент обновленного gridView, этот конкретный элемент должен быть полностью удален из gridview, а оставшиеся изображения должны быть перегруппированы в gridview. 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;
}
очень хороший ответ sir.but иногда его отображение arrayindexoutofboundexception –
Я отредактировал мой ответ. –
yup .by, используя концепцию проблемы ArrayList. –