私はSingleChoiceItems AlertDialogをOKとCancelボタンで作成しようとしています。これにより、ListViewからonItemLongClickで1.ユーザーのプロファイルを表示し、2.ユーザーにメッセージを送信し、3.ユーザーを友達から削除しますリスト。
オプション 1 と 2 はまだ実行していませんが、オプション 3 を作成しようとしています。これには、データベースの削除メソッドと「友人が削除されました」というトーストが含まれていますが、オプション 3 を選択して [OK] を押すと、その行は実行されます削除されないか、トーストにユーザーが削除されたと表示されます。ダイアログを使用しているアクティビティのコードを次に示します。
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();
}
}