问题描述:

I am using a custom adapter for adding datas to my listview which extends ResourceCursorAdapter. and i need to handle the button click events in the listview. Every list conatins 2 buttons. How can i show alertdailog when i click those buttons...

My ListViewAdapter.java code...

class ListViewAdapter extends ResourceCursorAdapter {

ListViewAdapter(Context context, int layout, Cursor c) {

super(context, layout, c);

}

@Override

public void bindView(View view, Context context, Cursor cursor) {

TextView fname = (TextView) view.findViewById(R.id.fname);

TextView lname = (TextView) view.findViewById(R.id.lname);

TextView gender=(TextView)view.findViewById(R.id.gender);

Button edit=(Button)view.findViewById(R.id.button3);

Button start=(Button)view.findViewById(R.id.button4);

ImageView img=(ImageView)view.findViewById(R.id.profile_image);

fname.setText(cursor.getString(2));

lname.setText(cursor.getString(3));

gender.setText(cursor.getString(5));

Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(10));

Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(cursor.getString(10)),320,240);

img.setImageBitmap(thumbBitmap);

edit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setMessage("Hello")

.setPositiveButton("Start", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

Intent intent = new Intent(getApplicationContext(), Consultation.class);

startActivity(intent);

}

})

.setNegativeButton("Edit", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

Intent intent = new Intent(getApplicationContext(), AddPatient.class);

startActivity(intent);

}

})

.setNeutralButton("Cancel",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

}

});

AlertDialog d = builder.create();

d.setTitle("Patient Details");

d.show();

d.setCancelable(false);

}

});

}

The code shows error on alertdialog...

How can i resolve this issue.?

网友答案:

Simply pass the context to Alert Dialog like this:

class ListViewAdapter extends ResourceCursorAdapter {
Context context;

ListViewAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
this.context=context;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView fname = (TextView) view.findViewById(R.id.fname);
TextView lname = (TextView) view.findViewById(R.id.lname);
TextView gender=(TextView)view.findViewById(R.id.gender);
Button edit=(Button)view.findViewById(R.id.button3);
Button start=(Button)view.findViewById(R.id.button4);

ImageView img=(ImageView)view.findViewById(R.id.profile_image);
fname.setText(cursor.getString(2));
lname.setText(cursor.getString(3));
gender.setText(cursor.getString(5));
Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(10));

Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(cursor.getString(10)),320,240);

img.setImageBitmap(thumbBitmap);

edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new            AlertDialog.Builder(context);
builder.setMessage("Hello")
        .setPositiveButton("Start", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                Intent intent = new Intent(context.getApplicationContext(),  Consultation.class);

                startActivity(intent);

            }
        })
        .setNegativeButton("Edit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {


                Intent intent = new Intent(context.getApplicationContext(), AddPatient.class);

                startActivity(intent);
            }
        })
        .setNeutralButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
AlertDialog d = builder.create();
d.setTitle("Patient Details");
d.show();
d.setCancelable(false);
}
} );
}
网友答案:

Please Try Below code

class ListViewAdapter extends ResourceCursorAdapter {
Context mContext;
ListViewAdapter(Context context, int layout, Cursor c) {
 super(context, layout, c);
this.mContext=context;
 }
}

edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Hello")
        .setPositiveButton("Start", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

                Intent intent = new Intent(mContext, Consultation.class);

                startActivity(intent);

            }
        })
        .setNegativeButton("Edit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {


                Intent intent = new Intent(mContext, AddPatient.class);

                startActivity(intent);
            }
        })
        .setNeutralButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
AlertDialog d = builder.create();
d.setTitle("Patient Details");
d.show();
d.setCancelable(false);
 }
});

Instead your MainActivity.This and getApplicationContext() use mContext

相关阅读:
Top