我想使用户配置文件像图像中的一个,但是当我运行该应用程序时,屏幕上没有任何内容出现,只是出现背景出现了什么问题? 为什么列表视图项(两个文本视图和图像视图)不会出现为什么屏幕上只显示背景而没有任何内容显示在列表视图中?
这是我的代码:
这是第一个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