2014-04-21 11 views
5

Tôi đang học từ một hướng dẫn trực tuyến, và tôi cố gắng thực hiện một số chức năng của riêng tôi. Làm thế nào tôi có thể bật lên một hộp thoại để cảnh báo người dùng khi phát hiện một báo dài trên mục danh sách? Sau đây là một số mã từ hướng dẫn rằng:Làm thế nào để bật lên một hộp thoại để xác nhận xóa khi người dùng nhấn và giữ trên mục danh sách?

public class FriendList extends ListActivity 
{ 


private static final int ADD_NEW_FRIEND_ID = Menu.FIRST; 

private static final int EXIT_APP_ID = Menu.FIRST + 1; 
private IAppManager imService = null; 
private FriendListAdapter friendAdapter; 

public String ownusername = new String(); 

private class FriendListAdapter extends BaseAdapter 
{  
    class ViewHolder { 
     TextView text; 
     ImageView icon; 
    } 
    private LayoutInflater mInflater; 
    private Bitmap mOnlineIcon; 
    private Bitmap mOfflineIcon;   

    private FriendInfo[] friends = null; 


    public FriendListAdapter(Context context) { // Constructor of Class "FriendListAdapter" 
     super();    

     mInflater = LayoutInflater.from(context); 

     mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar); 
     mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar); 

    } 

    public void setFriendList(FriendInfo[] friends) 
    { 
     this.friends = friends; 
    } 


    public int getCount() { // get how many row are in the listview 

     return friends.length; 
    } 


    public FriendInfo getItem(int position) { // get item from the row 

     return friends[position]; 
    } 

    public long getItemId(int position) { 

     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { // For modify the content of row 
     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is no need 
     // to reinflate it. We only inflate a new View when the convertView supplied 
     // by ListView is null. 
     if (convertView == null) 
     { 
      convertView = mInflater.inflate(R.layout.friend_list_screen, null); 

      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.text); 
      holder.icon = (ImageView) convertView.findViewById(R.id.icon);          

      convertView.setTag(holder); 
     } 
     else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     holder.text.setText(friends[position].userName); 
     holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon); 

     return convertView; 
    } 

} 
+0

bạn nên sử dụng menu ngữ cảnh cho ... đăng ký mục danh sách của bạn cho menu ngữ cảnh – amj

+0

Tôi nghĩ rằng bạn đang cố gắng làm một điều phức tạp hơn nhiều so với bạn có thể. Bạn nên bắt đầu với phần đào tạo một trang web dành cho nhà phát triển Android. – Valdrinit

Trả lời

18

sử dụng các mã như:

this.getListView().setLongClickable(true); 
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) { 
    //Do your tasks here 


      AlertDialog.Builder alert = new AlertDialog.Builder(
        Activity.this); 
      alert.setTitle("Alert!!"); 
      alert.setMessage("Are you sure to delete record"); 
      alert.setPositiveButton("YES", new OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
               //do your work here      
        dialog.dismiss(); 

       } 
      }); 
      alert.setNegativeButton("NO", new OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        dialog.dismiss(); 
       } 
      }); 

      alert.show(); 

    return true; 
    } 
    }); 

Bạn có thể tùy chỉnh hộp thoại cảnh báo theo nhu cầu của bạn ...

+0

Cảm ơn bạn! Đó chính xác là những gì tôi cần :) – Chung

+0

luôn chào đón @Chung –

0

Hy vọng nó giúp đỡ:

selectRoom.setOnItemLongClickListener(new OnItemLongClickListener(){ 
    @Override 
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
      int position, long arg3) { 
     // TODO Auto-generated method stub 
     Dialog(); 
} 
} 

hoặc

final MyDialog rDialog=new MyDialog(this,this); 
     return false; 
    } 

}); 

public void Dialog() { 

    AlertDialog.Builder dialogAlert = new AlertDialog.Builder(getApplicationContext()); 
    dialogAlert.setTitle("Demo ?"); 

    dialogAlert.show(); 
} 

//Another way 

public class MyDialog extends Dialog { 

Activity act; 
public ReminderSettingDialog(Context context,Activity act) { 
super(context); 
this.act=act; 
// TODO Auto-generated constructor stub 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(R.layout.settings); 

} 
} 
+0

cảm ơn bạn đã đề xuất của bạn, tôi đã giải quyết được vấn đề của mình :) – Chung

0

Bạn nên làm trong hoạt động của mình với ListView.

ListView myListView; 
myListView = (ListView) findViewById(R.id.my_list_view); 

myListView.setOnLongClickListener(new OnLongClickListener() { 

    @Override 
    public boolean onLongClick(View v) { 
     //show your dialog 
     //like this 
     CustomDialog dialog = new CustomDialog(); 
     dialog.show(getSupportFragmentManager(), TAG) 
     return false; 
    } 
}); 
Các vấn đề liên quan