繁体   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