Reputation: 23596
I have Data in ArrayList like:
[android_metadata, test1, test5, test4, test2, test3, test10, test1002, abcd, zxy, re, test1001, testing, test21, test12]
Now i want to Set such data in to the ListView in android. Please help me in this matter. I have seen many Example. but should i have to implement getter setter class to store data and display it into ListView ? I have just to show Just above List of data in to List View and set its ClickEvent. Please Give me Some code to done such implement.
Upvotes: 4
Views: 54871
Reputation: 3177
As a complement to Balaji.K's answer, you don't need the String[]
array.
You can attach an ArrayList to a ListView by using an ArrayAdapter directly:
private ArrayList<String> arrayList = Arrays.asList("android_metadata", "test1", "test5", "test4", /*...*/ "test21", "test12");
ListView lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
Upvotes: 0
Reputation: 4829
try this code
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Start extends Activity {
private String[] lv_arr = {};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get a handle to the list view
ListView lv = (ListView) findViewById(R.id.ListView01);
// Convert ArrayList to array
lv_arr = (String[]) arrayList.toArray();
lv.setAdapter(new ArrayAdapter<String>(Start.this,
android.R.layout.simple_list_item_1, lv_arr));
}
}
Upvotes: 14
Reputation: 30855
This is the example
ArrayList yourlist = new ArrayList();
ArrayAdapter adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, yourlist);
yourlistview.setAdapter(adapter);
Upvotes: 6