Chait
Chait

Reputation: 677

ListView with Image Adapter

I have a List items in List View.if i click on first item i will start the activit and will show some images.If i click in second list item,i will start another activity and will show some set of images.Here is the List View code. package com.example.per.app;

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 };
    }
}

I need to maintain the ImageAdapater class for List items in view?Because for each List item i want to show different set of images.

Currently i added only Case 0 and I will add Case 1 case2 case3...case 12.So i have to launch 12 activities.in each activity i want to display different set of images.

Upvotes: 0

Views: 936

Answers (1)

ayon
ayon

Reputation: 2180

Your question is not clear. I think it's not necessary to launch 12 activities. You can write just one activity and when you which to that activity from your list item click then , you can just pass a int array of drawables to be used as image set in your next activity.

Here is a sample code that you can try....

In your list Activity you can keep some Arraylist of drawables like this , so for your case you may require 12 such 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);

Then when you click on a list item them then launch your second Activity like this

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

Finally in your second activity , catch the above ArrayList of Integer like this

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

then , you can load these images in a gridview or anything whatever you like.

Upvotes: 1

Related Questions