2014-10-29 104 views
1

我寫了下面的代碼,但我確信我在方法onItemClick()中做了錯誤() 我的列表視圖顯示正確,點擊時另一項活動打開我想只顯示在新的活動 圖像所以需要從你們我想在另一個活動中打開一個圖像,當點擊列表視圖中的項目時

MainActivity

public class MainActivity extends ActionBarActivity { 

String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
     "Saturday", "Sunday" }; 
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, 
     R.drawable.ic_launcher }; 

int[] images1 = { R.drawable.image01, R.drawable.image02, 
     R.drawable.image03, R.drawable.image04, R.drawable.image05, 
     R.drawable.image06, R.drawable.image07 }; 
ListView lv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lv = (ListView) findViewById(R.id.listView1); 

    ArjAdapter adapter = new ArjAdapter(getBaseContext(), days, images); 

    lv.setAdapter(adapter); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 

      File imgfile = new File(""); 
      if (imgfile.exists()) { 
       String myBitmap = imgfile.getAbsolutePath(); 
       Intent intent = new Intent(MainActivity.this, 
         ImageActivity.class); 
       intent.setType("image/*"); 
       intent.putExtra("img", "images1"); 
       startActivity(intent); 
      } 
     } 
    }); 

在上述方法的一些幫助,我不知道該文件的對象中包括的內容,這種方法我看到了某處,但我不確定它裏面包含什麼,所以留下了空白ķ從你們

ImageActivity,其中我想顯示圖像

public class ImageActivity extends Activity { 
ImageView iv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.images); 

    Bitmap myBitmap = BitmapFactory.decodeFile(getIntent().getStringExtra(
      "img")); 

    ImageView iv = (ImageView) findViewById(R.id.imageView2); 
    iv.setImageBitmap(myBitmap); 
} 

任何建議,歡迎

+0

你的意思是你要顯示在images1 []陣列右點擊的圖像? – 2014-10-29 12:11:10

+0

7項在列表視圖和7圖像在我images1 [],當點擊各自的項目時,相應的圖片應顯示在我的ImageActivity,是 – 2014-10-29 12:13:32

+0

我發表答案只是試圖讓我知道會發生什麼 – 2014-10-29 12:21:10

回答

1

你應該寫onListItemClick()這樣

lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 

       Intent intent = new Intent(MainActivity.this, 
         ImageActivity.class); 
       intent.putExtra("img", images1[position]); 
       startActivity(intent); 


      } 
     }); 

ImageActivity.java

public class ImageActivity extends Activity { 
    ImageView iv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.images); 


     ImageView iv = (ImageView) findViewById(R.id.imageView2); 
     iv.setImageResource(getIntent().getIntExtra("img", 0)); 
    } 
} 

OUTPUT:

enter image description here

+0

先生只是一個空白的活動開放 – 2014-10-29 12:26:04

+0

你粘貼的代碼,因爲它是我給的? – 2014-10-29 12:30:19

+0

是的先生,我甚至在我的清單文件中聲明 2014-10-29 12:36:46

0

這裏你沒有定義圖像的任何位置進行磋商。定義位置字符串並放入文件並檢查它。

String location="storage/img1.jpg"; 

File imgfile = new File(location); 
+0

先生我不想顯示我想要顯示7個不同的圖像,當點擊相應的項目相應,這就是爲什麼我已初始化的圖像數組,並將圖像放置在可繪製的文件夾內,所以根據你,如果我想通過文件訪問圖像,如何我應該指定位置? – 2014-10-29 12:18:34

相關問題