为什么在将列表信息显示为列表视图时,屏幕上只显示背景?

时间:2012-03-12 19:00:43

标签: java android

我想让用户个人资料像图片中的那个,但是当我运行应用程序时屏幕上没有任何内容只显示背景出现问题是什么? 为什么没有出现列表视图项(两个文本视图和图像视图)

这是我的代码:

这是第一个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>

enter image description here

       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;
   }

}

private void 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();
   }
 });

} }

2 个答案:

答案 0 :(得分:1)

您需要覆盖ArrayAdapter中的getCount方法,以便指定适配器中的记录数。目前,getCount方法将返回0并在页面上显示0条记录。此外,您应该使用BaseAdapter,因为您没有使用ArrayAdapter,因为它被设计为使用。

答案 1 :(得分:0)

每次要加载ListView时都不需要设置适配器。 检查listview是否已有适配器并调用notifyDataSetChanged()。

if(ProfileView.this.mList.getAdapter() == null){
    ProfileView.this.mList.setAdapter(ProfileView.this.mProfileAdapter);
}
else{
     ProfileView.this.mProfileAdapter.notifyDataSetChanged();
}

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

   <ImageView
        android:id="@+id/row_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5.0dip"
        android:layout_alignParentRight="true"
        android:gravity="left|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_alignParentLeft="true" android:layout_height="fill_parent" 
        android:layout_width="wrap_content" android:layout_marginLeft="5dip"/>

     <TextView 
        android:gravity="right|center" android:id="@+id/row_txt" 
        android:layout_width="wrap_content" android:layout_height="fill_parent" 
        android:layout_centerInParent="true"/>

</RelativeLayout>