将视图从GridView发送到另一个活动

时间:2013-08-22 08:18:16

标签: android gridview android-intent view android-activity

我的主要布局有一个GridView。 GridView包含一些带有ImageView和TextView的GridLayouts。我想要做的是,当我从GridView中选择一个GridLayout时,一个新的Activity在其布局上打开了这个特定的GridLayout。

我的想法是通过Intent将GridLayout的id传递给第二个Activity。这是我到目前为止编写的代码。

//from MainActivity

public final static String USERDATA="com.example.myproject.MESSAGE";

protected void onCreate(Bundle savedInstanceState) {
.
.


 GridView gridview = (GridView) findViewById(R.id.gridView1);
adapter=new ArrayAdapter(this,R.layout.windows_button, R.id.textView1,names);
          gridview.setAdapter(adapter);     
    .
     gridview.setOnItemClickListener(new OnItemClickListener() {


    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                    int viewId=v.getId();
                    sendToLogin(viewId);
                }


            });
}

public void sendToLogin(int viewId){

     Intent intent = new Intent(this,LoginActivity.class);
     intent.putExtra(USERDATA, viewId);
     startActivity(intent);
}


//from second activity

View userdata;//The View that will receive the GridLayout
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);


        //recieving the intent and the information from the gridview list
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.USERDATA);
        userdata=(View)findViewById(R.id.loginView);


    }
  1. 我怎么能这样做?
  2. 如何从GridLayout中隔离另一个视图?(例如GridLayout中的textView)。

1 个答案:

答案 0 :(得分:1)

你试过这个吗?

Intent i = new Intent(this, SecondActivity.class);
Bitmap b = img.getDrawingCache();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("myImage", bs.toByteArray());
startActivity(i);

在下一个Activity中写道

if(getIntent().hasExtra("myImage")) {
    ImageView image = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("myImage"),0,getIntent().getByteArrayExtra("myImage").length);        
    image.setImageBitmap(b);
}

首先查看以下链接到api功能

link

另一种方法 1)

first add listener like below
selection = (TextView) findViewById(R.id.selection);

        GridView gv = (GridView) findViewById(R.id.grid);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1, 
                items );

        gv.setAdapter(aa);
        gv.setOnItemClickListener(this);

2)在项目中单击

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        selection.setText(items[position]);
    }