开发者

Passing data through Intents from a class to another class and can't catch it?

I've two String Arrays like that ,

String[] htmlArray = {
      "file:///android_asset/Pinocchio/OPS/chapter-001.xml",
      "file:///android_asset/Pinocchio/OPS/chapter-002.xml",

      "file:///android_asset/Pinocchio/OPS/chapter-035.xml",
      "file:///android_asset/Pinocchio/OPS/chapter-035.xml" 
};

String[] htmlArray1 = {
      "file:///android_asset/Harry_Potter/OEBPS/part1.xhtml",
      "file:///android_asset/Harry_Potter/OEBPS/part2_split_000.xhtml",

      "file:///android_asset/Harry_Potter/OEBPS/part18_split_000.xhtml",
      "file:///android_asset/Harry_Potter/OEBPS/part18_split_001.xhtml", 
};

then, I put two ImageView in another class,

private void init() {
    pino_cover = (ImageView) findViewById(R.id.pino_cover);
    pino_cover.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent reader=new Intent(instance,ReaderScreen.class);
        reader.putExtra("pino",true);
        startActivity(reader);
      }
    });

    harry_cover=(ImageView) findViewById(R.id.harry_cover);
    harry_cover.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent reader=new Intent(instance,ReaderScreen.class);
        reader.putExtra("harry",true);
        startActivity(reader);
      }
    });
  }

Then, if I click the Pino Image, I could get the data through htmlArray .

Intent i=getIntent();
      Bundle b = i.getExtras();
       String newText = b.
       String setext=b.getString("harry");
      if (newText=="pino")    
        pages = htmlArray;    
      else
        pages = h开发者_Go百科tmlArray1;

but if I click the Harry Image, it'd been taken to get the data through the htmlArray too. I want to get htmlArray1. How could I get ?


You are putting only a boolean to your intent, but you could use .putExtra(mStringArray, htmlArray1); as this method exists to pass arrays through intents...? Plus, to compare two strings in java, you MUST NOT do == but .equals(""). In your case if(newText.equals("harry))...

EDIT Ok, in an easier version, you have that :

Intent i=getIntent();
  Bundle b = i.getExtras();
   String newText = b.
   String setext=b.getString("harry");
  if (newText=="pino")    
    pages = htmlArray;    
  else
    pages = htmlArray1;

replace it by that :

Intent i=getIntent();
  Bundle b = i.getExtras();
   String newText = b.
   String setext=b.getString("harry");
  if (newText.equals("pino"))    
    pages = htmlArray;    
  else
    pages = htmlArray1;

This should logically work.


I think the error lies in your using "==" to compare 2 string as mentioned above. Please use str1.equals(str2) instead of str1 == str2.

And the answer by Sephy suggests you pass the 2 html arrays to the called activity as well. If for some reasons, the called activity can still access the 2 arrays then you can just do as you have done.

Also, in the map you passed, if you use 2 different keys (one is "harry", and one is "pino"), it seems to defeat the purpose. I suggest sth like:

on harry event: i.putExtra("data", harry_html_array)

on pino event: i.putExtra("data", pino_html_array)

Inside the called activity: array = extras.getStringArray("data");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜