Cannot get DialogFragment to dismiss programmatically - android

I have a DialogFragment that shows a list of items to pick from (similar to the attach dialog in Messaging).
My problem is that I cannot get this dialog to dismiss when an item is selected. I've tried calling dismiss() and getDialog().dismiss() inside the OnItemClickListener, no luck. I've tried to remove the dialog through the FragmentManager, I've tried fragmentManager.popBackStack(), all to no avail. I cannot get this dialog to dismiss. It goes away fine when clicking outside the dialog or hitting the back button, but nothing in my code will make it go away.
Has anyone seen this before? How do I get the dialog to dismiss correctly?
Dialog Code:
public class ShareDialog extends DialogFragment {
public enum ShareType {
Camera, Gallery, Web, Whiteboard, Browse,
}
BaseAdapter mShareAdapter = new BaseAdapter() {
#Override
public View getView(int position, View contentView, ViewGroup parent) {
TextView view = null;
if (contentView == null) {
view = (TextView) getLayoutInflater(null).inflate(android.R.layout.simple_list_item_1, parent, false);
} else {
view = (TextView) contentView;
}
int draw = 0;
switch (ShareType.values()[position]) {
case Browse:
view.setText("Browse Content...");
draw = R.drawable.ic_share_browse;
break;
case Camera:
view.setText("Image from Camera...");
draw = R.drawable.ic_share_camera;
break;
case Gallery:
view.setText("Image from Gallery...");
draw = R.drawable.ic_share_gallery;
break;
case Web:
view.setText("New Browsing Session");
draw = R.drawable.ic_share_web;
break;
case Whiteboard:
view.setText("New Whiteboard");
draw = R.drawable.ic_share_whiteboard;
break;
}
view.setCompoundDrawablesWithIntrinsicBounds(draw, 0, 0, 0);
view.setCompoundDrawablePadding(8);
return view;
}
#Override
public long getItemId(int position) {
return ShareType.values()[position].ordinal();
}
#Override
public Object getItem(int position) {
return ShareType.values()[position];
}
#Override
public int getCount() {
return ShareType.values().length;
}
};
public Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Share which?");
ListView list = new ListView(getActivity());
list.setAdapter(mShareAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long itemId) {
dismiss(); // WHY DOESN'T THIS WORK???
if (listener != null)
listener.newShare((ShareType) mShareAdapter.getItem(position));
}
});
builder.setView(list);
return builder.create();
}
public interface ShareDialogListener {
void newShare(ShareType type);
}
private ShareDialogListener listener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the AutoconnectListener so we can send events to the host
listener = (ShareDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement ShareDialogListener");
}
}
}

Why not use the methods available on AlertDialog.Builder to build the list, instead of creating your own ListView and populating it?
I have modified your sample code to show how this would work, and in this example the dialog dismiss() functions fine.
public Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setSingleChoiceItems(mShareAdapter, 0, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (listener != null) {
listener.newShare((ShareType) mShareAdapter.getItem(which));
}
}
});
builder.setTitle("Share which?");
return builder.create();
}

For some - unknown to me - reason, the dialog reference you get back from getDialog() isn't the one you want to work with when inside the listener. You need a reference to the dialog as provided you when you call builder.create();
For example:
final AlertDialog dialog = builder.create();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
}
});
return dialog;

HA....
I've found it...
The reason for this is actually ours... we were trying to inflate an xml, and have called:
DialogFragment.this.getLayoutInflater(null).inflate(...);
This call causes, like I've stated in the comment to create 4 dialogs, and then everything gets messed up.
The proper way to do this would be to call:
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(...);
This fix solved the annoying bug for me on the first go!

Related

Android - onClick of button not working in adapter inside a listview that is inside a fragment

I hope my title makes sense, basically I have a button inside a listview a layout as an adapter in a listview inside a fragment.
My problem is that my onClick method deleteQuote(View v) is not recognized when I declare it in the fragment class. It errors out something about not found in the main activity. So I transferred it to the main activity, but the problem is that I needed the view tag to get a primary key in the entry via the getTag to delete that entry in the database and in the adapter of the listview. Where should I put my onClick method where in i can still get the view tag or What is the best way to do this implementing setonclicklisteners?
Fragment class
public class QuotationList extends Fragment{
ListView lvQuotationsMotorcar;
ArrayList<Quotation> menuList_motorcar;
CustomMotorcarQuoteAdapter cmqa;
ListView lvQuotationsFire;
ArrayList<Quotation> menuList_fire;
CustomFireQuoteAdapter cfqa;
Button btnDeleteQuote_motorcar;
Spinner spQuotationType;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_online_quotation_list, container, false);
lvQuotationsMotorcar = (ListView) rootView.findViewById(R.id.lvQuotationsMotorcar);
menuList_motorcar = new ArrayList<Quotation>();
cmqa = new CustomMotorcarQuoteAdapter(getActivity(), R.layout.motorcarquotationlayout, menuList_motorcar);
lvQuotationsMotorcar.setAdapter(cmqa);
lvQuotationsFire = (ListView) rootView.findViewById(R.id.lvQuotationsFire);
menuList_fire = new ArrayList<Quotation>();
cfqa = new CustomFireQuoteAdapter(getActivity(), R.layout.firequotationlayout, menuList_fire);
lvQuotationsFire.setAdapter(cfqa);
final SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("INLIS", Context.MODE_PRIVATE, null);
spQuotationType = (Spinner) rootView.findViewById(R.id.spQuotationType);
btnDeleteQuote_motorcar = (Button) getActivity().findViewById(R.id.btnDeleteMotorcarQuote);
Cursor resultSet = mydatabase.rawQuery("Select * from QuotationMotorcar", null);
if (resultSet != null){
if (resultSet .moveToFirst()) {
while (!resultSet.isAfterLast()) {
//stuff
cmqa.add(q);
resultSet.moveToNext();
}
}
}else{
Toast.makeText(getActivity(), "No Pending Motorcar Quotations", Toast.LENGTH_LONG).show();
}
Cursor resultSet1 = mydatabase.rawQuery("Select * from QuotationFire", null);
if (resultSet1 != null){
if (resultSet1 .moveToFirst()) {
while (!resultSet1.isAfterLast()) {
//stuff
cfqa.add(q);
resultSet1.moveToNext();
}
}
}else{
Toast.makeText(getActivity(), "No Pending Fire Quotations", Toast.LENGTH_LONG).show();
}
mydatabase.close();
//more code
return rootView;
}
onClick method deleteQuote
public void deleteQuote_motorcar(final View v){
final SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("INLIS", Context.MODE_PRIVATE, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle("Warning!");
alertDialogBuilder
.setMessage("Are you sure you want to delete this quote?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Quotation itemToRemove = (Quotation)v.getTag();
cmqa.remove(itemToRemove);
mydatabase.execSQL("DELETE FROM QoutationMotorcar WHERE QuoteNumber =" + itemToRemove.getQuoteNumber() + "; ");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
This works perfectly fine if it just extends an activity class. Hope someone can help. I'm really annoyed on how this onClick is not working on Fragments.
How and where can I declare my method where in it can be called properly with the view tag?
Use this declaration of OnClickListener in your custom Adapter getView method
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_lv_item_layout, parent, false);
view.findViewById(R.id.your_bt).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
//do what ever you want
}
});
return view;
}
Instead of public void deleteQuote_motorcar(final View v)

Android onclick listener custom listview

With the following code I'm correctly receiving a dynamic list from mysql db and putting the elements in a listview.
public class MenuActivity extends ListActivity implements FetchDataListener {
private ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
initView();
}
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading..");
String url = "http://www.*********.php";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
#Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}
#Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
This is my array adapter:
public class ApplicationAdapter extends ArrayAdapter<Application>{
private List<Application> items;
public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_cat_list, items);
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_cat_list, null);
}
Application app = items.get(position);
if(app != null) {
TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
if(titleText != null) titleText.setText(app.getTitle());
}
return v;
}
Now I want to click on single row and open another activity passing some values via intent extra.
Where should I implement click listener?
I'm pretty sure it should be inserted in the "getView" but how I pass the app.getTitle() via intent? I know how pass intent extra in general, tried but no click happens.
Any help would be appreciated, thanks
Now I want to click on single row and open another activity passing
some values via intent extra. Where should I implement click listener?
No need to add OnItemClickListener because extending ListActivity in MenuActivity so just override onListItemClick method for handing ListView row click:
#Override
public void onListItemClick(ListView l, View view, int position, long id) {
// your code here...
}
how I pass the app.getTitle() via intent?
Get selected row TextView value in onListItemClick using view parameter:
TextView txtView=(TextView)v.findViewById(R.id.titleTxt);
String selectedText=txtView.getText().toString();
Use selectedText for sending value with Intent in Next Activity
Put this in your getView()
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent =new Intent(context, YourActivity.class);
context.startActivity(intent);
}
});
There can be multiple ways and following is one of them:
Set onItemClickListner on your listview in your activity and it will give you a callback i.e onListItemClick. But as you said you want the title their you have to set tag on the convertView in the getView method like covertView.setTag("itemTitle"); and in your onListItemClick get the tag from view and convert it to the title like this v.getTag().toString(); and set it any where you want.
follwoing is the full code:
#Override
public void onListItemClick(ListView l, View view, int position, long id) {
String title = view.getTag().toString();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", title);
startActivity(intent);
// your code here... }
Please post if got stuck anywhere.

AlertDialog - do not dismiss on item click

OK so I am creating an ArrayAdapter and using it in my Alert Dialog because I don't want to show the default radio buttons on SingleItemSelection dialog.
Instead I want to change the background of the item that is selected, and then when the user presses the positive button I will perform the action related to the item that has been selected.
private void showAlertDialog()
{
final String[] options = getResources().getStringArray(R.array.dialog_options);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("My Dialog");
dialogBuilder.setAdapter(adapter, new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show();
// Here I need to change the background color of the item selected and prevent the dialog from being dismissed
}
});
//String strOkay = getString(R.string.okay);
dialogBuilder.setPositiveButton("OK", null); // TODO
dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
There are two problems I'm trying to tackle.
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
To prevent dialog from dismissing on item click you can use AdapterView.OnItemClickListener instead of DialogInterface.OnClickListener.
Like this:
dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});
You can set custom ListView as content of AlertDialog and set OnItemClickListener
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String[] items = ...;
ListView list = new ListView(this);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
...
}
});
builder.setView(list);
and then save reference to dialog
mDialog = builder.show();
in order to dismiss it if necessary
mDialog.dismiss();
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
Here is example
public class MainActivity extends AppCompatActivity {
private static final String listFragmentTag = "listFragmentTag";
private static final String data[] = {"one", "two", "three", "four"};
public MainActivity() {
super();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View v) {
ListFragment lf = new ListFragment();
lf.show(getSupportFragmentManager(), listFragmentTag);
}
public static class ListFragment extends DialogFragment {
#Override #NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("List")
.setItems(data, null)
.setPositiveButton("OK", null); // pass your onClickListener instead of null
// to keep dialog open after click on item
AlertDialog ad = adb.create();
ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
private int colorOrg = 0x00000000;
private int colorSelected = 0xFF00FF00;
private View previousView;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// restoring color of previous view
if(previousView != null) {
previousView.setBackgroundColor(colorOrg);
}
// changing items's BG color
view.setBackgroundColor(colorSelected);
previousView = view;
}
});
return ad;
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
}
You can use setCanceledOnTouchOutside(false) or setCanceleable(false).
Set selector for the root element tag of the dialog layout xml.

Highlight selected Spinner Item

I have implemented Custom Spinner using Button following wildnove's answer. Everything works fine, but I am not able to display the highlighted radio button for the selected button.
Below is the code.
((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// How to highlight Radio button of a selected Item???
final String[] items = view.getResources().getStringArray(R.array.planets__entries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Button) findViewById(R.id.btnSpinnerPlanets)).setText(items[which]);
dialog.dismiss();
}
}).create().show();
}
});
Can somebody help me how to highlight selected Item's Radio button ...
Unfortunately this behavior is not natively implemented in Spinner component, however, you can always create your own BaseAdapter to show whatever you need weather is in the spinner it self or in the dropdown like this:
private class ExampleAdapter extends BaseAdapter{
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Here is where you actually get the chance to return whatever you want in the spinner component (the single bar with the arrow)
return yourCommonView;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
//Here is where you get the chance to return whatever you want in the dropdown menu so here you should validate what's the currently selected element and return an image accordingly...
return yourSelectedView;
}
}
The important method here is, getDropDownView that is the one that gives you the chance to return an element with a checked CheckBox, or any mark you want to use, of course you have to create your own layout and validate if the element currently created need to be marked or not...
Regards!
The problem with this code is that you are creating the Spinner each time the Button is clicked. Try the following code:
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id) {
case 1:
Button b=((Button) findViewById(R.id.btnSpinnerPlanets));
builder = new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(get_the_adapter(b), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
b.setText(b.getResources().getStringArray(R.array.planets__entries)[which]);
dismissDialog(1);
}
})
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
public ArrayAdapter<String> get_the_Adapter(Button view){
String[] items = view.getResources().getStringArray(R.array.planets__entries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
return adapter;
}
And for the Button's onClick():
((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog(1);
}
});

android delete any item from listview deleting last element

I made a list view u can delete an item by touch the row of that listview, but it always deleting the last item, and after some delete times it shows index out of bound error
system was when u toch an item a dialogue box open u click print then item will removed the item and open an activity for few seconds.
here is the list adapter class
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final int positionplayer = position;
ViewHolderaway1 holder;
if (convertView == null) {
View row = inflater.inflate(R.layout.readonlyvendorlist, parent,
false);
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// set title
try {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle(values.get(positionplayer).Voucherref
+ " Sell");
// set dialog message
alertDialogBuilder
.setMessage(
values.get(positionplayer)
.getVoucherref() + "For Print")
.setCancelable(false)
.setPositiveButton("Print Voucher",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
// if this button is clicked,
// close
PrintTestAcitvity.printettext = values
.get(positionplayer).PrinterText;
ListService.printerlist
.remove(positionplayer);
Datasync.storedataprint(context);
notifyDataSetChanged();
Intent ia = new Intent(context,
PrintTestAcitvity.class);
context.startActivity(ia);
}
})
.setNegativeButton("Cancell",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
// if this button is clicked,
// just close
// the dialog box and do nothing
Toast.makeText(
context,
"Printing Data store for locally",
Toast.LENGTH_LONG)
.show();
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
notifyDataSetChanged();
}
}
});
holder = new ViewHolderaway1();
holder.ProductItemID = (TextView) row
.findViewById(R.id.ProductItemID);
holder.VoucherCost = (TextView) row.findViewById(R.id.VoucherCost);
holder.SupplierID = (TextView) row.findViewById(R.id.SupplierID);
Log.d("goru", "gadha");
row.setTag(holder);
holder = (ViewHolderaway1) row.getTag();
// Printer Productsdata = values.get(positionplayer);
// if (Productsdata != null) {
// holder.ProductItemID.setText("Print");
// holder.VoucherCost.setText(Productsdata.getVoucherref());
// // holder.SupplierID.setText(resid)
// // holder.SupplierID.setVisibility(View.GONE);
//
// if (Productsdata.getVoucherref().contains("Voda")) {
// holder.VoucherCost.setBackgroundColor(Color.RED);
// holder.VoucherCost.setTextColor(Color.WHITE);
// holder.SupplierID.setBackgroundDrawable(getContext()
// .getResources().getDrawable(R.drawable.voda));
// }
// if (Productsdata.getVoucherref().contains("Eco")) {
// holder.VoucherCost.setBackgroundColor(Color.BLUE);
// holder.VoucherCost.setTextColor(Color.WHITE);
// holder.SupplierID.setBackgroundDrawable(getContext()
// .getResources().getDrawable(R.drawable.eco));
// }
//
// }
convertView = row;
}else
{
holder = (ViewHolderaway1) convertView.getTag();
}
Printer Productsdata = values.get(positionplayer);
if (Productsdata != null) {
holder.ProductItemID.setText("Print");
holder.VoucherCost.setText(Productsdata.getVoucherref());
// holder.SupplierID.setText(resid)
// holder.SupplierID.setVisibility(View.GONE);
if (Productsdata.getVoucherref().contains("Voda")) {
holder.VoucherCost.setBackgroundColor(Color.RED);
holder.VoucherCost.setTextColor(Color.WHITE);
holder.SupplierID.setBackgroundDrawable(getContext()
.getResources().getDrawable(R.drawable.voda));
}
if (Productsdata.getVoucherref().contains("Eco")) {
holder.VoucherCost.setBackgroundColor(Color.BLUE);
holder.VoucherCost.setTextColor(Color.WHITE);
holder.SupplierID.setBackgroundDrawable(getContext()
.getResources().getDrawable(R.drawable.eco));
}
}
return convertView;
}
There are some major flaws with your logic in getView(), this is the biggest one:
if (convertView != null)
{
holder = new ViewHolderaway1();
return convertView;
}
This returns the recycled row without altering it... At a minimum you need to change the written content of the layout.
Look at the code in this answer: https://stackoverflow.com/a/4145996/1267661. You should use this as template. It demonstrates how to properly use a ViewHolder and display accurate data after Views are returned from ListView's RecycleBin.
Addition
This new code is much more efficient! Also I think I see why only the last row is deleted. In your AlertDialog you reference positionplayer but this onClick() code is run after getView() has finished, so positionplayer is not helpful here. Since your OnClickListener refers to the whole row, you should use the OnItemClickListener in your Activity instead and this will help delete the appropriate row:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Move your AlertDialog code here and use position instead of positionplayer
}
});

Categories

Resources