I am implementing a custom ListAdapter which is using different list item layouts to show some items. From that custom ListAdapter, I actually want to show an AlertDialog when a particular Button is clicked. I implemented the onCreateDialog(int) method and I am trying to use showDialog(int) to show the dialog. But the dialog does not show up in the Activity.
Here is my custom listadapter file
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
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 AddProblemsLayoutAdapter extends BaseAdapter {
private Activity mContext;
private static final int TYPE_TITLE = 0;
private static final int TYPE_TAG = 1;
private static final int TYPE_SOLUTION = 2;
private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
private static final int ADD_TAG_DIALOG = 3378;
private static int ITEM_COUNT = 4;
private static Button addSolution = null, addTag = null;
private Activity mContext;
private static final int TYPE_TITLE = 0;
private static final int TYPE_TAG = 1;
private static final int TYPE_SOLUTION = 2;
private static final int LAYOUT_MAX_COUNT = TYPE_SOLUTION + 1;
private static final int ADD_TAG_DIALOG = 3378;
private static int ITEM_COUNT = 4;
private static Button addSolution = null, addTag = null;
public AddProblemsLayoutAdapter(Activity aContext) {
mContext = aContext;
}
@Override
public int getViewTypeCount() {
return LAYOUT_MAX_COUNT;
}
protected Dialog onCreateDialog(int id) {
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
switch (id) {
case ADD_TAG_DIALOG:
builder.setMessage("Are you sure you want to exit?").setCancelable(
false).setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
dialog.setOwnerActivity(mContext);
return dialog;
}
@Override
public int getItemViewType(int position) {
if (position < 2)
return TYPE_TITLE;
else
return position > 2 ? TYPE_SOLUTION : TYPE_TAG;
}
@Override
public View get开发者_如何学CView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
LayoutInflater inflater = mContext.getLayoutInflater();
switch (type) {
case TYPE_TITLE:
convertView = inflater.inflate(R.layout.title_row, null);
break;
case TYPE_TAG:
convertView = inflater.inflate(R.layout.tag_row, null);
break;
case TYPE_SOLUTION:
convertView = inflater.inflate(R.layout.solution_row, null);
break;
}
}
if (position == 0) {
TextView titleText = (TextView) convertView
.findViewById(R.id.titleText);
titleText.setText(R.string.title_string);
} else if (position == 1) {
TextView titleText = (TextView) convertView
.findViewById(R.id.titleText);
titleText.setText(R.string.description_string);
} else if (position == 2) {
addTag = (Button) convertView.findViewById(R.id.addProblemTag);
addTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.showDialog(ADD_TAG_DIALOG);
Toast.makeText(mContext, "Add Tags", Toast.LENGTH_LONG)
.show();
}
});
} else if (position == 3) {
addSolution = (Button) convertView.findViewById(R.id.addSolution);
addSolution.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ITEM_COUNT++;
notifyDataSetChanged();
}
});
}
return convertView;
}
@Override
public int getCount() {
return ITEM_COUNT;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
Could anyone give me some pointers as to how to show the AlertDialog box on the button click.
If you're using the context to call showDialog
then you probably want to define the onCreateDialog
dialog in your Activity
and not your adapter.
It does not work because onCreateDialog
belongs to the Activity
class. You shouldn't do anything in the adapter but manage data. That said, put the onCreateDialog
inside your activity and call it from there (using the showDialog
method, of course :).
You added a click listener to one of the buttons; so what you want to do there is sending a callback to the activities for it to invoke the showDialog
method.
You can use the MaterialDesignLibrary
. Which also will work from the BaseAdapters and ListAdapters. please check it here https://github.com/navasmdc/MaterialDesignLibrary
In Class CustomAdapter you declare a variable mContext and a ArrayList data to ListView
ArrayList<String> datasource;
Context mContext;
Create a constructor:
public AdapterAudio(Context mContext, ArrayList<String> data) {
super();
this.datasoure = data;
this.mContext = mContext;
}
When you call CustomAdapter from Activity, "Activity_Main.this" is Context you need
CustomAdapter adapter = new CustomAdapter(Activity_Main.this, listAudio_hienthi10);
Now you have a Context, use variable mContext is declared to replace
"getContext()", "v.getContext()"
Now you can Toast or show any dialog when click Button in CustomAdapter you want. Enjoy your code!
精彩评论