2013-03-23 34 views
0

即時通訊只是試圖創建一個簡單的適配器的列表視圖。 setadapter不起作用。這是我的代碼。我的setcontentview是在這個代碼塊之前,這是oncreate。setadapter for android not working。空的適配器或列表視圖?

 ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>(); 
HashMap<String,String> map = new HashMap<String,String>(); 
map.put("CType", "Alarm"); 
map.put("RType", "Once"); 
map.put("hour", "1"); 
map.put("minute", "1"); 
map.put("second", "30"); 
arraylist.add(map); 
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"}; 
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce, 
     R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds}; 
ListView clocklistview = (ListView)findViewById(R.id.clocklistview); 
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray); 

clocklistview.setAdapter(adapter); 

這是包含我的ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
     android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <ListView 
    android:id="@+id/clocklistview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</ListView></LinearLayout> 

的XML這是我list_clock.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 


    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="3"> 
    <TextView 
     android:id="@+id/clocktextviewalarmtimer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="1" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/clocktextviewrepeatonce" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="1" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/clocktextviewhours" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="0.3" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/clocktextviewminutes" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="0.3" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView 
     android:id="@+id/clocktextviewseconds" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:layout_weight="0.3" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</LinearLayout> 
+0

你有沒有試過這個例子:http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/?另外,如果您在內置適配器時遇到問題,那麼使用baseAdapter實際上並不困難。 – 2013-03-23 19:50:44

+0

你爲什麼認爲setAdapter不起作用?是因爲你在屏幕上看不到任何東西嗎?你可以向我們展示你的ListView的XML佈局嗎?您的ListView高度設置爲match_parent? – satur9nine 2013-03-23 20:08:03

+0

你是什麼意思「設置適配器不工作」,因爲我試過你的代碼,它工作正常 - 除了沒有顯示。 :) – 2013-03-23 20:13:19

回答

1

這裏有一個自定義的適配器的例子。試試這個......

public class ClockListAdapter extends BaseAdapter { 

    ArrayList<HashMap<String, String>> _arrayList; 

    ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) { 
     this._arrayList = arrayList; 
    } 

    @Override 
    public int getCount() { 
     return (_arrayList != null) ? _arrayList.size() : 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     return (_arrayList != null) ? _arrayList.get(position) : null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 

     if (convertView == null) { 
      LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 
      // REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv 
      convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder._textView.setText("set some text here"); 
     return convertView; 
    } 

    public class ViewHolder { 
     // ADD YOUR VIEW(S) vvvvvvvvv 
     TextView _textView; 

     ViewHolder(View v) { 
      // REPLACE WITH YOUR TEXT vvvvvvvvv 
      _textView = (TextView) v.findViewById(R.id.textView1); 
     } 
    } 
}