2015-10-21 110 views
0

我想做一個自定義列表視圖。我沒有收到錯誤,數據正確添加到我的ArrayList中,但屏幕上什麼也看不到。佈局或適配器有問題嗎?自定義列表視圖不工作

這裏是我的列表行佈局:

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


<TextView 
    android:id="@+id/nomtv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp"/> 

<TextView 
    android:id="@+id/desctv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:id="@+id/debuttv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/nomtv" 
    android:layout_toEndOf="@+id/nomtv"/> 

<TextView 
    android:id="@+id/fintv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/desctv" 
    android:layout_toEndOf="@+id/desctv"/> 

</RelativeLayout> 

這裏是我的自定義適配器:

public class ListTacheAdapter extends ArrayAdapter<String> { 

ArrayList<Tache> taches; 
ListView listView = null; 
final Context context; 
View rowView = null; 
TextView nom = null, desc = null, debut = null, fin = null; 

public ListTacheAdapter(Context context, ArrayList<Tache> taches) { 
    super(context, R.layout.row_tache); 
    // TODO Auto-generated constructor stub 
    this.context = context; 
    this.taches = taches; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    rowView = inflater.inflate(R.layout.row_tache, parent, true); 
    nom = (TextView) rowView.findViewById(R.id.nomtv); 
    desc = (TextView) rowView.findViewById(R.id.desctv); 
    debut = (TextView) rowView.findViewById(R.id.debuttv); 
    fin = (TextView) rowView.findViewById(R.id.fintv); 

    nom.setText(taches.get(position).getNom()); 
    desc.setText(taches.get(position).getDescription()); 
    debut.setText(taches.get(position).getDebut()); 
    fin.setText(taches.get(position).getFin()); 

    return rowView;  
} 

} 

我的活動佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.testapp.ShowActivity" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="#D46A6A" 
    android:dividerHeight="1dp"/> 

</RelativeLayout> 

我的活動類:

public class ShowActivity extends Activity { 

ArrayList<Tache> taches; 
ListView listView; 
TacheDAO td = new TacheDAO(this); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show); 

    td.open(); 
    taches = td.getAllTache(); 
    td.close(); 
    ListTacheAdapter lta = new ListTacheAdapter(this, taches); 
    listView = (ListView) findViewById(R.id.list); 

    listView.setAdapter(lta); 
} 

} 

回答

0

我也是個初學者,所以我告訴我想嘗試: 我認爲這個問題是在調用對適配器的onCreate方法超()。嘗試傳遞super(context,-1)ins(超級)(context,R.layout.row_tache)。

讓我知道。

+0

我試過但沒有改變。我使用bluestacks作爲模擬器 –