I am wondering if it is possible to put a list of TextView inside a Listview in Android. I tried this code but the result does not give me the right text wanted. I want to display 20 toto for testing.
Here my Main.class
public class MainActivity extends Activity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listcontacts);
// Instanciating an array list (you don't need to do this,
// you already have yours).
List<TextView> contacts = new ArrayList<TextView>();
for(int i = 0 ; i< 20; i++)
{
TextView toto = new TextView(this);
toto.setText("toto");
contacts.add(toto);
}
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(
this,
android.R.layout.simple_list_item_1,
contacts );
listview.setAdapter(arrayAdapter);
}
Then the XML file of main
<ListView
android:id="@+id/listcontacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
Should I need to create a TextView inside my listview and call "findViewById(TextView)" in my class?
Thanks.
Aucun commentaire:
Enregistrer un commentaire