简体   繁体   中英

Custom listview don't work

I'm trying to make a custom listview. I'm not getting error and data are correctly added to my ArrayList but I see nothing on screen. Is there a problem with a layout or the adapter ?

Here is my list row layout:

<?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>

Here is my custom adapter:

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;    
}

}

My activity layout:

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

And my activity class:

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);
}

}

I'm also a beginner, so i tell what i'd try: I believe that the problem is in the calling to super() on the onCreate method of the adapter. Try passing super(context, -1) insted of super(context, R.layout.row_tache).

Let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM