2013-12-09 29 views
0
public class Uploads extends Activity implements OnClickListener { 

    ImageView btnAllShops, btnFavourites, btnUploads, btnSettings, btnBuys, 
      btnTakePhoto; 

    GridView gvUploads; 

    PhotoDbAdapter ourHelper; 

    private String description, category, price, imagepath; 

    final static int cameraData = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_uploads); 
     findViewById(); 
     onBackPressed(); 
     sdcard(); 

    } 

    private void findViewById() { 
     gvUploads = (GridView) findViewById(R.id.gvUploads); 
     btnAllShops = (ImageView) findViewById(R.id.btnAllShops); 
     btnFavourites = (ImageView) findViewById(R.id.btnFavourites); 
     btnUploads = (ImageView) findViewById(R.id.btnUploads); 
     btnSettings = (ImageView) findViewById(R.id.btnSettings); 
     btnBuys = (ImageView) findViewById(R.id.btnBuys); 
     btnTakePhoto = (ImageView) findViewById(R.id.btnTakePhoto); 

     btnAllShops.setOnClickListener(this); 
     btnFavourites.setOnClickListener(this); 
     btnUploads.setOnClickListener(this); 
     btnSettings.setOnClickListener(this); 
     btnBuys.setOnClickListener(this); 
     btnTakePhoto.setOnClickListener(this); 

    } 

    @Override 
    public void onBackPressed() { 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      Bitmap bmp = (Bitmap) extras.get("data"); 

      Intent goMain = new Intent(getApplicationContext(), 
        EditUploads.class); 

      ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, bs); 
      goMain.putExtra("byteArray", bs.toByteArray()); 
      startActivity(goMain); 
     } 
    } 

    private void sdcard() { 
     setContentView(R.layout.activity_uploads); 

     // Set up an array of the Thumbnail Image ID column we want 
     String[] projection = { MediaStore.Images.Thumbnails._ID }; 
     // Create the cursor pointing to the SDCard 
     cursor = managedQuery(
       MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which 
                       // columns 
                       // to 
                       // return 
       null, // Return all rows 
       null, MediaStore.Images.Thumbnails.IMAGE_ID); 
     // Get the column index of the Thumbnails Image ID 
     columnIndex = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

     GridView gvuploads = (GridView) findViewById(R.id.gvUploads); 
     gvuploads.setAdapter(new ImageAdapter(this)); 

    } 

    private class ImageAdapter extends BaseAdapter { 

     private Context context; 

     public ImageAdapter(Context localContext) { 
      context = localContext; 
     } 

     public int getCount() { 
      return cursor.getCount(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView picturesView; 
      if (convertView == null) { 
       picturesView = new ImageView(context); 
       // Move cursor to current position 
       cursor.moveToPosition(position); 
       // Get the current value for the requested column 
       int imageID = cursor.getInt(columnIndex); 
       // Set the content of the image based on the provided URI 
       picturesView.setImageURI(Uri.withAppendedPath(
         MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" 
           + imageID)); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_XY); 
       picturesView.setPadding(10, 10, 10, 10); 
       picturesView 
         .setLayoutParams(new GridView.LayoutParams(266, 266)); 
      } else { 
       picturesView = (ImageView) convertView; 
      } 
      return picturesView; 
     } 
    } 

    private Cursor cursor; 

    // Column index for the Thumbnails Image IDs. 

    private int columnIndex; 

    @Override 
    public void onClick(View arg0) { 
     switch (arg0.getId()) { 
     case R.id.btnAllShops: 
      Intent iA = new Intent(getApplicationContext(), AllShops.class); 
      startActivity(iA); 
      break; 

     case R.id.btnFavourites: 
      Intent iF = new Intent(getApplicationContext(), Favourites.class); 
      startActivity(iF); 

      break; 

     case R.id.btnUploads: 
      Intent iU = new Intent(getApplicationContext(), Uploads.class); 
      startActivity(iU); 

      break; 

     case R.id.btnSettings: 
      Intent iS = new Intent(getApplicationContext(), 
        SettingsActivity.class); 
      startActivity(iS); 

      break; 

     case R.id.btnBuys: 
      Intent iBuy = new Intent(getApplicationContext(), Buys.class); 
      startActivity(iBuy); 

      break; 

     case R.id.btnTakePhoto: 
      Intent i = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(i, cameraData); 
      break; 
     } 
    } 
} 

i前添加的網格視圖佈局我能夠使用按鈕的功能。但是,插入網格視圖佈局後,我只能滾動網格視圖上的圖像。我做了什麼以便我可以滾動瀏覽圖片並使用按鈕?由於爲什麼我不能按按鈕,當我來到這個頁面?

公共類移動擴展活動{

PhotoDbAdapter ourHelper; 
ImageView grid_item_image; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.mobile); 

    grid_item_image = (ImageView) findViewById(R.id.grid_item_image); 
    PhotoDbAdapter getImage = new PhotoDbAdapter(this); 
    getImage.open(); 
    String getImagepathe = getImage.getImagePath(); 

    Toast.makeText(this, getImagepathe, Toast.LENGTH_LONG).show(); 

    int id = getResources().getIdentifier(getImagepathe, null, null); 

    grid_item_image.setImageResource(id); 

    Intent mobile = new Intent(getApplicationContext(), Uploads.class); 

    startActivity(mobile); 

} 

}

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:focusableInTouchMode="true" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:src="@drawable/sh_uploads" /> 

    <ImageView 
     android:id="@+id/btnTakePhoto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/hbtn_camera" /> 
</LinearLayout> 

<GridView 
    android:id="@+id/gvUploads" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/bottomBar" 
    android:layout_below="@+id/linearLayout1" 
    android:numColumns="3" 
    tools:listitem="@layout/mobile" > 
</GridView> 

<LinearLayout 
    android:id="@+id/bottomBar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/btnAllShops" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1.0" 
     android:contentDescription="@null" 
     android:src="@drawable/sbtn_home" /> 

    <ImageView 
     android:id="@+id/btnFavourites" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1.0" 
     android:contentDescription="@null" 
     android:src="@drawable/sbtn_fave" /> 

    <ImageView 
     android:id="@+id/btnBuys" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1.0" 
     android:contentDescription="@null" 
     android:src="@drawable/sbtn_buys" /> 

    <ImageView 
     android:id="@+id/btnUploads" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1.0" 
     android:contentDescription="@null" 
     android:src="@drawable/sbtn_uploads" /> 

    <ImageView 
     android:id="@+id/btnSettings" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1.0" 
     android:contentDescription="@null" 
     android:src="@drawable/sbtn_settings" /> 
</LinearLayout> 

+0

已將U包含在另一個佈局中,或者可能存在上下文問題。請爲此發佈一些更多的代碼。 – AndroidHacker

+0

這是另一個也具有gridview佈局的類。我如何檢查是否存在上下文問題? – user3042516

+0

調試您的代碼並且想知道代碼停止的地方。 – AndroidHacker

回答

0

如果圖像併入電網,這也許可以幫助:

gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if(v.getId() == btnFavourites.getId()){ //Something happen } } });

+1

發表您的XML佈局是好的,如果你能進一步解釋這是如何工作和大致相當於我想把這行代碼? sorry..i是在推進這些things..Thanks很新 – user3042516

+0

對不起,林新使用的StackOverflow,但你可以試試這個:http://pastebin.com/raw.php?i=RmwCXpeA –

+0

當我運行應用程序,當我點擊頁面時,它只是崩潰。我檢查了logcat,它說使用setonItemclicklistener來代替。這是個好主意嗎? – user3042516

相關問題