在另一个活动中调用ListItem值/变量

时间:2018-03-07 14:52:55

标签: java android

在图像中我有一个标志列表视图(例如)。当我在第二个活动中单击其中一个时,我得到该标志的文本名称和媒体图像。一切正常,我想要的。但是现在在第二个活动中,当我点击图像时,我想在第3个活动中看到相应的大图,但我不能这样做。我该怎么做?

activitys screenshots

public class MainActivity extends AppCompatActivity
{
ListView mListView;

String [] Names = {"France", "Germany", "Italy", "USA"};
int [] Small = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};
int [] Medium = {R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4};
int [] Big = {R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4};

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

    mListView = (ListView) findViewById(R.id.listview);
    MyAdapter myAdapter = new MyAdapter(MainActivity.this, Names, Small);
    mListView.setAdapter(myAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent mIntent = new Intent(MainActivity.this, SecondActivity.class);
            mIntent.putExtra("flagName", Names[i]);
            mIntent.putExtra("flagSmall", Small[i]);
            mIntent.putExtra("flagMedium", Medium[i]);
            mIntent.putExtra("flagBig", Big[i]);
            startActivity(mIntent);

        }
    });
}

这是如何调用第二个活动上的图像:

public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;

TextView mTextView;
ImageView mImageView;

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

    mImageView = (ImageView) findViewById(R.id.imageView);
    mTextView = (TextView) findViewById(R.id.textView);

    Bundle mBundle = getIntent().getExtras();
    if (mBundle != null)
    {
        mImageView.setImageResource(mBundle.getInt("flagMedium"));
        mTextView.setText(mBundle.getString("flagName"));
    }

1 个答案:

答案 0 :(得分:0)

我的简单解决方案 mIndex

public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;

TextView mTextView;
ImageView mImageView;
int mIndex;

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

mImageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);

Bundle mBundle = getIntent().getExtras();
if (mBundle != null)
{
    mImageView.setImageResource(mBundle.getInt("flagMedium"));
    mTextView.setText(mBundle.getString("flagName"));
    mIndex = mBundle.getInt("flagBig");
}

我已将意图发送给其他活动。

Intent mIntent = new Intent(SecondActivity.this, ThirdActivity.class);
                mIntent.putExtra("fullImg", mIndex);
                startActivity(mIntent);