2013-10-05 42 views
0

我在列表視圖中有一個列表項目,如果我點擊第一個項目,我將開始活動,並會顯示一些圖像。如果我點擊第二個列表項目,我將開始另一個活動,並會顯示一些一組圖像。這裏是列表視圖代碼。 package com.example.per.app;帶圖像適配器的ListView

public class AlbumListActivity extends Activity { 

    public String[] mListInfo = { "1st Month", "2nd Month", "3rd Month", 
      "4th Month", "5th Month", "6th Month", "7th Month", "8th Month", 
      "9th Month", "10th Month", "11th Month", "12th Month" }; 
    public ListView mList = null; 
    public Intent mLaunch = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list_album); 
     mList = (ListView) findViewById(R.id.list_view); 
     ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(
       getApplicationContext(), android.R.layout.simple_list_item_1, 
       mListInfo); 
     mList.setAdapter(mAdapter); 
     mList.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 

       switch (arg2) { 
       case 0: 
        mLaunch = new Intent(getApplicationContext(),FirstMonthActivity.class); 
        startActivity(mLaunch); 
       } 
      } 

     }); 

    } 
    public class ImageAdapter extends BaseAdapter { 
     private Context mContext; 

     public ImageAdapter(Context c) { 
      mContext = c; 
     } 

     public int getCount() { 
      return mThumbIds.length; 
     } 

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

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

     // create a new ImageView for each item referenced by the Adapter 
     public View getView(int arg0, View arg1, ViewGroup arg2) { 
      ImageView imageView; 
      if (arg1 == null) { // if it's not recycled, initialize some 
           // attributes 
       imageView = new ImageView(mContext); 
       imageView.setLayoutParams(new GridView.LayoutParams(363, 363)); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       imageView.setPadding(0, 0, 0, 0); 
      } else { 
       imageView = (ImageView) arg1; 
      } 
      imageView.setImageResource(mThumbIds[arg0]); 
      return imageView; 
     } // references to our images 

     Integer[] mThumbIds = { R.drawable.sample0, R.drawable.sample1, 
       R.drawable.sample2, R.drawable.sample3, R.drawable.sample4, 
       R.drawable.sample5, R.drawable.sample6, R.drawable.sample7, 
       R.drawable.sample8, R.drawable.sample9 }; 
    } 
} 

我需要保持在視圖列表項ImageAdapater類?由於每個列表項我想顯示不同的圖像集。

目前我只添加案例0,我會添加案例1案例2案例3 ...案例12.所以我必須推出12個activities.in每個活動中我想顯示不同的圖像集。

回答

1

你的問題不清楚。我認爲沒有必要開展12項活動。你可以只寫一個活動,當你從你的列表項中點擊那個活動時,你可以傳遞一個int型數組作爲你的下一個活動中的圖像集。

這裏是一個示例代碼,你可以嘗試....

在列表的活動,你可以保持可繪製的一些ArrayList中就是這樣,所以你的情況可能需要12個這樣的ArrayList。

ArrayList<Integer> thumb1list = new ArrayList<Integer>(); 
     thumb1list.add(R.drawable.ic_launcher); 
     thumb1list.add(R.drawable.ic_launcher); 
     thumb1list.add(R.drawable.ic_launcher); 

然後當你點擊一個列表項它們然後啓動你的第二個活動是這樣

Intent intent = new Intent(MainActivity.this , Second.class); 
     intent.putIntegerArrayListExtra("key", thumb1list); 
     startActivity(intent); 

最後,在你的第二個活動,捉整數以上的ArrayList這樣

ArrayList<Integer> Array = getIntent().getExtras().getIntegerArrayList("key"); 

然後,你可以加載這些圖像在gridview或任何你喜歡的任何東西。

+0

我在List.I中有12個項目,在res文件夾中有120個圖像。對於每個List項目,我想顯示10個圖像。如何做到這一點? – chait

+0

看到我編輯的答案,並讓我知道你是否有任何問題。 – ayon

+0

所以在我的代碼中,我使用ImageAdaper類來加載圖像。所以這個類是不需要的?請參閱上面的代碼。 – chait