How to delete a selected item from the listview? - java

SORRY FOR THIS CONFUSION:
UPDATED QUESTION:
I'm trying to remove a list item from the listview. when the item is clicked, the alertdialog is shown. If i click OK, then the selected item must be removed from the listview.
My Code goes below:
case R.id.lvinc:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Event ");
builder.setMessage("Delete this Event ?");
builder.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try
{
???? //What code to delete the selected list item?
}catch(Exception e)
{
e.printStackTrace();
}
}
});
AlertDialog alert = builder.create();
alert.show();
displaylist();
break;
Any help is really appreciated and thanks in advance...

I tried to solve it and got one solution pls go through the code below:
listview.java
public class listview extends Activity implements OnItemClickListener{
ListView list;
ListAdapter adapter;
ArrayList<String> nameArray;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//xml should have an ListView element
nameArray = new ArrayList<String>();
nameArray.add("Item1");
nameArray.add("Item2");
nameArray.add("Item3");
nameArray.add("Item4");
nameArray.add("Item5");
list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(listview.this);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
showDialog(arg2);
}
#Override
protected Dialog onCreateDialog(final int id) {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete Event")
.setCancelable(true)
.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
nameArray.remove(id);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
return super.onCreateDialog(id);
}
}
//Adapter Class
ListAdapter.java
public class ListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> name;
private static LayoutInflater inflater=null;
public ListAdapter(Activity a, ArrayList<String> nameArray) {
activity = a;
name = nameArray;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return name.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.title);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name.get(position));
return vi;
}
}

I don't know what kind of data you are using.
I imagine Cursor, database or List, feel free to tell us, so it will be easier to help.
This example is for a list:
protected void onListItemClick(View v, int pos, long id) {
Log.i(TAG, "onListItemClick id=" + id);
//Display your Dialog
(...)
builder.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
myList.remove(pos);
myAdapter.notifyDataChanged();
}
});
}

You propably have some kind of Adapter, that you have feed with some kind of data.
Remove the item from that list, and notifyDataChanged to the ListView.
And finally: dialog.dismiss();

itemLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
indexClick = position;
}
}
if ((indexClick) == (position)) {
itemLayout.setBackgroundResource(R.drawable.tab_select);
}
otherwise put tab_unselected image.

Related

How can I fix Recyclerview item deletion display error

I have a recyclerview. There are city names in the recyclerview and when I long click, I want to delete it from the recyclerview. I wrote some code in adapter class. When I click on the city names, I can delete them, but when I view the recyclerview again, the city names I deleted appear again. How can I fix this ?
My adapter class
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
ArrayList<City> arrayList;
Context context;
public Adapter(ArrayList<City> arrayList ,Context context ){
this.arrayList = arrayList;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
RecyclerviewRowBinding recyclerviewRowBinding = RecyclerviewRowBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
return new MyViewHolder(recyclerviewRowBinding);
}
#Override
public void onBindViewHolder(#NonNull Adapter.MyViewHolder holder, int position) {
holder.binding.MytxtCities.setText(arrayList.get(position).cityName);
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.warningicon);
builder.setMessage("Are you sure that you want to delete "+arrayList.get(position).cityName);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
arrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,arrayList.size());
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
return true;
}
});
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(holder.itemView.getContext(),MainActivity.class);
intent.putExtra("citId",arrayList.get(position).id);
holder.itemView.getContext().startActivity(intent);
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Mytxt_cities;
private RecyclerviewRowBinding binding;
public MyViewHolder(#NonNull RecyclerviewRowBinding binding) {
super(binding.getRoot());
this.binding = binding;
Mytxt_cities = itemView.findViewById(R.id.Mytxt_cities);
}
}
}
My recyclerview class is cities class
public class cities extends AppCompatActivity {
RecyclerView recyclerView ;
ArrayList<City> cityArrayList;
Adapter cityadapter;
ImageView cities_back_icon;
public void init(){
cities_back_icon = findViewById(R.id.Id_cities_back_icon);
cities_back_icon_click_register();
cityArrayList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerview_id);
SQLGet_Data();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cities);
init();
}
private void cities_back_icon_click_register(){
cities_back_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(cities.this, MainActivity.class);
startActivity(intent);
}
});
}
private void SQLGet_Data(){
try {
SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("City",MODE_PRIVATE,null);
Cursor cursor = sqLiteDatabase.rawQuery("SELECT*FROM city",null);
int idIx = cursor.getColumnIndex("id");
int nameIx = cursor.getColumnIndex("cityname");
while(cursor.moveToNext()){
String cityname = cursor.getString(nameIx);
int id = cursor.getInt(idIx);
City city = new City(cityname,id);
cityArrayList.add(city);
}
cityadapter.notifyDataSetChanged();
cursor.close();
}
catch (Exception e ){
e.printStackTrace();
}
/*---------------------- set recyclerview-----------------------------*/
recyclerView.setLayoutManager(new LinearLayoutManager(cities.this));
cityadapter = new Adapter(cityArrayList,this);
recyclerView.setAdapter(cityadapter);
/*--------------------------We drew a line between the data in the recyclerview------------------------------------*/
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.custom_divider);
dividerItemDecoration.setDrawable(drawable);
recyclerView.addItemDecoration(dividerItemDecoration);
}
}
The problem is that you are only deleting the cities from your ArrayList that is inside your adapter. But those cities still remain in your SQL database. Then when you restart the Activity your ArrayList will be created with the data of your SQL database, where those deleted cities still exist.
To delete cities consistently you need to delete the cities not only in your adapter but also in your SQL database.

how to get values from sqlite in view holder

product Adapter
public class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder>{
private Context context;
private List<Product> listProducts;
private SqliteDatabase mDatabase;
public ProductAdapter(Context context, List<Product> listProducts) {
this.context = context;
this.listProducts = listProducts;
mDatabase = new SqliteDatabase(context);
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list_layout, parent, false);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
final Product singleProduct = listProducts.get(position);
holder.name.setText(singleProduct.getName());
// holder.quantity.setText(singleProduct.getQuantity());
//when i call quantity(the ABOVE value) the application crashes and when i call only name the field the values gets displayed
holder.editProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editTaskDialog(singleProduct);
}
});
holder.deleteProduct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDatabase.deleteProduct(singleProduct.getId());
((Activity)context).finish();
context.startActivity(((Activity) context).getIntent());
}
});
}
#Override
public int getItemCount() {
return listProducts.size();
}
private void editTaskDialog(final Product product){
LayoutInflater inflater = LayoutInflater.from(context);
View subView = inflater.inflate(R.layout.add_product_layout, null);
final EditText nameField = (EditText)subView.findViewById(R.id.enter_name);
final EditText quantityField = (EditText)subView.findViewById(R.id.enter_quantity);
if(product != null){
nameField.setText(product.getName());
quantityField.setText(String.valueOf(product.getQuantity()));
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Edit product");
builder.setView(subView);
builder.create();
builder.setPositiveButton("EDIT PRODUCT", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String name = nameField.getText().toString();
final String quantityStr = quantityField.getText().toString();
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
{
Toast.makeText(context, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
}
else
{
final int quantity = Integer.parseInt(quantityStr);
if(quantity<=0)
{
Toast.makeText(context,"input is less than or equal to zero",Toast.LENGTH_LONG).show();
}
else {
mDatabase.updateProduct(new Product(product.getId(), name, quantity));
//refresh the activity
((Activity)context).finish();
context.startActivity(((Activity)context).getIntent());
}
}
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "Task cancelled", Toast.LENGTH_LONG).show();
}
});
builder.show();
}
}
product viewHolder
public class ProductViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView quantity;
public ImageView deleteProduct;
public ImageView editProduct;
public ProductViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.product_name);
deleteProduct = (ImageView)itemView.findViewById(R.id.delete_product);
editProduct = (ImageView)itemView.findViewById(R.id.edit_product);
quantity=(TextView)itemView.findViewById(R.id.product_name2);
}
}
Now the issue here is when is use holder.quantity.setText(singleProduct.getQuantity()) the application crashes.
I also wanted to add date picker dialog and want to display it in the recycler view so for date the code is same like holder.object.setText or something else and can we use a form for alert dialog like
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
dialog.setContentView(R.layout.dialog_dark);
dialog.setCancelable(true);
I have created the database file but the issue is mainly related to recycler view contents and if i wanted multiple holder is it possible to get the contents?
getQuantity() returns a int. So, Android is trying to search for a resource with id == getQuantity() leading to the crash (since you probably any resource with that id.
In other words, you are invoking TextView.setText(int resourceId) and not TextView.setText(String text)).
To fix, change this:
holder.quantity.setText(singleProduct.getQuantity());
To this:
holder.quantity.setText(String.valueOf(singleProduct.getQuantity()));

Alert Dialog.Builder(??)

I am trying to make an alertdialog for when the user presses the cardview to delete it. This is in my ProductAdapter code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(????);
what would I put in the brackets where I have the ????
I have looked at examples on this site and the ones I have seen have used ActivityName.this inside the brackets. However this code is in the ProductAdapter code and I can't use ProductAdapter.this. (my main activity is called create.java fyi)
So what would go in there? Thanks
update
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();
//this context we will use to inflate the layout
private Context mCtx;
private SearchableSpinner spinner;
//we are storing all the products in a list
private List<Product> productList;
private Activity create;
public ProductAdapter(Activity activity){
create = activity;
}
//getting the context and product list with constructor
public ProductAdapter(Activity activity, List<Product> productList) {
this.mCtx = mCtx;
create = activity;
this.productList = productList;
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, final int position) {
// //getting the product of the specified position
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(mCtx, R.layout.item_spinner_layout,
Product.getSpinnerItemsList());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner.setAdapter(spinnerArrayAdapter);
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
mSpinnerSelectedItem.put(position, mPosition);
TextView mTextView = view.findViewById(R.id.mSpinnerText);
Toast.makeText(mCtx, "Selected Item: " + mTextView.getText().toString(), Toast.LENGTH_LONG).show();
Log.e("***************", "Selected Item: " + mTextView.getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//binding the data with the viewholder views
if (mSpinnerSelectedItem.containsKey(position)) {
holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
}
holder.getView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);
// set title
alertDialogBuilder.setTitle("Delete Item");
// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
productList.remove(position);
notifyItemRemoved(position);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
#Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
SearchableSpinner spinner;
EditText editText;
TextView textView5;
CheckBox checkBox;
LinearLayout linearLayout;
View rootView;
public ProductViewHolder(View itemView) {
super(itemView);
spinner = itemView.findViewById(R.id.spinner);
editText = itemView.findViewById(R.id.editText);
textView5 = itemView.findViewById(R.id.textView5);
checkBox = itemView.findViewById(R.id.checkBox);
rootView = itemView.findViewById(R.id.linearLayout);
}
public View getView() {
return rootView;
}
}
}
Although AlertDialog.Builder() accepts a Context reference in its argument, it is better to pass an Activity reference to it, not applicationContext because of potential theme and window attachment problems. So, pass an Activity reference to your adapter and use it to build an AlertDialog.
public class ProductAdapter extends SomeAdapter {
private Activity create;
public ProductAdapter(Activity activity, List<Product> productList) {
create = activity;
this.productList = productList;
}
private void showAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);
// ...
alertDialogBuilder.create().show();
}
}
You must pass context of ther activity there, check this link for more understanding.
Only pass the context of calling activity to your ProductAdapter class and use this

Item removed from ArrayList, but notifyDataSetChanged() only deletes the bottom item

I have a recycler view, with an ArrayList from custom objects.
Each row has a delete button, when clicked the item is deleted from the recyclerview.
Now the delete button works, and deletes the correct item from the arraylist, however when the items are displayed again in the list, only the bottom item is deleted, and the item which was really deleted from the list stays there.
Its really weird, and I cannot figure out why?
I manage the deleted item from the activity, here is the code of the activity:
public class ManageExerciseActivity extends AppCompatActivity {
private static final String TAG = "999.OffLimitsActivity";
private RecyclerView exerciseRecyclerView;
private ExerciseManageAdapter exerciseAdapter;
public ArrayList<Exercise> exerciseList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_exercise);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ManageExerciseActivity.this, AddExerciseActivity.class);
startActivity(intent);
}
});
fab.setImageBitmap(textAsBitmap("ADD", 40, Color.WHITE));
exerciseList = new ArrayList<>();
getSupportActionBar().setTitle("Manage my exercises");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
exerciseRecyclerView = (RecyclerView) findViewById(R.id.exercise_manage_recycler_view);
exerciseAdapter = new ExerciseManageAdapter(exerciseList);
exerciseAdapter.setOnItemClickListener(new ExerciseManageAdapter.ClickListener() {
#Override
public void onItemClick(final int position, final View v) {
Log.d(TAG, "onItemClick pos = " + position);
// custom dialog
AlertDialog.Builder builder = new AlertDialog.Builder(ManageExerciseActivity.this);
builder.setMessage("Do you wish to delete this exercise?")
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String itemRemoved = exerciseList.get(position).getNameOfExercise();
exerciseList.remove(position);
exerciseAdapter.notifyDataSetChanged();
Snackbar.make(v, itemRemoved + " deleted", Snackbar.LENGTH_LONG)
.setAction("Undo", null).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create();
builder.show();
}
});
exerciseRecyclerView.setAdapter(exerciseAdapter);
exerciseRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
and here is the adapter:
public class ExerciseManageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static ClickListener clickListener;
public ImageView exerciseItemIcon;
public TextView exerciseNameTextView;
public TextView exerciseDurationTextView;
public TextView exerciseTimesRemainingTextView;
public ImageView deleteImageView;
ArrayList<Exercise> exerciseArrayList = new ArrayList<>();
public ExerciseManageAdapter(ArrayList<Exercise> exerciseArrayList) {
this.exerciseArrayList = exerciseArrayList;
}
public interface ClickListener {
void onItemClick(int position, View v);
}
class ExerciseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ExerciseViewHolder(View view) {
super(view);
// view.setOnClickListener(this);
exerciseNameTextView = (TextView)view.findViewById(R.id.exercise_manage_name_textView);
exerciseDurationTextView = (TextView)view.findViewById(R.id.exercise_manage_duration_textview);
exerciseTimesRemainingTextView = (TextView)view.findViewById(R.id.exercise_manage_times_remaining);
exerciseItemIcon = (ImageView)view.findViewById(R.id.exercise_manage_item_icon);
deleteImageView = (ImageView)view.findViewById(R.id.delete_exercise_button);
deleteImageView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(ClickListener clickListener) {
ExerciseManageAdapter.clickListener = clickListener;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getItemCount() {
return exerciseArrayList.size();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.exercise_manage_item_row, parent, false);
return new ExerciseViewHolder(itemView);
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
ExerciseViewHolder exerciseViewHolder = (ExerciseViewHolder) holder;
exerciseNameTextView.setText(exerciseArrayList.get(position).getNameOfExercise());
exerciseDurationTextView.setText(exerciseArrayList.get(position).getDurationOfExercise());
exerciseTimesRemainingTextView.setText(exerciseArrayList.get(position).getTimesPerWeek() + " times a week");
exerciseItemIcon.setImageResource(exerciseArrayList.get(position).getExerciseIcon());
}
}
Thanks
try this.!
notifyItemRemoved(position);
notifyItemRangeChanged(position, mData.size());

How to remove and set the view of the dialog box in the adaptor class?

Getting the error:
The specified child already has a parent. You must call removeView() on the child's parent first.
Issue: Because I am calling the alert builder repeatedly so first I have to remove the view then need to set the view again.
Problem: I don't know how to remove and set the view in the adaptor.
public class ActionAdaptor extends SectionRecyclerViewAdapter<SectionHeader, Action, SectionViewHolder, ActionChildGoal> {
Context context;
DbHelper dbHelper;
HealthVitalsFunction interfaceAdapter;
public ActionAdaptor(Context context, List<SectionHeader> sectionItemList) {
super(context, sectionItemList);
this.context = context;
dbHelper = new DbHelper(context);
}
#Override
public SectionViewHolder onCreateSectionViewHolder(ViewGroup sectionViewGroup, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.section, sectionViewGroup, false);
return new SectionViewHolder(view);
}
#Override
public ActionChildGoal onCreateChildViewHolder(ViewGroup childViewGroup, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.action_list, childViewGroup, false);
return new ActionChildGoal(view);
}
#Override
public void onBindSectionViewHolder(SectionViewHolder sectionViewHolder, int sectionPosition, SectionHeader section) {
sectionViewHolder.name.setText(section.getSectionText());
}
#Override
public void onBindChildViewHolder(final ActionChildGoal holder, int sectionPosition, final int childPosition, final Action action) {
//for more options
//When nothing is there..
if (action.getBenefits().length()==0 && action.getSideEffects().length() == 0 && action.getRemarks().length()==0){
holder.moreOptions.setVisibility(View.GONE);
holder.actionSubSection.setVisibility(View.GONE);
}else {
String msg=" ";
if (action.getSideEffects().length()!=0)
msg=msg.concat("<br /> <strong>Side Effects</strong> <br /> "+action.getSideEffects());
holder.actionSubSection.setText(Html.fromHtml(msg));
holder.actionSubSection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialogBox("More Information ",holder.actionSubSection);
}
});
}
public void showDialogBox(String title, View text)
{
final AlertDialog.Builder alert=new AlertDialog.Builder(new ContextThemeWrapper(context,R.style.AlertBoxTheme));
alert.setView(new TextView(context));
text.setPadding(3,0,3,0);
alert.setView(text);
alert.setTitle(title);
alert.setPositiveButton("READ", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
}
You are calling alert.setView 2 times. That's the problem.
This line is problem:
alert.setView(text);
You are trying to set view for alert dialog with view which is currently added in RecyclerView, you need to create new one or inflate similar from xml.

Categories

Resources