2016-05-07 37 views
6

Voglio impostare l'immagine catturata alla vista di immagini creata dinamicamente nella stessa griglia, ma crea un nuovo layout di griglia e imposta l'immagine catturata nella nuova griglia.vista immagine creata dinamicamente in griglia non funzionante

Questo è il mio file di mainactivity.java

public class MainActivity extends Activity implements AdapterView.OnItemClickListener { 

    GridView gridView; 
    ArrayList<Damage_Item> gridArray = new ArrayList<Damage_Item>(); 
    CustomAdapter_back customGridAdapter; 
    GridLayout mContainerView; 
    int REQUEST_CAMERA = 0, SELECT_FILE = 1; 
    ImageView dynamic_imageview; 
    LinearLayout dynamic_linearview; 
    String image1; 
    ArrayList<String> arrayList_image = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mContainerView = (GridLayout)findViewById(R.id.describedamage_gridview) ; 
     //gridArray.add(new Item(homeIcon,"Home")); 
     gridView = (GridView) findViewById(R.id.gridview1); 

     Button add = (Button)findViewById(R.id.button1); 
     add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       inflateImageRow(); 
      } 
     }); 

    } 

    public void selectImage() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(intent, REQUEST_CAMERA); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      if (requestCode == REQUEST_CAMERA) 
      onCaptureImageResult(data); 
     } 
    } 

    public void onCaptureImageResult(Intent data) { 
     Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
     Bitmap resized = Bitmap.createScaledBitmap(thumbnail, 140, 150, true); 
     Bitmap dest = Bitmap.createBitmap(resized.getWidth(), resized.getHeight(), Bitmap.Config.ARGB_8888); 

     ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream(); 
     dest.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream1); 

     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
     String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system 


     Canvas canvas = new Canvas(dest); //bmp is the bitmap to dwaw into 
     Paint paint = new Paint(); 
     canvas.drawBitmap(resized, 0f, 0f, null); 
     paint.setColor(Color.BLACK); 
     canvas.drawRect(1, 145, 100, 130, paint); 
     paint.setColor(getResources().getColor(R.color.orange)); 
     paint.setTextSize(10); 
     paint.setFakeBoldText(true); 

     paint.setTextAlign(Paint.Align.LEFT); 
     float height = paint.measureText("yY"); 
     canvas.drawText(dateTime, 5f, height+130f, paint); 

     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     dest.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
     byte[] byteArray = byteArrayOutputStream.toByteArray(); 
     String image1 = Base64.encodeToString(byteArray, Base64.DEFAULT); 

     //arrayList_image.add(image1); 
     BitmapDrawable background = new BitmapDrawable(dest); 
     //holder.imageItem.setBackground(background); 
     Damage_Item damage_item = new Damage_Item(); 
     damage_item.setTemp_image(dest); 
     gridArray.add(damage_item); 

     customGridAdapter = new CustomAdapter_back(MainActivity.this, gridArray); 
     gridView.setAdapter(customGridAdapter); 


     /*gridView.setAdapter(customGridAdapter); 

     gridView.setOnItemClickListener(MainActivity.this)*/; 



    } 
    int count; 
    private void inflateImageRow() { 

     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View rowView = inflater.inflate(R.layout.dynamic_row, null); 
     dynamic_imageview = (ImageButton)rowView.findViewById(R.id.dynamic_imageview); 

     dynamic_linearview=(LinearLayout)rowView.findViewById(R.id.dynamic_linear); 

      dynamic_imageview.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //image_flagdynamic_right = true; 
       selectImage(); 
      } 
     }); 
     count= mContainerView.getChildCount()-1; 
     dynamic_imageview.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(final View v) { 
      int parent=((View) v.getParent()).getId(); 
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
       builder.setTitle("Whould you like to delete this image?"); 
       builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         int index = ((View) v.getParent()).getId() + 2; 

         try { 
          mContainerView.removeView((View) v.getParent()); 
          // arrayList_image.remove(((View) v.getParent()).getId()); 
         } catch (IndexOutOfBoundsException e) { 
          e.printStackTrace(); 
         } 
         Toast.makeText(getApplicationContext(), "delete", Toast.LENGTH_LONG).show(); 
        } 
       }); 
       builder.setNegativeButton("No", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 

       return false; 
      } 
     }); 

     mContainerView.addView(rowView, mContainerView.getChildCount() - 1); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

    } 
} 

questo è il mio file dell'adattatore CustomAdapter_back.java

public class CustomAdapter_back extends BaseAdapter { 

    private Context context; 
    private ArrayList<Damage_Item> Damage_Item; 
    LayoutInflater inflater; 
    ImageView button; 

    public CustomAdapter_back(Context context, ArrayList<Damage_Item> Damage_Item) { 
     this.context = context; 
     this.Damage_Item = Damage_Item; 
     inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return Damage_Item.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.row_grid, null); 
      button = (ImageView) convertView.findViewById(R.id.imageview); 
     } 
     else 
     { 
     } 

     button.setImageBitmap(Damage_Item.get(position).getTemp_image()); 

     return convertView; 
    } 
} 

mio activity_manin.xml file di

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="26dp" 
     android:padding="11dp" 
     android:text="Add" /> 
<GridLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/describedamage_gridview" 
    android:columnCount="2" 
    android:layout_below="@+id/button1" 
    android:rowCount="1" 
    android:orientation="horizontal"> 

    <GridView 
     android:id="@+id/gridview1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnWidth="90dp" 
     android:numColumns="2" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
     /> 
</GridLayout> 

row_grid.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="170dp" 
     android:layout_height="150dp" 
     android:background="@drawable/camera" /> 

    </LinearLayout> 
+0

Non capisco chiaramente che senso usare una GridView in un GridLayout ... Potresti usare solo un GridView con 2 colonne e aggiungere il ImageView in modo dinamico. Come vedo, il problema sembra ricreare l'adattatore ogni volta che si passa in 'onCaptureImageResult()'. Dovresti crearlo una volta e aggiornarlo chiamando 'notifyDataSetChanged()'. – Fllo

risposta

0

prima vista sembra non usando il modello della titolare nel vostro CustomAdapter_back anche come Fllo notato che non è necessario rimuovere manualmente i punti di vista. aggiungere questa funzione alla scheda

public void setData(ArrayList<Damage_Item> data) 
{ 
    this.Damage_Item = Damage_Item; 
    notifyDataSetChanged(); 
} 

e chiamare questa funzione dalla finestra di

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      builder.setTitle("Whould you like to delete this image?"); 
      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //get deleted item index according to your logic 
        int index = ((View) v.getParent()).getId() + 2; 
        gridArray.remove(index); 
        customGridAdapter.setData(gridArray); 
        Toast.makeText(getApplicationContext(), "delete", Toast.LENGTH_LONG).show(); 
       } 
      }); 
0

in su activityResult avete un 2 righe di codice:

customGridAdapter = new CustomAdapter_back(MainActivity.this, gridArray); 
    gridView.setAdapter(customGridAdapter); 

così ogni singola volta quando aggiungi una nuova immagine stai ricreando l'adattatore.

Basta spostare la dichiarazione dell'adattatore su onCreate con la lista vuota. Add metodo

public void addItem(Damage_Item item){ 
    this.Damage_Item.add(item); 
    this.notifyDataSetChange(); 
} 

al vostro adattatore e innescare invece di creare adattatore capace di creare nuovi all'interno del vostro onCaptureImageResult.

E per amor di dio, convenzione di denominazione. Il tuo codice è illeggibile

0

Prova questa

public class MainActivity extends Activity implements AdapterView.OnItemClickListener { 

GridView gridView; 
ArrayList<Damage_Item> gridArray = new ArrayList<Damage_Item>(); 
CustomAdapter_back customGridAdapter; 
GridLayout mContainerView; 
int REQUEST_CAMERA = 0, SELECT_FILE = 1; 
ImageView dynamic_imageview; 
LinearLayout dynamic_linearview; 
String image1; 
ArrayList<String> arrayList_image = new ArrayList<String>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mContainerView = (GridLayout)findViewById(R.id.describedamage_gridview) ; 
    //gridArray.add(new Item(homeIcon,"Home")); 
    gridView = (GridView) findViewById(R.id.gridview1); 

    Button add = (Button)findViewById(R.id.button1); 
    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      inflateImageRow(); 
     } 
    }); 
customGridAdapter = new CustomAdapter_back(MainActivity.this, gridArray); 
    gridView.setAdapter(customGridAdapter); 

} 

public void selectImage() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(intent, REQUEST_CAMERA); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == REQUEST_CAMERA) 
     onCaptureImageResult(data); 
    } 
} 

public void onCaptureImageResult(Intent data) { 
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
    Bitmap resized = Bitmap.createScaledBitmap(thumbnail, 140, 150, true); 
    Bitmap dest = Bitmap.createBitmap(resized.getWidth(), resized.getHeight(), Bitmap.Config.ARGB_8888); 

    ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream(); 
    dest.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream1); 

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
    String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system 


    Canvas canvas = new Canvas(dest); //bmp is the bitmap to dwaw into 
    Paint paint = new Paint(); 
    canvas.drawBitmap(resized, 0f, 0f, null); 
    paint.setColor(Color.BLACK); 
    canvas.drawRect(1, 145, 100, 130, paint); 
    paint.setColor(getResources().getColor(R.color.orange)); 
    paint.setTextSize(10); 
    paint.setFakeBoldText(true); 

    paint.setTextAlign(Paint.Align.LEFT); 
    float height = paint.measureText("yY"); 
    canvas.drawText(dateTime, 5f, height+130f, paint); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    dest.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream.toByteArray(); 
    String image1 = Base64.encodeToString(byteArray, Base64.DEFAULT); 

    //arrayList_image.add(image1); 
    BitmapDrawable background = new BitmapDrawable(dest); 
    //holder.imageItem.setBackground(background); 
    Damage_Item damage_item = new Damage_Item(); 
    damage_item.setTemp_image(dest); 
    gridArray.add(damage_item); 

    customGridAdapter.notifyDataSetChanged(); 


    /*gridView.setAdapter(customGridAdapter); 

    gridView.setOnItemClickListener(MainActivity.this)*/; 



} 
int count; 
private void inflateImageRow() { 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View rowView = inflater.inflate(R.layout.dynamic_row, null); 
    dynamic_imageview = (ImageButton)rowView.findViewById(R.id.dynamic_imageview); 

    dynamic_linearview=(LinearLayout)rowView.findViewById(R.id.dynamic_linear); 

     dynamic_imageview.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //image_flagdynamic_right = true; 
      selectImage(); 
     } 
    }); 
    count= mContainerView.getChildCount()-1; 
    dynamic_imageview.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(final View v) { 
     int parent=((View) v.getParent()).getId(); 
     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
      builder.setTitle("Whould you like to delete this image?"); 
      builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        int index = ((View) v.getParent()).getId() + 2; 

        try { 
         mContainerView.removeView((View) v.getParent()); 
         // arrayList_image.remove(((View) v.getParent()).getId()); 
        } catch (IndexOutOfBoundsException e) { 
         e.printStackTrace(); 
        } 
        Toast.makeText(getApplicationContext(), "delete", Toast.LENGTH_LONG).show(); 
       } 
      }); 
      builder.setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

      return false; 
     } 
    }); 

    mContainerView.addView(rowView, mContainerView.getChildCount() - 1); 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

} 
} 
0

provare a cambiare il tuo XML rimuovendo il GridView come GridLayout dovrebbe modificare la dimensione dell'immagine automaticamente. Un'altra soluzione non raccomandata sarebbe quella di utilizzare le dimensioni dello schermo per ridimensionare l'immagine