1,ListView是手机中非常广泛的一种组件
2,ListView于其他AutoComplete、Spinner组件类似,它们都需要一个供显示的列表项,这都需要借助于内容Adapter了,内容Adapter负责提供需要现实的列表项
public class MyListView extends Activity {
private ListView listView;
//private List<String> data = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));
setContentView(listView);
}
private List<String> getData(){
List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
return data;
}
}
demo如下:总是报同一个错误有可能是编辑器的问题了,重启了一下AndroidStuido就好了:
public class MainActivity extends AppCompatActivity {
// private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String json;
// int i=0;
List<String> listS=new ArrayList<String>();
ListView listView=(ListView) findViewById(R.id.list1);
//ArrayAdapter<String> arrayAdapter;
//String[] arr={"松武","八戒","牛魔王"};
//DynamicBean dynamicBean;
Gson gson = new Gson();
json=ReadJsonFile.getJson("dynamic.json",MainActivity.this);
Root root=gson.fromJson(json,Root.class);
for(int i=0;i<root.getDynamic().size();i++){
// root.getDynamic().get(i);
// Toast.makeText(this,root.getDynamic().get(i).getUsername(), Toast.LENGTH_SHORT).show();
listS.add(root.getDynamic().get(i).getUsername());
}
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,listS));
// root.getDynamic().get(4).getUsername();
//Toast.makeText(this,root.getDynamic().get(1).getUsername(), Toast.LENGTH_SHORT).show();
// dynamicBean=gson.fromJson(jsonStr,type);
// dynamicBean.getUsername().length();
//将Java集合转换为json
// String json2 = gson.toJson(List); System.out.println(json2);
}
}