我正在尝试向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);
}
答案 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);
}