Thursday, 9 August 2012

GridView (Android)

/// First class
//*************************************************************************//

package com.test.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.android.R;

public class Adapter extends BaseAdapter {
    private Context context;
    private final String[] Values;

    public Adapter(Context context, String[] Values) {
        this.context = context;
        this.Values = mobileValues;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.view, null);

            // set value into textview
            TextView textView = (TextView) gridView.findViewById(R.id.item);
            textView.setText(Values[position]);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.image);

            String value = Values[position];

            if (value.equals("image1")) {
                imageView.setImageResource(R.drawable.image1);
            } else if (value.equals("image2")) {
                imageView.setImageResource(R.drawable.image2);
            } else if (value.equals("image3")) {
                imageView.setImageResource(R.drawable.image3);
            } else {
                imageView.setImageResource(R.drawable.image_default);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return Values.length;
    }

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

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

}
// Second class
//*************************************************************************//

package com.test.android;

import com.test.android.adapter.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;

public class GridView extends Activity {

    GridView gridView;

    static final String[] Arr = new String[] { "image1", "image2","image3"};

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new ImageAdapter(this, Arr));
        gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,int position, long id){
            Toast.makeText(getApplicationContext(),
                ((TextView) v.findViewById(R.id.item))
                    .getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}

No comments:

Post a Comment