Reputation: 13
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.
Upvotes: 1
Views: 76
Reputation: 13
Thank you all for your answers. I found something on internet and tested it. This is exactly I want to do.
"Using a Custom ArrayAdapter" is the solution I use.
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
In this they create a model with 2 String data. Then create the template xml you want to show. And create you adapter to fill data. Fill data.
Upvotes: 0
Reputation: 179
you can create a custom adapter. Or use an String adapter.
List<String> list = new ArrayList<String>();
for(int i = 0; i < 20; i++){
list.add("toto");
}
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
Upvotes: 0
Reputation: 11137
You don't have to create the TextViews
the adapter does that for you. Just create
List<String> contacts = new ArrayList<String>(20);
for(int i = 0 ; i< 20; i++) {
contacts.add("toto");
}
And call
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
contacts );
Upvotes: 0
Reputation: 1337
Just change your Array of TextView for Strings:
remove:
List<TextView> contacts = new ArrayList<TextView>();
for(int i = 0 ; i< 20; i++)
{
TextView toto = new TextView(this);
toto.setText("toto");
contacts.add(toto);
}
and:
ArrayAdapter<TextView> arrayAdapter = new ArrayAdapter<TextView>(
this,
android.R.layout.simple_list_item_1,
contacts );
add:
List<String> contacts = new ArrayList<String>();
for(int i = 0 ; i< 20; i++)
{
contacts.add("toto");
}
and:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
contacts );
Upvotes: 0
Reputation: 157437
Should I need to create a TextView inside my listview and call "findViewById(TextView)" in my class?
No you shouldn't. The Adapter is responsible to adapt your dataset to some sort of visualization. ArrayAdapter
, for instance, is using the TextView contained into simple_list_item_1.xml
. In your case, contacts
, should be an ArrayList<String>
and the adapter should Adapter<String>
Upvotes: 1