2012-03-12 74 views
0

我想使用戶配置文件像圖像中的一個,但是當我運行該應用程序時,屏幕上沒有任何內容出現,只是出現背景出現了什麼問題? 爲什麼列表視圖項(兩個文本視圖和圖像視圖)不會出現爲什麼屏幕上只顯示背景而沒有任何內容顯示在列表視圖中?

這是我的代碼:

這是第一個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; 
    } 

}

私人無效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(); 
    } 
}); 

}}

+0

我不明白它爲什麼會是空的,但如果您在'onCreate'中調用'initAdapter()',則不需要'runOnUiThread()',因爲您已經在那裏的UI線程中。 – zapl 2012-03-12 19:05:32

回答

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> 
+0

我試過你說的,但問題仍然存在。屏幕上沒有任何顯示 – user 2012-03-12 19:22:21

+0

我看到您正在嘗試在開關盒中設置文本。您可以嘗試添加Log.i()語句以確保staticPreferences.getName(this)返回數據。 – dcanh121 2012-03-12 19:29:08

+0

好的,但爲什麼圖像視圖和適配器列表字符串不出現在屏幕上? – user 2012-03-12 19:31:21

1

你需要重寫ArrayAdapter的getCount方法,所以你可以在你的適配器指定的記錄數。目前,getCount方法將返回0,並在頁面上顯示0條記錄。此外,您應該使用BaseAdapter,因爲您沒有使用ArrayAdapter,因爲它是用來設計的。

相關問題