自定义列表视图无法正常工作

时间:2015-10-21 23:12:03

标签: java android eclipse listview

我正在尝试制作自定义列表视图。我没有收到错误,数据已正确添加到我的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);
}

}

1 个答案:

答案 0 :(得分:0)

我也是初学者,所以我告诉我要尝试的内容: 我相信问题在于对适配器的onCreate方法调用super()。尝试传递超级(context,R.layout.row_tache)的超级(context,-1)。

让我知道。