Java AlertDialog SingleChoiceItems不工作
我正在尝试创建一个SingleChoiceItems AlertDialog,其中有一个“确定”和“取消”按钮,该按钮为用户提供了从列表视图长时间单击到1的选项。查看用户配置文件2。向用户发送消息3。从好友列表中删除用户 我还没有完成选项1和2,但我现在正在尝试做选项3,其中包括数据库删除方法和一个toast,上面写着“friend removed”,但当我选择选项3并点击ok时,该行不会被删除,或者我看到toast说用户被删除。这是我正在使用对话框的活动的代码Java AlertDialog SingleChoiceItems不工作,java,android,eclipse,android-alertdialog,Java,Android,Eclipse,Android Alertdialog,我正在尝试创建一个SingleChoiceItems AlertDialog,其中有一个“确定”和“取消”按钮,该按钮为用户提供了从列表视图长时间单击到1的选项。查看用户配置文件2。向用户发送消息3。从好友列表中删除用户 我还没有完成选项1和2,但我现在正在尝试做选项3,其中包括数据库删除方法和一个toast,上面写着“friend removed”,但当我选择选项3并点击ok时,该行不会被删除,或者我看到toast说用户被删除。这是我正在使用对话框的活动的代码 public class Pla
public class PlayAFriend extends ListActivity {
DBAdapter DBAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_items);
final DBAdapter db = new DBAdapter(this);
DBAdapter = db.open();
ListView FriendLV = (ListView) findViewById(android.R.id.list);
final Cursor friendslist = db.GetAllFriends();
String[] from = new String[] { "FRIENDS" }; // your column/columns here
int[] to = new int[] { R.id.textview_friends };
@SuppressWarnings("deprecation")
ListAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.list_items, friendslist, from, to, 0);
FriendLV.setAdapter(cursorAdapter);
FriendLV.setLongClickable(true);
FriendLV.setOnItemLongClickListener(new OnItemLongClickListener() {
class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, friendslist, flags);
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
LayoutInflater inflater;
@Override
public void bindView(View view, Context context,
Cursor friendslist) {
// TODO Auto-generated method stub
int row_id = ((com.fullfrontalgames.numberfighter.DBAdapter) friendslist)
.get("_id");
}
@Override
public View newView(Context context, Cursor friendslist,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_items, parent,
false);
bindView(v, context, friendslist);
return v;
}
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
showDialog(arg2);
return false;
}
});
}
public void OnButtonClick(View view) {
startActivity(new Intent(
"com.fullfrontalgames.numberfighter.Fightattacker"));
}
String[] items = { "View Profile", "Send Message", "Remove Friend" };
@Override
public Dialog onCreateDialog(final int id) {
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select an Option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
// TODO Auto-generated method stub
}
}
)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
// TODO Auto-generated method stub
if (choice == 0) {
Toast.makeText(getBaseContext(),
"Test", Toast.LENGTH_SHORT)
.show();
} else if (choice == 1) {
Toast.makeText(getBaseContext(),
"Test2", Toast.LENGTH_SHORT)
.show();
} else if (choice == 2) {
Toast.makeText(getBaseContext(),
"Friend Removed",
Toast.LENGTH_SHORT).show();
final TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends
.getText().toString();
db.DeleteFriends(deletedfriend);
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
}
})
.create();
}
return null;
}
@Override
protected void onDestroy() {
super.onDestroy();
// Clear the database
DBAdapter.close();
}
}
公共类PlayAFriend扩展ListActivity{
DBAdapter DBAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.list_项);
最终DBAdapter db=新的DBAdapter(本);
DBAdapter=db.open();
ListView-FriendLV=(ListView)findViewById(android.R.id.list);
最终游标friendslist=db.GetAllFriends();
String[]from=新字符串[]{“FRIENDS”};//您的列/此处的列
int[]to=newint[]{R.id.textview_friends};
@抑制警告(“弃用”)
ListAdapter cursorAdapter=新的SimpleCursorAdapter(此,
R.layout.list_项,friendslist,from,to,0);
FriendLV.setAdapter(游标适配器);
FriendLV.setLongClickable(真);
FriendLV.setOnItemLongClickListener(新的OnItemLongClickListener(){
类CustomCursorAdapter扩展了CursorAdapter{
公共CustomCursorAdapter(上下文上下文、游标c、int标志){
超级(上下文、好友列表、旗帜);
//TODO自动生成的构造函数存根
充气器=充气器。从(上下文);
}
充气机;
@凌驾
公共视图(视图、上下文上下文、,
光标好友列表){
//TODO自动生成的方法存根
int row_id=((com.fullfrontalgames.numberfighter.DBAdapter)friendslist)
.get(“_id”);
}
@凌驾
public View newView(上下文、游标友谊列表、,
视图组(父级){
//TODO自动生成的方法存根
LayoutFlater充气机=LayoutFlater.from(上下文);
视图v=充气机。充气(R.layout.list_项目,父项,
假);
bindView(v、上下文、好友列表);
返回v;
}
}
@凌驾
长单击(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
showDialog(arg2);
返回false;
}
});
}
公用作废按钮单击(查看){
星际触觉(新意图)(
“com.fullfrontalgames.numberfighter.Fightattacker”);
}
String[]items={“查看配置文件”、“发送消息”、“删除好友”};
@凌驾
公共对话框onCreateDialog(最终int id){
最终DBAdapter db=新的DBAdapter(本);
db.open();
开关(id){
案例0:
返回新的AlertDialog.Builder(此)
.setIcon(R.drawable.icon)
.setTitle(“选择一个选项”)
.setSingleChoiceItems(项目、id、,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,
整数选择){
//TODO自动生成的方法存根
}
}
)
.setPositiveButton(“确定”,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,
整数选择){
//TODO自动生成的方法存根
如果(选项==0){
Toast.makeText(getBaseContext(),
“测试”,吐司。长度(短)
.show();
}else if(选项==1){
Toast.makeText(getBaseContext(),
“测试2”,吐司。长度(短)
.show();
}else if(选项==2){
Toast.makeText(getBaseContext(),
“删除好友”,
吐司。长度(短)。show();
final TextView friends=(TextView)findViewById(R.id.TextView\u friends);
字符串deletedfriend=friends
.getText().toString();
db.DeleteFriends(deletedfriend);
}
}
})
.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,
整数选择){
}
})
.create();
}
返回null;
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
//清除数据库
DBAdapter.close();
}
}
选择单选按钮时,您必须保存选项,b中的另一个int参数
public class PlayAFriend extends ListActivity {
DBAdapter DBAdapter;
int mChoice;
......
......
@Override
public Dialog onCreateDialog(final int id) {
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select an Option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
mChoice = choice; // save the choice
}
}
)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
// TODO Auto-generated method stub
if (mChoice == 0) {
Toast.makeText(getBaseContext(),
"Test", Toast.LENGTH_SHORT)
.show();
} else if (mChoice == 1) {
Toast.makeText(getBaseContext(),
"Test2", Toast.LENGTH_SHORT)
.show();
} else if (mChoice == 2) {
Toast.makeText(getBaseContext(),
"Friend Removed",
Toast.LENGTH_SHORT).show();
final TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends
.getText().toString();
db.DeleteFriends(deletedfriend);
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int choice) {
}
})
.create();
}
return null;
}