我正在开发一个简单的应用程序,它由一个 ListView 和一个按钮组成。我看不到 ListView,但该按钮在我的活动中可见。我不知道发生了什么。请帮帮我。
我的 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Add_Contact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Contact" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
我的 MainActivity.java
公共类 MainActivity 扩展 Activity {
Button add_Contact;
ListView selectedList;
private ArrayList<SelectedListModel> selectedArrayList = new ArrayList<SelectedListModel>();
List<Map<String, String>> dataList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataList = new ArrayList<Map<String, String>>();
add_Contact = (Button)findViewById(R.id.Add_Contact);
selectedList = (ListView)findViewById(R.id.listView1);
add_Contact.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(MainActivity.this,SecondActivity.class);
//in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(in, 1);
}
});
//SimpleAdapter adpt = new SimpleAdapter(this, dataList, R.layout.simple, new String[]{"name","number"},new int[]{R.id.textView1, R.id.textView2});
//selectedList.setAdapter(adpt);
selectedList.setAdapter(new SimpleAdapter(this, dataList, android.R.layout.two_line_list_item, new String[] {"name", "number"}, new int[] {android.R.id.text1, android.R.id.text2}));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
selectedArrayList = data.getParcelableArrayListExtra("result");
System.out.println("activity result called4 :"+selectedArrayList.size());
Iterator<SelectedListModel> it = selectedArrayList.iterator();
while (it.hasNext()){
SelectedListModel selectedModel = (SelectedListModel)it.next();
String name1 = selectedModel.getName();
String number1 = selectedModel.getNumber();
Map<String, String> dict = new HashMap<String, String>(2);
dict.put("name", name1);
dict.put("number", number1);
dataList.add(dict);
}
}
}
} }