我遇到了以下问题:
我有一个从websocket服务器请求更新的方法:
public Playlist(Activity a) {
this.activity = a;
activity.setContentView(R.layout.player);
KMC.c.send("MyPlayer::CurrentSongList('ID,ArtistName,Title,AlbumName')");
}
以下功能成功收到了结果:
public void UpdateCPL(JSONArray jArray) {
TextView TVti = (TextView) this.activity.findViewById(R.id.title);
TextView TVar = (TextView) this.activity.findViewById(R.id.artist);
TextView TVal = (TextView) this.activity.findViewById(R.id.album);
ImageView IV = (ImageView) this.activity.findViewById(R.id.imageView1);
Log.d("KMC.updateCPL", TVti.toString());
try { //Get the first item - not part of the listview
JSONObject jObject = jArray.getJSONObject(0);
String title = jObject.getString("Title");
Log.d("KMC.updateCPL", title); //works
TVti.setText(title); //does not work
TVar.setText(jObject.getString("ArtistName"));
TVal.setText(jObject.getString("AlbumName"));
IV.setImageResource(R.drawable.noimage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView LV = (ListView) this.activity.findViewById(R.id.listView1);
LV.setAdapter(new listAdapter(this.activity, jArray));
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:textAppearance="?android:textAppearanceLarge"
android:id="@+id/title" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1"
android:text="@string/hello" android:layout_alignTop="@+id/imageView1"
android:layout_alignParentRight="true" />
<TextView android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/artist" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/title"
android:layout_alignLeft="@+id/title" android:layout_alignParentRight="true" />
<TextView android:textAppearance="?android:textAppearanceSmall"
android:id="@+id/album" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/artist"
android:layout_alignLeft="@+id/artist" />
<ListView android:id="@+id/listView1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="@+id/imageView1"
android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" />
<ImageView android:id="@+id/imageView1" android:layout_width="90.0dip"
android:layout_height="90.0dip" android:layout_marginLeft="5.0dip"
android:layout_marginTop="5.0dip" android:layout_marginRight="5.0dip"
android:layout_marginBottom="5.0dip" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
然而,虽然我可以记录结果,但TVti.setText(标题);不起作用,因为文本没有显示。
任何人都知道问题是什么?
编辑:忘记这一点,这不是问题所在。问题是没有从主线程调用UpdateCPL方法。答案 0 :(得分:1)
过去发生这种情况时,原因是文字颜色与背景颜色相同。您可以尝试设置文本颜色和/或背景颜色以查看它是否显示:
TVti.setTextColor(Color.RED);
TVti.setBackgroundColor(Color.WHITE);
答案 1 :(得分:0)
请发布布局然后很容易找到解决方案
根据代码
String title = jObject.getString("Title");
Log.d("KMC.updateCPL", title); //works
TVti.setText(title); //does not work
标题中的某些值意味着它应该正确反映,请通过为艺术家分配标题进行检查,反之亦然。