簡體   English   中英

自定義列表視圖不起作用

[英]Custom listview don't work

我正在嘗試制作自定義列表視圖。 我沒有收到錯誤,並且數據已正確添加到我的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);
}

}

我還是一個初學者,所以我告訴我要嘗試的方法:我相信問題出在適配器的onCreate方法上對super()的調用。 嘗試傳遞由super(context,R.layout.row_tache)插入的super(context,-1)。

讓我知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM