将项目添加到Listview Android?

时间:2015-02-15 18:05:04

标签: android listview android-listview

我正在尝试向ListView添加项目: 正确的吗?

public class Main extends Activity {
    ArrayAdapter <String> listAdapter ;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list = (ListView)findViewById(R.id.listView);

    String[] S= new String[]{"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali"};
    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(S) );


    listAdapter = new ArrayAdapter<String>(this,R.layout.main,planetList);

    list.setAdapter(listAdapter);
}

1 个答案:

答案 0 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main); // Main layout
     String[] s = {"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-  Ali","YA ali","YA-Ali"};
     list = (ListView)findViewById(R.id.listView); // Your activity should have listview with id listView
     ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); //android.R.layout.simple_list_item_1 is built-in xml file
     list.setAdapter(listAdapter);

}