【发布时间】:2010-10-29 03:24:16
【问题描述】:
我正在开发一个 Android 应用程序,该应用程序在选择选项卡时显示项目列表,然后显示有关从列表中选择的项目的详细信息:
活动1 Tab1 -(意图)-> 活动 2:item1 item2 -(intent)-> 活动 3:ImageView1 项目 3 文本视图 1 ...图像按钮1 ...
将使用 ImageViews、ImageButtons 和 TextViews 显示详细信息。数据来自数据库查询,因此必须动态分配 TextView 文本和 ImageView 可绘制对象。
来自数据库查询的正确文本被分配给每个视图。当我在调试器中单步执行 Activity3 的 onCreate 方法中的代码时,我可以看到正在添加的所有内容。
但是,仅当我选择 ListActivity 中的第一项时才会显示可绘制对象。如果我选择另一个项目,所有 TextViews 都会显示正确的信息,但不会为 ImageViews 或 ImageButtons 显示可绘制对象。似乎只显示了背景。
ImageButton 可绘制对象不是动态分配的,只有 ImageView 可绘制对象是。 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="@drawable/ic_phone",然后尝试添加 android:background="@drawable/ic_phone",最后创建了以下选择器 (phoneselector.xml): - 尾注)
如果我再次选择第一项,其正确的 ImageView 可绘制对象将与正确的文本一起显示在 TextViews 中。属于其他项目的 ImageView 可绘制对象在 Activity2 的 ListView 中显示没有问题。
代码成功地为按钮设置了一个 onClickListener(也在 Activity3.onCreate 中)。如果我点击drawable应该在的地方显示的内容,就会拨打电话号码。
代码成功为按钮设置了 onClickListener(也在 DetailActivity.onCreate 中)。
以前有人见过这样的事情吗?我将不胜感激任何帮助找出问题所在。
这里有更多细节,因为我想知道问题是否与视图的使用有关:
Activity1 扩展了 TabActivity 并设置了一个 TabHost 视图,该视图包含一个 FrameLayout 以及 TabWidget。 Activity1 动态创建选项卡。
Activity2 扩展 ListActivity 并将内容设置到它的 ListView(通过 setContentView(getListView())。
当 ListActivity.onListItemClick 被调用(列表中的项目被选中)时,会创建一个包含 Bundle 中的标识符的新 Intent 并启动 Activity3。
已启动的活动(在 Activity3.onCreate... 中)将内容设置为在 .xml 文件中定义的 TableLayout 视图(通过 setContentView(R.layout.details.xml)。
这是来自 Activity3.onCreate 的代码:
光标 c = null;
DatabaseHelper myDbHelper = null;
ImageButton 按钮 = 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
}
【问题讨论】:
标签: android