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 sh开发者_运维技巧ould 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.
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));
}
}
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);
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));
精彩评论