Reputation: 1183
I'm trying to make a SingleChoiceItems AlertDialog with an OK and Cancel button which gives the user the option on a onItemLongClick from a ListView to 1.view the user's profile 2.send the user a message and 3.delete the user from the friends list.
I haven't done option 1 and 2 yet but I'm trying to make option 3 right now in which I included the Database delete method and a toast that says "friend removed" but when I select Option 3 and hit ok that row does not get deleted or I see the Toast saying the user was deleted.Here is my code for the Activity I'm using the Dialog.
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();
}
}
Upvotes: 0
Views: 315
Reputation: 18151
You have to save the choice when select a radio button, the other int parameter in the button does not represent the choice.
Make int mChoice;
a class member
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;
}
Upvotes: 1