0

I am using gridview of layout,its not working after the grid view items in the list,it shows "throwIndexOutOfBoundsException" in logcat error.without onclick function if i use toast its working,if i give an onlcick function for next activity its not working

Gridviewadapter.java

public class GridviewAdapter extends BaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

    public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
        super();
        this.listginfy = listginfy;
        this.listimage = listimage;
        this.activity = (Activity) ginfyActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listginfy.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listginfy.get(position);
    }

    public int getItemId() {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageView imgViewGinfy;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listginfy.get(position));
        view.imgViewGinfy.setImageResource(listimage.get(position));

        return convertView;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


}

Mainactivity.java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_ginfy);

        prepareList();

        // prepared arraylist and passed it to the Adapter class
   mAdapter = new GridviewAdapter(this,listginfy, listimage);

        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {


                mAdapter = results.get(position);

                switch(mAdapter.getItemId())
                {
                case 1:
                Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
                startActivity(newActivity);
                break;

                case 2:
                Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
                startActivity(new1Activity);
                break;
                default:
                    Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }
            }
        });

    }

    public void prepareList()
    {
        listginfy = new ArrayList<String>();

        listginfy.add("Prayers");
        listginfy.add("Poojaroom");


        listimage = new ArrayList<Integer>();
        listimage.add(R.drawable.ginfyprayer);
        listimage.add(R.drawable.poojaroom1);

    }


}

Onclick function of gridview is not working here i post my xml layout also.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/border"
    android:padding="5dp">

    <ImageView
        android:layout_height="64dp"
        android:id="@+id/imageView1"
        android:layout_width="64dp"
        android:src="@drawable/icon"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:clickable="True">
    </ImageView>

    <TextView
        android:text="TextView"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="2dp"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:ellipsize="marquee"></TextView>


</RelativeLayout>

home_ginfy.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <GridView
        android:layout_height="wrap_content"
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:clickable="True">
    </GridView>
</LinearLayout>
4

3 回答 3

1

您的代码中有很多错误..(例如android:clickable="True"在您的布局中>>删除它)

仅用于测试,将您的代码替换为:

// Implement On Item click listener
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {



            switch(position)
            {
            case 1:
            Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
            startActivity(newActivity);
            break;

            case 2:
            Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
            startActivity(new1Activity);
            break;
            default:
                Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                }
        }
    });

适配器的 getItemId 始终返回 0..

于 2013-07-01T10:33:26.537 回答
0

代替

mAdapter = results.get(position);

                switch(mAdapter.getItemId())

经过

switch(position)
于 2013-07-01T10:15:47.617 回答
0

我一直在使用这样的东西。在您的情况下,这应该可以。

public class GridviewAdapter extends BaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

    public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
        super();
        this.listginfy = listginfy;
        this.listimage = listimage;
        this.activity = (Activity) ginfyActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listginfy.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listginfy.get(position);
    }

    public int getItemId() {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageView imgViewGinfy;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    switch (position){
                         case 1:
                              Intent newActivity = new Intent(GinfyActivity.this,MainActivity.class);
                              startActivity(newActivity);
                              break;
                         case 2:
                              Intent new1Activity = new Intent(GinfyActivity.this,AndroidTabLayoutActivity.class);
                              startActivity(new1Activity);
                              break;
                         default:
                              Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }
                }
             }
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listginfy.get(position));
        view.imgViewGinfy.setImageResource(listimage.get(position));

        return convertView;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


}
于 2016-09-08T12:27:30.933 回答