java 如何让 BaseAdapter 显示 AlertDialog android 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10333730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make BaseAdapter show AlertDialog android Application
提问by Monerah
How can I make the button in BaseAdapter in listView shows alertDialog, I tried that but it Stopped work unexpected (RunTime Error) my code is shown below .
如何使listView 中BaseAdapter 中的按钮显示alertDialog,我试过了,但它意外停止工作(运行时错误),我的代码如下所示。
any suggestion
任何建议
thanks in advance
提前致谢
Monerah
莫内拉
====after Update =====================
====更新后====================
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyCasesListAdapter extends BaseAdapter {
private Context context;
private List<MyCaseClass> listOfCases;
// TODO delete it not imp.
public MyCasesListAdapter() {
super();
}
public MyCasesListAdapter(Context context, List<MyCaseClass> listPhonebook) {
this.context = context;
this.listOfCases = listPhonebook;
}
public int getCount() {
return listOfCases.size();
}
public Object getItem(int position) {
return listOfCases.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
MyCaseClass entry = listOfCases.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.mypage_row, null);
}
// this is row items..
// Set the onClick Listener on this button
Button ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);
//To be a clickable button
ConfExpandRegion.setFocusableInTouchMode(false);
ConfExpandRegion.setFocusable(false);
//For Dialog
AlertDialog alertDialog = new AlertDialog.Builder(MyCasesListAdapter.this);
alertDialog.setTitle("Conformation");
alertDialog.setMessage("Are you sure you want to do ???");
ConfExpandRegion.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Some code
//ConfExpandRegion.setEnabled(false);
}
});
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
// Do nothing
}
});
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.show();
}});
//To be a clickable button
Cancelb.setFocusableInTouchMode(false);
Cancelb.setFocusable(false);
Cancelb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
MyCaseClass entry = (MyCaseClass) v.getTag();
listOfCases.remove(entry);
// listPhonebook.remove(view.getId());
notifyDataSetChanged();
}
});
// Set the entry, so that you can capture which item was clicked and
// then remove it
// As an alternative, you can use the id/position of the item to capture
// the item
// that was clicked.
ConfExpandRegion.setTag(entry);
Cancelb.setTag(entry);
// btnRemove.setId(position);
return convertView;
}
public void onClick(View view) {
MyCaseClass entry = (MyCaseClass) view.getTag();
listOfCases.remove(entry);
// listPhonebook.remove(view.getId());
notifyDataSetChanged();
}
private void showDialog(MyCaseClass entry) {
// Create and show your dialog
// Depending on the Dialogs button clicks delete it or do nothing
}
public void add(MyCaseClass myCaseClass) {
// TODO Auto-generated method stub
listOfCases.add(myCaseClass);
}
}
//============================================================================
//================================================ ============================
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MyPage extends Activity {
Button createForm;
Button ConfExpandRegion, Cancelb;
String ExpandMsg, CancelMsg;
boolean b;
MyCaseClass mycase;
TextView tvCase;
AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mypage);
// Moving to anther activity
createForm = (Button) findViewById(R.id.creat_new_formbtn);
createForm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(MyPage.this, CreateNewForm.class);
startActivity(j);
}
});
// ============================================================================================
// for list
ListView list = (ListView) findViewById(R.id.mypage_list);
list.setClickable(true);
final List<MyCaseClass> listOfPhonebook = new ArrayList<MyCaseClass>();
MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfPhonebook);
for (MyCaseClass m : All_Static.getMyCaseList())
adapter.add(new MyCaseClass(m));
// after fill the adapter.. assign the list to the adapter
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
System.out.println("sadsfsf");
;
}
});
list.setAdapter(adapter);
// ========================================================================================
}
public void sendSMS(String number, String msg) throws Exception {
if (!b) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
}
b = true;
}
// ========================================================================
}
回答by Gophermofur
Are you 100% sure when you pass "this" as the parameter when creating your Adapter that you are passing the Activity?
您是否 100% 确定在创建适配器时将“this”作为参数传递给您正在传递活动?
One good way to check is to modify the app like this:
检查的一种好方法是像这样修改应用程序:
Add an Activity parameter to your adapter
将 Activity 参数添加到您的适配器
private Context context;
private Activity parentActivity;
...
public MyCasesListAdapter(Context context, List<MyCaseClass> listPhonebook, Activity parentActivity) {
this.context = context;
this.listOfCases = listPhonebook;
this.parentActivity = parentActivity;
}
Create your alert dialog like this...
像这样创建警报对话框...
AlertDialog alertDialog = new AlertDialog.Builder(parentActivity);
Lastly, call the contructor of your adapter like this...
最后,像这样调用适配器的构造函数...
MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfPhonebook, MyPage.this);
Explanation: You probably don't need to pass in Activity and Context to your base adapter, but I did this just so you can keep everything else as is for the time being. I'm not sure if "this", when you are instantiating your adapter, is actually an activity. I defined the 3rd parameter in the constructor as "Activity" to force you to pass in an Activity. You'll get compile errors if you try and pass in something that isn't and activity, so it should help you out.
说明:您可能不需要将 Activity 和 Context 传递给您的基本适配器,但我这样做只是为了您可以暂时保留其他所有内容。当您实例化适配器时,我不确定“这个”是否实际上是一项活动。我在构造函数中将第三个参数定义为“Activity”,以强制您传入一个 Activity。如果您尝试传入一些不属于活动的内容,则会出现编译错误,因此它应该可以帮助您解决问题。
Also, I just noticed, but the problem is probably that your updated code is still trying to create the AlertDialog using MyCasesListAdapter.this as the context, which is not an activity.
另外,我刚刚注意到,但问题可能是您更新的代码仍在尝试使用 MyCasesListAdapter.this 作为上下文创建 AlertDialog,这不是活动。
回答by Praveen
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(view.getRootView().getContext());
alertDialogBuilder.setMessage("Are you sure you want to delete?");
alertDialogBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
deleteExp(tid);
}
});
alertDialogBuilder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
回答by ngesh
First thing your alertDialogis not even initialised.. so NPE .. and when you create it inside an Adapter make sure to use activity context and not ApplciatioContext
第一件事你的alertDialog甚至没有初始化..所以 NPE .. 当你在一个适配器中创建它时确保使用activity context and not ApplciatioContext
AlertDialog alertDialog = new AlertDialog.Builder(YourACtivity.this);
the above line should not be in the class level.. it should be inside getView()
method... and use your activity instance as Context.. something like nameOfYourActivity.this
上面的行不应该在类级别..它应该在getView()
方法内部......并使用您的活动实例作为上下文..类似nameOfYourActivity.this
回答by Rajesh
You have to use an AlertDialog.Builder
to build an AlertDialog
.
您必须使用 anAlertDialog.Builder
来构建AlertDialog
.