2013-04-14 22 views
0

我正在尝试使用OK和Cancel按钮创建SingleChoiceItems AlertDialog,该按钮允许用户从ListView中将onItemLongClick选项设置为1.查看用户的配置文件2.向用户发送消息和3从朋友列表中删除用户。AlertDialog SingleChoiceItems不工作

我还没有完成选项1和2,但我正在尝试做出选项3,其中我包括数据库删除方法和说“朋友删除”的祝酒,但是当我选择选项3并点击确定该行不会被删除,或者我看到吐司说用户被删除。这里是我使用对话框的活动的代码。

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(); 

    } 

} 
+0

请格式化你的代码,无法辨认。 –

+0

它被格式化我相信 – Cranosaur

+0

它不是。你知道我是怎么知道的吗?我有眼睛。 –

回答

1

您必须在选择单选按钮时保存选项,而按钮中的另一个int参数并不代表选择。 让int mChoice;类成员

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; 
} 
+0

我得到错误它什么我改变到最后,但当我这样做时它说:“最后的局部变量mChoice不能被分配,因为它是在封闭类型中定义的”。 – Cranosaur

+0

更改int mChoice;到最后int mChoice; –

+0

我做了,但现在它说“最后的局部变量mChoice不能被分配,因为它是在封闭类型中定义的”,并且选项0-2需要初始化变量 – Cranosaur