本文介绍了如何弹出一个对话框,确认删除列表项,当用户长preSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从网上教程学习,我尝试由我自己来实现一些功能。
我怎么能弹出一个对话框,当检测到列表项长preSS提醒用户?以下是该教程一些code:
公共类扩展好友列表ListActivity
{
私有静态最终诠释ADD_NEW_FRIEND_ID = Menu.FIRST;私有静态最终诠释EXIT_APP_ID = Menu.FIRST + 1;
私人IAppManager imService = NULL;
私人FriendListAdapter friendAdapter;公共字符串ownusername =新的String();私有类FriendListAdapter延伸BaseAdapter
{
类ViewHolder {
TextView的文本;
ImageView的图标;
}
私人LayoutInflater mInflater;
私人位图mOnlineIcon;
私人位图mOfflineIcon; 私人FriendInfo []朋友= NULL;
公共FriendListAdapter(上下文的背景下){//类的构造函数FriendListAdapter
超(); mInflater = LayoutInflater.from(上下文); mOnlineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.greenstar);
mOfflineIcon = BitmapFactory.de codeResource(context.getResources(),R.drawable.redstar); } 公共无效setFriendList(FriendInfo []的朋友)
{
this.friends =的朋友;
}
公众诠释的getCount(){//获取多少行是在ListView 返回friends.length;
}
公共FriendInfo的getItem(INT位置){//从该行获得项目 回到朋友[位置]
} 众长getItemId(INT位置){ 返回0;
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){//对于修改行的内容
//一个ViewHolder不断提及孩子的意见,以避免不必要的来电
//在每个行findViewById()。
ViewHolder持有人; //当convertView不为空,我们可以直接重用,也没有必要
//为reinflate它。我们只吹一个新的View提供的convertView时
//通过ListView控件为空。
如果(convertView == NULL)
{
convertView = mInflater.inflate(R.layout.friend_list_screen,NULL); //创建两个孩子意见ViewHolder和存储引用
//我们需要将数据绑定到。
持有人=新ViewHolder();
holder.text =(TextView中)convertView.findViewById(R.id.text);
holder.icon =(ImageView的)convertView.findViewById(R.id.icon); convertView.setTag(保持器);
}
其他{
//获取ViewHolder回才能到TextView的快速访问
//和ImageView的。
支架=(ViewHolder)convertView.getTag();
} //与支架有效地结合的数据。
holder.text.setText(朋友[位置] .userName);
holder.icon.setImageBitmap(?朋友[位置] .STATUS == STATUS.ONLINE mOnlineIcon:mOfflineIcon); 返回convertView;
}}
解决方案
使用code为:
this.getListView()setLongClickable(真)。
this.getListView()。setOnItemLongClickListener(新OnItemLongClickListener(){
公共布尔onItemLongClick(适配器视图<>母公司,视图V,INT位置,长的id){
//做你的工作在这里
AlertDialog.Builder警报=新AlertDialog.Builder(
Activity.this);
alert.setTitle(警报!);
alert.setMessage(你确定要删除记录);
alert.setPositiveButton(是,新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){
//做你的工作在这里
dialog.dismiss(); }
});
alert.setNegativeButton(否,新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){ dialog.dismiss();
}
}); alert.show(); 返回true;
}
});
您可以根据您的需要自定义警告对话框...
I am learning from a online tutorial, and i try to implement some function by my own.How can i pop up a dialog to alert user when detect a long press on list item? Following is some code from that tutorial:
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;
}
}
解决方案
use the code as :
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;
}
});
You can customize alert dialog according to your need...
这篇关于如何弹出一个对话框,确认删除列表项,当用户长preSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!