import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.alertdialogtest3.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class FrameActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TEST___"," FrameActivity ");
}
/* 退出 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitDialog(FrameActivity.this, R.layout.exitdialog);
}
return false;
}
/* 自定义的Dialog */
public Dialog exitDialog(final Context context, int layoutId) {
final Dialog dialog = new Dialog(context, R.style.NoTitleDialog);
View layout = LayoutInflater.from(context).inflate(layoutId, null);
//
/* 确定 */
Button confirmbutton = (Button) layout.findViewById(R.id.confirmdialog);
confirmbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.exit(0);
}
});
/* 取消 */
Button cancelbutton = (Button) layout.findViewById(R.id.canceldialog);
cancelbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
// add:
ListView listView1=(ListView) layout.findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.dialog_listitem, new String[] {"type" },
new int[] { R.id.textView1 });
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener_Ex());
dialog.setContentView(layout);
dialog.show();
return dialog;
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
list=new ArrayList<Map<String, Object>>();
map = new HashMap<String, Object>();
map.put("type", "1 test");
list.add(map);
map = new HashMap<String, Object>();
map.put("type", "2 test");
list.add(map);
map = new HashMap<String, Object>();
map.put("type", "3 test");
list.add(map);
return list;
}
class OnItemClickListener_Ex implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long item_line_id) {
Log.d("TEST___"," @@is "+ position);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="17dp"
android:layout_marginTop="135dp"
android:layout_toRightOf="@+id/textView1" >
</ListView>
</RelativeLayout>
dialog_listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
exitdialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="17dp"
android:layout_marginTop="135dp"
android:layout_toRightOf="@+id/textView1" >
</ListView>
</RelativeLayout>