gridview itmes的顺序打开Activity1和Activity2

时间:2017-12-08 09:50:27

标签: android android-intent android-activity gridview

我正在开展一个项目,我有4项活动,主要活动有Griview我想通过点击第1项打开Activity 1,然后点击第2项打开Activity 2 我想帮助完成这项工作。

CustomAdapter.java:

@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.sample_gridlayout, null);
    holder.os_text =(TextView) rowView.findViewById(R.id.os_texts);
    holder.os_img =(ImageView) rowView.findViewById(R.id.os_images);

    holder.os_text.setText(result[position]);
    holder.os_img.setImageResource(imageId[position]);

    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_SHORT).show();
        }
    });

    return rowView;
}

MainActivity.java:

public class MainActivity extends Activity {

GridView gridview;

public static String[] osNameList = {
        "Android",
        "Android",
        "Android",
        "Android",
        "Android",
        "Android",
        "Android",
        "Android",
};
public static int[] osImages = {
        R.mipmap.android,
        R.mipmap.ios,
        R.mipmap.linux,
        R.mipmap.macos,
        R.mipmap.msdos,
        R.mipmap.symbian,
        R.mipmap.windows10,
        R.mipmap.winxp,};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gridview = (GridView) findViewById(R.id.customgrid);
    gridview.setAdapter(new CustomAdapter(this, osNameList, osImages));
}

}

1 个答案:

答案 0 :(得分:1)

//实施项目点击监听器

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

            switch(position)
            {
            case 1:
    //Intent to open Avtivity 2
Intent in2 = new Intent(YourActivity.this, Activity2);
startActivity(in2);
            break;

            case 2:
    //Intent to open Avtivity 1
Intent in1 = new Intent(YourActivity.this, Activity1);
startActivity(in1);
                break;
                default:
                    Toast.makeText(GinfyActivity.this, "Wrong Input", Toast.LENGTH_LONG).show();
                    }
            }
        });