如何从GridView中的多个项目启动活动

时间:2016-05-04 11:25:48

标签: android

这是我的MainActivity类

public class MainActivity extends AppCompatActivity {

    GridView gridView;
    Context context;
    ArrayList prgmName;
    public static String [] prgmNameList={"GFU","FAB","DRAW","ANIM","ABC","DEF","PHH","XSE","HGY"};
    public static int [] prgmImages={R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,R.drawable.sample_0};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Custom Example for GridView ...
        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new GridCustomAdapter(this, prgmNameList,prgmImages));




        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // displayView(0);
                /*Intent intent = null;
                if (position == 0){
                    intent = new Intent(view.getContext(), ToolbarExample.class);
                }
                if (position == 1){
                    intent = new Intent(view.getContext(), FloatingButtonExample.class);
                }
                startActivity(intent);*/

                switch (position) {
                    case 0:
                        Intent i = new Intent(getApplicationContext(), ToolbarExample.class);
                        i.putExtra("id", position);
                        startActivity(i);
                        break;

                    case 1:
                        Intent j = new Intent(getApplicationContext(), FloatingButtonExample.class);
                        j.putExtra("id", position);
                        startActivity(j);
                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                    default:
                        break;
                }

            }
        });
    }
}

我的Adapter类如下:我想从适配器类开始多个活动,但它不能正常工作

public class GridCustomAdapter extends BaseAdapter {

    String [] result;
    Context context;
    int [] imageId;

    private static LayoutInflater inflater=null;
    public GridCustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
        inflater = (LayoutInflater)context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;

        rowView = inflater.inflate(R.layout.custom_grid_items, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);

        rowView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               /* Intent intent = new Intent(context, ToolbarExample.class);
                context.startActivity(intent);*/

                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_SHORT).show();
            }
        });

        return rowView;
    }
}

1 个答案:

答案 0 :(得分:1)

这里的一个小问题只是从适配器类中删除click方法。请参阅下面的代码更新代码。

public class GridCustomAdapter extends BaseAdapter {

String [] result;
Context context;
int [] imageId;

private static LayoutInflater inflater=null;
public GridCustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=mainActivity;
    imageId=prgmImages;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

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

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.custom_grid_items, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);



    return rowView;
}
}

注意: -

如果您想OnItemClickListener使用GridView,那么您无法在适配器类中使用Click事件或自定义类按钮或视图。