2010-10-29 68 views
0

我上顯示時,選擇一個選項卡的項目列表的Android應用程序的工作,則顯示有關的項目這是從列表中選擇詳細信息:圖片不顯示,但TextView的內容是正確的

活性1 TAB1 - (意圖) - >活動2:ITEM1 ITEM2 - (意圖) - >活動3:ImageView1 項目3 TextView1 ... ImageButton1 ...

的細節將被使用ImageViews顯示ImageButtons和TextViews。數據來自數據庫查詢,因此必須動態分配TextView文本和ImageView drawable。

將來自數據庫查詢的正確文本分配給每個視圖。當我在調試器中逐句通過Activity3的onCreate方法中的代碼時,我可以看到添加的所有內容。

但是,只有在選擇ListActivity中的第一項時,纔會顯示drawable。如果我選擇另一個項目,則所有TextView都會顯示正確的信息,但不會爲ImageView或ImageButton顯示繪圖。看起來只有背景正在顯示。

ImageButton drawable不會被動態分配,只有ImageView drawable可以。 ImageButton可繪製設置在與活動關聯的TableLayout XML中。這是一個條目:

<TableRow android:id="@+id/row11" > 
<ImageButton android:id="@+id/phonebutton" 
android:maxHeight="50px" 
android:maxWidth="50px" 
android:background="@drawable/ic_phone" 
android:adjustViewBounds="true"  
style="?android:attr/buttonStyleSmall" 
android:src="@drawable/phoneselector" > 
</ImageButton > 
<TextView android:id="@+id/phonenumber"/> 

(注意 - 我本來android_drawable = 「@繪製/ ic_phone」,然後嘗試添加機器人:背景= 「@繪製/ ic_phone」,終於創造了以下選擇器(phoneselector.xml): - 注完)

如果我再次選擇的第一個項目,其正確的ImageView可繪製與在TextViews正確的文本一起顯示。屬於其他項目的ImageView drawable在Activity2的ListView中顯示沒有問題。

該代碼成功地爲該按鈕設置了一個onClickListener(同樣在Activity3.onCreate中)。如果我點擊應該顯示的地方顯示的內容,則撥打電話號碼。

代碼成功設置按鈕的onClickListener(同樣在DetailActivity.onCreate中)。

以前有沒有人見過類似的東西?我將不勝感激任何幫助弄清楚什麼是錯的。

這裏有更多的細節,因爲我不知道這個問題有使用次數的做:

的活性1擴展TabActivity和設置包含與TabWidget沿的FrameLayout一個TabHost視圖。 Activity1動態創建選項卡。

活性2延伸ListActivity並將內容到它的ListView(通過的setContentView(getListView())。

(在列表項被選擇時ListActivity.onListItemClick被調用),一個新的目的是創建一個包含識別符在一個Bundle中,Activity3啓動。

的開始活動(在Activity3.onCreate ...)設定的內容,以在一個.xml文件定義的(通過的setContentView(R.layout.details.xml一個TableLayout視圖)。

下面是代碼從Activity3.onCreate:

光標C = NULL;

DatabaseHelper myDbHelper = NULL;

的ImageButton按鈕= NULL; 的TextView phoneText = NULL;

公共無效的onCreate(捆綁savedInstanceState){

super.onCreate(savedInstanceState); 
Resources res = getResources(); 
setContentView(R.layout.details); 
Bundle bundle = this.getIntent().getExtras(); 
Integer id = (Integer) bundle.getInt("id"); 
myDbHelper = new DatabaseHelper(this); 
c = myDbHelper.getWinery(id); 
if (c != null) { 
    c.moveToFirst(); 
    ImageView image1 = (ImageView) this.findViewById(R.id.iconimage); 
    String wholeString = c.getString(c.getColumnIndex("PhotoIcon")); 
    if (wholeString != null) { String[] splitString = wholeString.split(".jpg"); 
    String sString = splitString[0]; 
    int path = res.getIdentifier(sString, "drawable", "com.winerytour"); 
    Drawable drawImage1 = this.getResources().getDrawable(path); 
    drawImage1.setVisible(true, true); //added to try to fix problem 
    try { 
     image1.setImageDrawable(drawImage1); 
     image1.refreshDrawableState();); 
     } catch (RuntimeException e) { 
      e.getMessage(); 
     } 
    image1.setAdjustViewBounds(true); } 

    .. set other TextView text in same way ... 

    phoneText = (TextView) this.findViewById(R.id.phonenumber); 
    String phone = c.getString(c.getColumnIndex("Telephone")); 
    phoneText.setText(phone); 
    button = (ImageButton) this.findViewById(R.id.phonebutton); 
    button.setOnClickListener(new ImageButton.OnClickListener() { 
     public void onClick(View v) { 
      performDial(); } }); 
    ((ImageButton) button).refreshDrawableState(); // attempt to fix problem 
} // end c != null 

}

回答

0

content to its ListView (via setContentView(getListView()).不要那樣做。如果您擴展列表活動,則視圖將被構建。如果你不想這樣做,不要'擴展列表視圖

+0

我刪除了該語句。繪圖仍然不顯示,但至少我的代碼更正確。感謝您的建議。 – 2010-10-29 12:35:02

+0

嗯,我不知道你的問題的其餘部分是什麼,因爲它不能很好地格式化你的代碼,而且我在你原來的文章中看不到任何問題。另外,不要這樣做'Integer id =(Integer)bundle.getInt(「id」)' – Falmarri 2010-10-29 17:28:37

+0

將代碼複製到提交框中時,代碼格式正確。 '看到帖子後它被亂碼了。 – 2010-11-01 00:13:09