开发者

Images don't display but TextView contents is correct

开发者 https://www.devze.com 2023-01-23 02:24 出处:网络
I am working on an Android application that displays a list of items when a tab is selected, then displays detail information about an item that was selected from the list :

I am working on an Android application that displays a list of items when a tab is selected, then displays detail information about an item that was selected from the list :

Activity1 Tab1 -(intent)-> Activity 2: item1 item2 -(intent)-> Activity 3: ImageView1 item3 TextView1 ... ImageButton1 ...

The details are to be displayed using ImageViews, ImageButtons, and TextViews. The data comes from a database query, so the TextView text and ImageView drawables have to be assigned dynamically.

The correct text from the database query gets assigned to each of the Views. I can see everything being added when I step through the code in the onCreate method of Activity3 in the debugger.

BUT, the drawables are only displayed when I select the first item in the ListActivity. If I select another item, all TextViews display the correct information but the drawables are not displayed for the ImageViews or ImageButtons. It looks like only the backgrounds are being displayed.

The ImageButton drawables are not being dynamically assigned, only the ImageView drawables are. The ImageButton drawables are set in the TableLayout XML that is associated with the activity. Here is one entry :

 <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"/>

(note - Originally, I had android_drawable="@drawable/ic_phone", then tried adding android:background="@drawable/ic_phone", and finally created the following selector (phoneselector.xml) : - end note)

If I select the first item again, its proper ImageView drawables are displayed along with the proper text in the TextViews. The ImageView drawables that belong to the other items have no problems being displayed in Activity2's ListView.

The code successfully sets an onClickListener for the button (also in Activity3.onCreate). If I click on what is displayed on the spot where the drawable sh开发者_StackOverflow社区ould be, the phone number is dialed.

The code successfully sets an onClickListener for the button (also in DetailActivity.onCreate).

Has anyone seen anything like this before? I would appreciate any help figuring out what is wrong.

Here are more details because I wonder if the problem has to do with the use of the Views :

The Activity1 extends TabActivity and sets a TabHost view that contains a FrameLayout along with the TabWidget. Activity1 creates the tabs dynamically.

Activity2 extends ListActivity and sets the content to its ListView (via setContentView(getListView()).

When ListActivity.onListItemClick is invoked (item in list is selected), a new Intent is created that contains an identifier in a Bundle and Activity3 is started.

The started activity (in Activity3.onCreate...) sets the content to a TableLayout view defined in a .xml file (via setContentView(R.layout.details.xml).

Here is the code from Activity3.onCreate :

Cursor c = null;

DatabaseHelper myDbHelper = null;

ImageButton button = null; TextView phoneText = null;

public void onCreate(Bundle 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

}


content to its ListView (via setContentView(getListView()). Don't do that. If you extend listactivity, the view is built it. If you don't want that, don't 'extend listview

0

精彩评论

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