2014-02-05 73 views
0

我有一个显示布局与两个项目其中之一是imageView和另一个是TextView.I'm使用此布局显示很多东西,但有一个问题。问题是让我们说我跑一个活动使用这种布局这个活动填充它并杀死自己。后来如果我打电话给另一个活动使用该布局它改变图像,但文字仍然same.I不想发布所有的代码,但如果你们需要我可以发布它。Android TextView文本更改错误

setContentView(R.layout.info_display_tablet); 
    iv = (ImageView) findViewById(R.id.imageView1); 
    tx = (TextView) findViewById(R.id.textView1); 
    try { 
     result = MethodInfoGetter.methodRequest("CTVIslemleriGetir", "", ""); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    tx.setText(""); 
    iv.setImageResource(R.drawable.cevre_temizlik); 
    tx.setText(result.get(0).get(1).toString()); 

这是我的第一个电话让我们asume的第二呼叫的代码是

public class BalikesirTarih extends Activity 
{ 
ArrayList<ArrayList> result = new ArrayList<ArrayList>(); 
ImageView iv; 
TextView tx; 
Bitmap bmp = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info_display_tablet); 
    iv = (ImageView) findViewById(R.id.imageView1); 
    tx = (TextView) findViewById(R.id.textView1); 
    try { 
     result = MethodInfoGetter.methodRequest("BalikesirTarihiGetir", "", ""); 
     Log.i("Balikesir tarihi log denemesi",result.toString()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    new asTask().execute(); 
} 

private class asTask extends AsyncTask<Void , Void ,Void> { 
    @Override 
    protected Void doInBackground(Void... params) { 

     URL url = null; 
     try { 
      url = new URL("http://balikesir.bel.tr/Balikesir_Tarihi/Resim_b_1.jpg"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     try { 
      bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     iv.setImageBitmap(bmp); 
     tx.setText(""); 
     tx.setText(result.get(0).get(1).toString()); 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    finish(); 
} 
} 
+0

你不必发布你所有的代码,但没有相关的位,我们不会能够帮助你。 –

+2

每次加载时,文本将作为'layout.xml'文件中的默认文本,除非您保存该值并在膨胀'layout时特别设置它。我认为你需要提供*一些*代码和/或更好的解释,因为这很混乱。 – codeMagic

+0

这是我在程序中调用的代码。 –

回答

0

您不能使用文本视图,从一个活动传递到另一个数据。

这样做的一种方法是额外的。当您打开下一个活动,你可以这样做:

Intent intent = /*create intent for opening the next activity*/; 
    intent.addExtra("textLabel", tx.getText()); 
    startActivity(intent); //or however you are opening next activity 

然后在接下来的活动:

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info_display_tablet); 
    iv = (ImageView) findViewById(R.id.imageView1); 
    tx = (TextView) findViewById(R.id.textView1); 

    String passedTextLabel = getIntent.getExtra("textLabel",""); 
    tx.setText(passedTextLabel); 
    .... 

} 

但是,如果你的第一个活动是不打开的第二个活动的活动,你可能有一个更容易的时间存储在一个单身的数据。因此,在你的第一个活动,当你得到的数据:

tx.setText(result.get(0).get(1).toString()); 
MySingleton.instance().setTextLabel(result.get(0).get(1).toString()); 

然后在接下来的活动:

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info_display_tablet); 
    iv = (ImageView) findViewById(R.id.imageView1); 
    tx = (TextView) findViewById(R.id.textView1); 

    tx.setText(MySingleton.instance().getTextLabel()); 
    .... 
+0

我有不同种类的数据,我不想传递任何东西,我只想每次将布局文本更改为新数据。 –

+0

然后它仍然不清楚你想要什么。从你发布的代码中,你没有想到会发生什么错误? – Tenfour04

+0

好吧,我有一个布局和第一个代码,我用它来显示一些东西和活动,当我按下后退按钮,然后我开始另一个活动来显示另一个数据,但我无法删除旧数据这是我的问题。 –

0

从你的意思是什么,以及我的理解,我认为你是在布局xml上硬编码文本。
如果您需要为textView保留一些值,然后查看首选项API,那么您可以存储您的上一个值并在将活动加载后重置它。
如果这不是你想要做的,请更具体(给出一个例子,并向我们展示你的布局xml)