我有一些带有图像的网格视图。以及网格视图末尾的添加按钮。我想通过添加按钮从电话库向此网格添加新图像。当我点击网格中的每个图像时,我还必须从网格中删除该项目。我尝试了很多教程,但没有什么能满足我的要求。请帮忙 。 这是我的主要活动
public class MainActivity extends Activity {
GridView gv;
String [] web ={"1","2","3","4","5","6"};
int [] images={
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
};
GridAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView)findViewById(R.id.gridView1);
adapter = new GridAdapter(MainActivity.this, web, images);
gv.setAdapter(adapter);
Button add = (Button)findViewById(R.id.buttonadd);
//code for add button..
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// this code is to open the phone gallery
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
}
//this is to get selected image from phone gallery
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1234:
if(resultCode == RESULT_OK){
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Integer xx = Integer.valueOf(filePath);
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
};
}
这是我的网格适配器类
public class GridAdapter extends BaseAdapter {
private Context mcontext;
private final String [] web;
private final int[] images;
public GridAdapter (Context c , String [] web ,int [] images){
mcontext = c;
this.images = images;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater layoutinflatr = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mcontext);
grid = layoutinflatr.inflate(R.layout.gridxml, null);
TextView tv = (TextView)grid.findViewById(R.id.textView1);
ImageView iv = (ImageView)grid.findViewById(R.id.imageView1);
tv.setText(web[position]);
iv.setImageResource(images[position]);
}
else{
grid = (View) convertView;
}
grid.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// code to delete the selected item.
}
});
return grid;
}
}
请帮助我。
答案 0 :(得分:1)
使用ArrayList
代替Array
更新您的codel,如下所示。
String[] web = {"1", "2", "3", "4", "5", "6"};
int[] images = {
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher
};
ArrayList<Integer> NewImages;
ArrayList<String> NewWeb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewImages = new ArrayList<Integer>();
NewWeb = new ArrayList<String>();
for (int i = 0; i < web.length; i++) {
NewImages.add(images[i]);
NewWeb.add(web[i]);
}
// ...
adapter = new GridAdapter(MainActivity.this, NewWeb, NewImages);
// ...
}
适配器修改:
public class GridAdapter extends BaseAdapter {
private Context mcontext;
ArrayList<Integer> images;
ArrayList<String> web;
public GridAdapter(Context c, ArrayList<String> web, ArrayList<Integer> images) {
mcontext = c;
this.images = images;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater layoutinflatr = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mcontext);
grid = layoutinflatr.inflate(R.layout.gridxml, null);
TextView tv = (TextView) grid.findViewById(R.id.textView1);
ImageView iv = (ImageView) grid.findViewById(R.id.imageView1);
tv.setText(web.get(position));
iv.setImageResource(images.get(position));
} else {
grid = (View) convertView;
}
grid.setTag(position);
grid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// code to delete the selected item.
web.remove((int) v.getTag());
images.remove((int) v.getTag());
notifyDataSetChanged();
}
});
return grid;
}
}