我的代码中出现此错误。 请帮帮我。
我的代码如下。我不知道如何解决这个问题。当我点击添加按钮时,我想将所有数据添加到模型类中,并且该类应该填充主活动中的列表数组。
public class MainActivity extends Activity implements DataTransferInterfase{
private Button btnSelect,btnShow;
public int REQUEST_CAMERA=1;
public int SELECT_IMAGE=0;
private GridView gridview;
private CustomAdapter gridAdaptor;
public TextView tvcounter;
public ImageItemBin imageItemBin;
private Uri mCapturedImageUri;
public static ArrayList<ImageItemBin> publicSelectedImage=new ArrayList<ImageItemBin>();
public ArrayList<ImageItemBin> showImagelist=new ArrayList<ImageItemBin>();
public Uri ImageUri;
private int count=publicSelectedImage.size();
private int coloumn=3;
private int row=count/coloumn;
public String imgPath="";
private Uri fileUri; // file url to store image
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect=(Button)findViewById(R.id.btnselect);
btnShow=(Button)findViewById(R.id.btnShow);
tvcounter=(TextView)findViewById(R.id.tvcounter);
publicSelectedImage=new ArrayList<ImageItemBin>();
//showImagelist=new ArrayList<ImageItemBin>();
showImagelist=new ArrayList<ImageItemBin>();
gridview=(GridView) findViewById(R.id.gridLayout_main);
// gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage);
// gridview.setAdapter(gridAdaptor);
//gridAdaptor(R.layout.custom_grid_item_layout,getView());
//gridLayout.addView(gridAdaptor.getView());
//gridLayout.addView(inflater.inflate(R.layout.custom_grid_item_layout,null));
//gridLayout.addView(img);
//tvcounter.setText(CountRecord(imageItemBin));
//tvcounter.setText(CustomAdapter.result+"");
showImagelist.add(CustomAdapter.showBin);
tvcounter.setText(showImagelist.size());
//showImagelist.add(CustomAdapter.result);
//tvcounter.setText(counter);
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//custom dialogBox
final Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
dialog.setTitle("Select from..");
//set the custom dialog components
TextView txtmsg=(TextView)dialog.findViewById(R.id.txtmsg);
Button btnGallaery=(Button)dialog.findViewById(R.id.btngallery);
Button btnCamara=(Button)dialog.findViewById(R.id.btncamara);
btnGallaery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent image=new Intent();
image.setType("image/*");
image.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(image,"select file"),SELECT_IMAGE);
dialog.dismiss();
}
});
btnCamara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cam=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/*if(cam.resolveActivity(getPackageManager())!=null){
String filename="temp.jpg";
ContentValues values=new ContentValues();
values.put(MediaStore.Images.Media.TITLE,filename);
mCapturedImageUri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);*/
//cam.putExtra(MediaStore.EXTRA_OUTPUT,setImageUri());
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
cam.putExtra("Image", fileUri);
startActivityForResult(cam,REQUEST_CAMERA);
dialog.dismiss();
// }
}
});
dialog.show();
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==SELECT_IMAGE){
if(resultCode==RESULT_OK && null!=data){
for (int i=0,c=0,r=0;i<count;i++,c++){
if(c==coloumn){
c=0;
r++;
}
}
ImageUri=data.getData();
imageItemBin=new ImageItemBin();
imageItemBin.setImage(ImageUri.toString());
publicSelectedImage.add(imageItemBin);
gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage,this);
gridview.setAdapter(gridAdaptor);
}
}
if(requestCode==REQUEST_CAMERA)
{
if(resultCode==RESULT_OK && data!=null){
/*String[] projection={MediaStore.Images.Media.DATA};
Cursor cursor=managedQuery(mCapturedImageUri,projection,null,null,null);
int coloumn_index_data=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picturePath=cursor.getString(coloumn_index_data);
Uri selectedImage=data.getData();
imageItemBin=new ImageItemBin();
imageItemBin.setImage(selectedImage.toString());
publicSelectedImage.add(imageItemBin);*/
// gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage);
// gridview.setAdapter(gridAdaptor);
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
String stringImage=BitMapToString(mphoto);
// String getimage=getImagePath();
imageItemBin=new ImageItemBin();
imageItemBin.setImage(stringImage);
//imageItemBin.setImage(picturePth.toString());
publicSelectedImage.add(imageItemBin);
gridAdaptor=new CustomAdapter(MainActivity.this,publicSelectedImage,this);
gridview.setAdapter(gridAdaptor);
//gridAdaptor.notifyDataSetChanged();
// tvcounter.setText(counter);
//publicSelectedImage=selectedImage;
}
}
}
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
/*private List<ImageItemBin> DisplayImage(){
ImageItemBin itembin=new ImageItemBin();
itembin.getImage();
}*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
/*File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
*/
//File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
// Create the storage directory if it does not exist
/*if (!file.exists()) {
if (!file.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}*/
// Create a media file name
//String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
// Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
} /*else if (type == MEDIA_TYPE_VIDEO) {
//mediaFile = new File(file.getPath() + File.separator
// + "VID_" + timeStamp + ".mp4");
}*/ else {
return null;
}
return mediaFile;
//return file;
}
private Bitmap previewCapturedImage(Uri file)
{
Bitmap bitmap=null;
try {
// hide video preview
//videoPreview.setVisibility(View.GONE);
//imgPreview.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(file.getPath(),
options);
return bitmap;
//imgPreview.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
return bitmap;
}
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp= Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
public static int totalAmt=0;
@Override
public int CountRecord(ImageItemBin bin) {
//for (int i=0;i<count;i++){
totalAmt=totalAmt+1;
// }
return totalAmt;
}
}
答案 0 :(得分:0)
我得到了它。 这里是获得完美result.in适配器的方法,只需将data.get(position)添加到countRecord方法。
public int CountRecord(ImageItemBin bin)
{
showImagelist.add(showBin);
int size=showImagelist.size();
tvcounter.setText(Integer.toString(size));
btnShow.setText(Integer.toString(size));
return 0;
}
CustomAdaptor.java
public class CustomAdapter extends BaseAdapter {
public static String counter="";
public static int result=0;
Context context;
private int layoutResourceId;
private ArrayList<ImageItemBin> data=new ArrayList<ImageItemBin>();
public static ImageItemBin showBin=new ImageItemBin();
DataTransferInterfase dataTransferInterfase;
String qty,title;
public MainActivity mainActivity;
ImageLoader imageLoader;
DisplayImageOptions options;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity,ArrayList<ImageItemBin> data,DataTransferInterfase dataTransferInterfase){
this.data=data;
this.context=mainActivity;
this.dataTransferInterfase=dataTransferInterfase;
this.mainActivity=mainActivity;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
Holder holder=null;
View rootview=convertView;
if(rootview==null) {
rootview = inflater.inflate(R.layout.custom_grid_item_layout, null);
holder = new Holder();
holder.edttitle = (EditText) rootview.findViewById(R.id.edtTitle);
holder.edtqty = (EditText) rootview.findViewById(R.id.edtQty);
holder.image = (ImageView) rootview.findViewById(R.id.MyimageView);
holder.tvcounter=(TextView)rootview.findViewById(R.id.tvcounter);
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
rootview.setTag(holder);
qty=holder.edtqty.getText().toString();
title=holder.edttitle.getText().toString();
data.get(position).setTitle(title);
data.get(position).setQty(qty);
}
else
{
holder=(Holder)convertView.getTag();
holder.image.setTag(position);
holder.edttitle.setTag(position);
holder.edtqty.setTag(position);
qty=holder.edtqty.getText().toString();
title=holder.edttitle.getText().toString();
data.get(position).setTitle(title);
data.get(position).setQty(qty);
}
String img=data.get(position).getImage();
final String title=data.get(position).getTitle();
final String qty=data.get(position).getQty();
options = getDisplayImageOptions(context, R.mipmap.ic_launcher);
imageLoader.displayImage(img, holder.image, options);
holder.btnadd=(Button)rootview.findViewById(R.id.btnAdd);
final Holder finalHolder = holder;
//public static final ImageItemBin showBin=new ImageItemBin();
holder.btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setQty(qty);
data.get(position).setTitle(title);
showBin.setImage(data.get(position).getImage());
showBin.setTitle(title);
showBin.setQty(qty);
mainActivity.CountRecord(data.get(position));
}
});
return rootview;
}
static class Holder{
ImageView image;
EditText edtqty;
EditText edttitle;
Button btnadd;
TextView tvcounter;
}
public static DisplayImageOptions getDisplayImageOptions(Context context, int defaultImg) {
DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(defaultImg)
.resetViewBeforeLoading(true).showImageForEmptyUri(defaultImg).showImageOnFail(defaultImg)
.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
.considerExifParams(true).build();
return options;
}
}