Cardview onclick开启了一项新活动

时间:2016-03-30 20:28:11

标签: android android-activity onclicklistener android-recyclerview android-cardview

单击一张卡片会打开一个活动,然后点击另一张卡片会打开另一个活动,依此类推。有没有办法只创建一个活动,并识别卡的ID被点击并显示其相应的数据?

2 个答案:

答案 0 :(得分:2)

您可以使用Intent.putExtra发送ID,然后在您的活动中使用Intent.getIntExtra获取该ID,并在活动中提供您的数据

以下是使用ListView向MyActtivity发送ID和索引的示例:

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getApplicationContext(),MyActivity.class);
        intent.putExtra("id",view.getId());
        intent.putExtra("index",position);
        startActivity(intent);
    }
};

你可以在MyActivity中检索它,如下所示:

Intent intent = new Intent(getIntent());
int id = intent.getIntExtra("id",0);
int index = intent.getIntExtra("index",0);

答案 1 :(得分:-1)

public class FirstActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
    }

    public void card(View view) {
        Intent intent = new Intent(FirstActivity.this, ActivitymainActivity.class);
        startActivity(intent);
    }
}