我想使用戶配置文件像圖像中的一個,但是當我運行該應用程序時,屏幕上沒有任何內容出現,只是出現背景出現了什麼問題? 爲什麼列表視圖項(兩個文本視圖和圖像視圖)不會出現爲什麼屏幕上只顯示背景而沒有任何內容顯示在列表視圖中?
這是我的代碼:
這是第一個xml文件「是profile.xml」
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/color_white" >
<RelativeLayout
android:id="@+id/list_holder" android:paddingTop="20dp"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip" android:layout_marginTop="0.0dip"
android:layout_marginRight="10.0dip" >
<ListView
android:id="@+id/profile_list" android:paddingTop="5dip"
android:paddingBottom="4.0dip" android:scrollbars="none"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginLeft="6.0dip" android:layout_marginRight="6.0dip"
android:cacheColorHint="#00000000" android:divider="@drawable/list_divider" android:dividerHeight="1.0dip" />
</RelativeLayout>
</RelativeLayout>
和這是第二個 「profile_row.xml」
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profile_row" android:layout_width="fill_parent"
android:layout_height="50.0dip">
<LinearLayout
android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1.0">
<ImageView
android:id="@+id/row_image"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_marginRight="20.0dip"
android:layout_weight="0.2"
android:gravity="left|center"
android:scaleType="center"
android:src="@drawable/arrow_left" />
<TextView
android:textSize="14.0sp" android:textColor="#ff000000"
android:gravity="left|center" android:id="@+id/row_data"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:layout_marginLeft="10.0dip" android:layout_weight="0.5" />
<TextView
android:gravity="right|center" android:id="@+id/row_txt"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:layout_marginRight="10.0dip" android:layout_weight="0.4" style="@style/Text.profileRow" />
</LinearLayout>
public class ProfileView extends Activity
{
private List<String> adapterList;
private ListView mList;
private ProfileAdapter mProfileAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
mList = ((ListView)findViewById(R.id.profile_list));
initFixedList();
initAdapter();
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "you are clicked"+position,
Toast.LENGTH_SHORT).show();
}
});
}
private void initFixedList()
{
this.adapterList = new ArrayList<String>();
this.adapterList.add("name");
this.adapterList.add("age");
this.adapterList.add("height");
this.adapterList.add("weight");
this.adapterList.add("Activity level");
}
class ProfileAdapter extends ArrayAdapter<String>
{
Context context;
int layoutResourceId;
private TextView rowData;
private ImageView rowImage;
private TextView rowTxt;
public ProfileAdapter(Context context, int layoutResourceId)
{
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null)
{
row= ProfileView.this.getLayoutInflater().inflate(R.layout.profile_row,parent,false);
this.rowImage = ((ImageView)row.findViewById(R.id.row_image));
this.rowData = ((TextView)row.findViewById(R.id.row_data));
this.rowTxt = ((TextView)row.findViewById(R.id.row_txt));
}
else
row = convertView;
int j;
if ((position != 0) || (getCount() <= 0))
j = 0;
else
j = 1;
int i;
if ((position != getCount() - 1) || (getCount() <= 0))
i = 0;
else
i = 1;
RelativeLayout localRelativeLayout = (RelativeLayout)row.findViewById(R.id.profile_row);
if (j == 0)
{
if (i == 0)
localRelativeLayout.setBackgroundResource(R.drawable.row_gradient_selector);
else
localRelativeLayout.setBackgroundResource(R.drawable.row_gradient_bottom_selector);
}
else
localRelativeLayout.setBackgroundResource(R.drawable.row_gradient_top_selector);
switch (position)
{
case 0:
this.rowData.setText(StaticPreferences.getName(this));
break;
case 1:
this.rowData.setText(StaticPreferences.getAge(this));
break;
case 2:
this.rowData.setText(StaticPreferences.getHeight(this));
break;
case 3:
this.rowData.setText(StaticPreferences.getWeight(this));
break;
case 4:
this.rowData.setText(StaticPreferences.getActivityLevel(this));
break;
}
this.rowImage.setImageResource(R.drawable.arrow_left);
this.rowTxt.setText((CharSequence)ProfileView.this.adapterList.get(position));
return row;
}
}
私人無效initAdapter(){
runOnUiThread(new Runnable()
{
public void run()
{
ProfileView.this.mProfileAdapter = null;
ProfileView.this.mProfileAdapter = new ProfileView.ProfileAdapter(ProfileView.this,R.layout.profile_row);
ProfileView.this.mList.setAdapter(ProfileView.this.mProfileAdapter);
ProfileView.this.mProfileAdapter.notifyDataSetChanged();
}
});
}}
我不明白它爲什麼會是空的,但如果您在'onCreate'中調用'initAdapter()',則不需要'runOnUiThread()',因爲您已經在那裏的UI線程中。 – zapl 2012-03-12 19:05:32