简体   繁体   中英

How can I change an ImageView after a ListActivity?

The idea is to go to a ListActivity, click on an item, and if you clicked on item X it displays an especific image.

That's my Android code.

package vds.cmc;
public class Dae extends ListActivity {
static final String bigfoot[] = {"Boy", "Girl"};

ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new PeopleAdapter(this, bigfoot));
    iv = (ImageView) findViewById(R.id.ivID);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String qlo = bigfoot[position];
    try {
        if (qlo == "Girl") {
            setContentView(R.layout.ppl);
            iv.setImageResource(R.drawable.cw);
        }
        else {
            setContentView(R.layout.ppl);
            iv.setImageResource(R.drawable.no);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

}

How should I do it? Or how do I fix it?

Thank you, I'm pretty new at this.

Spawn off a new activity to view the image.

    Intent intent = new Intent(this, ImageViewerActivity.class);
    intent.putExtra(EXTRA_IMAGE_ID, R.drawable.boy); //could also pass the resource
    startActivity(intent);

and then

public class ImageViewerActivity extends Activity {
    private static final String EXTRA_IMAGE_ID = "ImageViewerActivity.EXTRA_IMAGE_ID";
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras == null) {
        return;
    }

        Resource resId = extras.getInt(EXTRA_IMAGE_ID);
        mImageView.setImageDrawable(resId);
}

You've extended ListActivity , so it means that the activity only has a ListView and nothing more.

You can do any of the next things:

  1. Create a new activity that will show the image.

  2. Use fragments, while one shows the ListView and one shows the image , and switch between them.

  3. Use setContentView twice, one before choosing (to show the ListView) and one after (to show the image).

  4. Use a different layout, which you switch between the ListView and the ImageView (via visibility or via using ViewSwitcher) .

I'm sure there are other ways too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM