I'm making some listview based app with ability to delete specific row with a button in it, like on screenshot below: Screenshot
Here are my classes:
MainActivity:
package com.example.patryk.notes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
LayoutInflater layoutInflater;
ArrayList<Item> itemList = new ArrayList<Item>();
ItemArrayAdapter itemArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemArrayAdapter = new ItemArrayAdapter(this, R.layout.list_item, itemList);
listView = (ListView) findViewById(R.id.item_list);
listView.setAdapter(itemArrayAdapter);
layoutInflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) layoutInflater.inflate(R.layout.list_view_footer, listView, false);
listView.addFooterView(footer);
itemList.add(new Item("Item", 1));
}
public void addRow(View v) {
itemList.add(new Item("Item", itemList.size() + 1));
Log.d("add", "metoda add row");
itemArrayAdapter.notifyDataSetChanged();
}
}
ItemArrayAdapter
package com.example.patryk.notes;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by patryk on 04.01.2018.
*/
public class ItemArrayAdapter extends ArrayAdapter<Item> {
private int listItemLayout;
ImageButton deleteButton;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
Item item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listItemLayout,parent, false);
// viewHolder.item = (TextView) convertView.findViewById(R.id.row_item);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
deleteButton = (ImageButton) convertView.findViewById(R.id.xButtonID);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
**here expected to do something**
}
});
return convertView;
}
private static class ViewHolder {
TextView item;
}
}
item
package com.example.patryk.notes;
/**
* Created by patryk on 04.01.2018.
*/
public class Item {
private String name;
private double value;
public Item(String name, double value) {
this.value = value;
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I've tried many ideas from Stack Overflow but always there has been my clue missing. Firstly I wanted to make delete action on OnclickListener method in ItemArrayAdapter class, but the problem was to access ArrayList "itemList" located in MainActivity class, so I couldn't perform itemList.remove(position).
On the other hand I wanted to make deleteRow method in MainActivity and attach it to delete button on ItemArrayAdapter class by adding android:onClick="deleteRow" tag, but that makes exception similar to that: IllegalStateException
I'd rather chose first idea, so I would like to know how to get access to this ArrayList within adapter class
cheers :)
Reference variable of ArrayList is send to the adapter class. So you can easily do this:
public class ItemArrayAdapter extends ArrayAdapter<Item> {
private int listItemLayout;
ImageButton deleteButton;
private ArrayList<Item> itemList;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList;
}
Then
deleteButton = (ImageButton) convertView.findViewById(R.id.xButtonID);
deleteButton.setTag(position);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int) v.getTag();
itemList.remove(pos);
notifyDataSetChanged();
notifyDataSetInvalidated();
}
});
You don't need to access the MainActivity, item list is available in the constructor
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
}
You better assign to a variable and use it.
Like this
Private ArrayList <Item> itemList;
public ItemArrayAdapter(Context context, int layoutId, ArrayList<Item> itemList) {
super(context, layoutId, itemList);
listItemLayout = layoutId;
this.itemList = itemList
}
After remove please call notifytheDataSetChange method to refresh the list view.
Related
I am new, and I am making an app that displays data in a cardView within a recyclerView in android studio. I am trying to be able to delete the data that is displayed on the cardView. The cardView and data gets deleted as it should, but once I go back to the activity holding the cardView and recyclerView, it's all still there. Does anyone know why this is happening? I'll post my code below
ReminderDBHelper:
package com.example.lasttry;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList reminder_id, reminder_title, reminder_location, reminder_medication;
reminderDBHelper mydb;
Button reminderDelete;
CustomAdapter(Context context,
ArrayList reminder_id,
ArrayList reminder_title,
ArrayList reminder_location,
ArrayList reminder_medication) {
this.context = context;
this.reminder_id = reminder_id;
this.reminder_title = reminder_title;
this.reminder_location = reminder_location;
this.reminder_medication = reminder_medication;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.reminder_id.setText(String.valueOf(reminder_id.get(position)));
holder.reminder_title.setText(String.valueOf(reminder_title.get(position)));
holder.reminder_location.setText(String.valueOf(reminder_location.get(position)));
holder.reminder_medication.setText(String.valueOf(reminder_medication.get(position)));
holder.reminderDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mydb = new reminderDBHelper(context);
String id = String.valueOf(reminder_id);
mydb.deleteData(id);
reminder_id.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getLayoutPosition());
}
});
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(reminder_id.get(holder.getAdapterPosition())));
intent.putExtra("title", String.valueOf(reminder_title.get(holder.getAdapterPosition())));
intent.putExtra("location", String.valueOf(reminder_location.get(holder.getAdapterPosition())));
intent.putExtra("medication", String.valueOf(reminder_medication.get(holder.getAdapterPosition())));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return reminder_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView reminder_id, reminder_title, reminder_location, reminder_medication;
Button reminderDelete;
LinearLayout mainLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
reminderDelete = itemView.findViewById(R.id.reminder_delete);
reminder_id = itemView.findViewById(R.id.reminder_id);
reminder_title = itemView.findViewById(R.id.reminder_title);
reminder_location = itemView.findViewById(R.id.reminder_location);
reminder_medication = itemView.findViewById(R.id.reminder_medication);
mainLayout = itemView.findViewById(R.id.mainLayout);
}
}
}
CustomAdapter:
package com.example.lasttry;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private ArrayList reminder_id, reminder_title, reminder_location, reminder_medication;
reminderDBHelper mydb;
Button reminderDelete;
CustomAdapter(Context context,
ArrayList reminder_id,
ArrayList reminder_title,
ArrayList reminder_location,
ArrayList reminder_medication) {
this.context = context;
this.reminder_id = reminder_id;
this.reminder_title = reminder_title;
this.reminder_location = reminder_location;
this.reminder_medication = reminder_medication;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.reminder_id.setText(String.valueOf(reminder_id.get(position)));
holder.reminder_title.setText(String.valueOf(reminder_title.get(position)));
holder.reminder_location.setText(String.valueOf(reminder_location.get(position)));
holder.reminder_medication.setText(String.valueOf(reminder_medication.get(position)));
holder.reminderDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mydb = new reminderDBHelper(context);
String id = String.valueOf(reminder_id);
mydb.deleteData(id);
reminder_id.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getLayoutPosition());
}
});
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(reminder_id.get(holder.getAdapterPosition())));
intent.putExtra("title", String.valueOf(reminder_title.get(holder.getAdapterPosition())));
intent.putExtra("location", String.valueOf(reminder_location.get(holder.getAdapterPosition())));
intent.putExtra("medication", String.valueOf(reminder_medication.get(holder.getAdapterPosition())));
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return reminder_id.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView reminder_id, reminder_title, reminder_location, reminder_medication;
Button reminderDelete;
LinearLayout mainLayout;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
reminderDelete = itemView.findViewById(R.id.reminder_delete);
reminder_id = itemView.findViewById(R.id.reminder_id);
reminder_title = itemView.findViewById(R.id.reminder_title);
reminder_location = itemView.findViewById(R.id.reminder_location);
reminder_medication = itemView.findViewById(R.id.reminder_medication);
mainLayout = itemView.findViewById(R.id.mainLayout);
}
}
}
I have searched stackOverflow and reddit, but nothing seems to work. I have tried making the array array list instead.
I am trying to send the user to another activity when he clicks on the picture inside the recycle view after gathering the position, the problem whatever I do i still gitting null on the Adapter when using context, please find the codes below and describe to me what is the thing that I missed ?!
the problem is in the codes of onclick method ( last one ):-
package com.example.boc.Teachers;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.boc.R;
import com.example.boc.main.DashboardActivity;
import java.util.ArrayList;
public class TeachersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater mInflater;
private ArrayList<TeachersModel> items;
private TeacherMain mActivity;
private Context context;
public TeachersAdapter(ArrayList<TeachersModel> data, TeacherMain activity) {
this.items = data;
this.mActivity = activity;
this.mInflater = LayoutInflater.from(mActivity);
}
public void addItem(TeachersModel result) {
items.add(result);
}
public TeachersAdapter(Context context) {
this.context = context;
}
public void setInflater(LayoutInflater layoutInflater){
this.mInflater =layoutInflater;
}
public void replaceItems(ArrayList<TeachersModel> newItems) {
this.items.clear();
for(TeachersModel item: newItems)
this.items.add(item);
}
public void insertItem(TeachersModel item) {
items.add(0, item);
}
public void clearItems(){
items.clear();
}
public void AddResults(ArrayList<TeachersModel> result) {
items.addAll(result);
}
public TeachersModel getItemsAt(int position){
return items.get(position);
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
TeachersModel model = items.get(position);
MessageViewHolder messageViewHolder = (MessageViewHolder) holder;
messageViewHolder.imageViewIcon.setBackgroundResource(model.getImage());
messageViewHolder.textViewName.setText(model.getName());
messageViewHolder.textViewStatus.setText(model.getStatus());
messageViewHolder.textViewMobile.setText(model.getMobile());
}
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootCategoryView = mInflater.inflate(R.layout.row_item_teachers, parent, false);
return new MessageViewHolder(rootCategoryView, this);
}
private class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageViewIcon;
private TextView textViewName;
private TextView textViewStatus;
private TextView textViewMobile;
private CardView cardView;
private MessageViewHolder(View itemView, TeachersAdapter adapter) {
super(itemView);
imageViewIcon = (ImageView) itemView.findViewById(R.id.imageViewIcon);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewStatus = (TextView) itemView.findViewById(R.id.textViewStatus);
textViewMobile = (TextView) itemView.findViewById(R.id.textViewMobile);
cardView = (CardView) itemView.findViewById(R.id.cardView);
cardView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final int pos = getAdapterPosition();
if (pos == 0) {
imageViewIcon.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( context, DashboardActivity.class );
context.startActivity( intent );
}
} );
Toast.makeText(mActivity, "Selected Item Position "+pos, Toast.LENGTH_SHORT).show();
}
}
}
}
I am assuming you are using the constrcutor without passing a context:
//update the constructor to take a context
public TeachersAdapter(ArrayList<TeachersModel> data, TeacherMain activity , Context context) {
this.items = data;
this.mActivity = activity;
this.mInflater = LayoutInflater.from(mActivity);
//add this
this.context = context;
}
Set the click listener in onBindViewHolder:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
....
....
....
messageViewHolder.imageViewIcon.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, DashboardActivity.class );
context.startActivity(intent);
}
});
}
When you create the adapter in your activity, make sure to pass the correct context:
adapter = new TeachersAdapter(... , .... , getApplicationContext());
For my android project, I have used the recycle view in a layout which has 10+ view-holders each has a button and a text.
Whenever I click the button in the first item or view-holder, the 8th item is also auto clicked. And when I click the button in the 2nd one, the 9th button gets clicked.
So I searched this issue on google, then I found that this problem is a rare one.
And I also got a solution for this type, but that solved my problem partially.
So I want to know whether it is a bug or my mistake??
CaptionedImagesAdapterMenu.java
package com.boredboy.atithikhana;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.facebook.share.Share;
import java.util.Objects;
class CaptionedImagesAdapterMenu extends
RecyclerView.Adapter<CaptionedImagesAdapterMenu.ViewHolder>{
private String[] captions;
private String[] foodName;
private String[] desc;
private String[] price;
private Context context;
private Listener listener;
private Remover remover;
private SparseBooleanArray hidebtns = new SparseBooleanArray();
private SparseBooleanArray setnums = new SparseBooleanArray();
public interface Remover{
void onRemove(int position);
}
public interface Listener{
void onClick(int position);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
private Button button;
private ElegantNumberButton numberButton;
public ViewHolder(CardView v) {
super(v);
cardView = v;
this.button = v.findViewById(R.id.addbt);
this.numberButton = v.findViewById(R.id.numberincdec);
}
}
public CaptionedImagesAdapterMenu(Context context,String[] foodName, String[] desc,String[] captions, String[] price){
this.context = context;
this.foodName = foodName;
this.captions = captions;
this.desc = desc;
this.price = price;
}
public void setListener(Listener listener){
this.listener = listener;
}
public void setRemover(Remover remover){
this.remover =remover;
}
#Override
public int getItemCount(){
return captions.length;
}
#Override
public CaptionedImagesAdapterMenu.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_captioned_image_menu, parent, false);;
return new ViewHolder(cv);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, #SuppressLint("RecyclerView") final int position){
SharedPreferences preferences = context.getSharedPreferences("NEWV",0);
SharedPreferences.Editor editor = preferences.edit();
SharedPreferences prr = context.getSharedPreferences("ELEV",0);
SharedPreferences.Editor editor1 = prr.edit();
holder.button.setVisibility(hidebtns.get(position,false)?View.GONE:View.VISIBLE);
holder.numberButton.setVisibility(hidebtns.get(position,false)?View.VISIBLE:View.GONE);
String elval = prr.getString("elev","1");
holder.numberButton.setNumber(Objects.requireNonNull(setnums.get(position) ? elval : "1"));
CardView cardView = holder.cardView;
TextView textView = cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
TextView textView1 = cardView.findViewById(R.id.info_menu);
textView1.setText(desc[position]);
TextView textView2 = cardView.findViewById(R.id.info_price);
textView2.setText("₹ " + price[position]);
TextView textView3 = cardView.findViewById(R.id.food_Info);
textView3.setText(foodName[position]);
holder.button.setOnClickListener(view -> {
holder.button.setVisibility(View.GONE);
holder.numberButton.setVisibility(View.VISIBLE);
holder.numberButton.setNumber("1");
hidebtns.put(position,true);
setnums.put(position,false);
editor.putString("newv", String.valueOf(holder.numberButton.getNumber()));
editor.apply();
if(listener!=null)
{
listener.onClick(position);
}
});
holder.numberButton.setOnValueChangeListener((view, oldValue, newValue) -> {
if (newValue < 1) {
holder.button.setVisibility(View.VISIBLE);
holder.numberButton.setVisibility(View.GONE);
editor1.clear();
editor1.putString("elev", String.valueOf(newValue));
editor1.apply();
hidebtns.put(position,false );
setnums.put(position,true);
if (remover != null)
{
remover.onRemove(position);
}
editor.clear();
editor.apply();
}
if (newValue > 0) {
editor.putString("newv", String.valueOf(newValue));
editor.apply();
editor1.clear();
editor1.putString("elev", String.valueOf(newValue));
editor1.apply();
hidebtns.put(position,true);
setnums.put(position,true);
if(listener!=null)
{
listener.onClick(position);
}
}
});
}
}
As you know recyclerview recycle view for better performance.
While watching your code there is some visibility changes in view of your viewholder.
Just give a try for this solution.
Under onBindViewHolder add condition which checks data in your preference with respective your String.valueOf(holder.numberButton.getNumber()) and toggle visibility of your holder.button holder.numberButton view.
I am trying to delete a row or any row while by clicking on my custom listview adapter which extends Baseadapter. I have found many solutions on Google but none of them worked for me.I am successfully generating a listview with the help of this class but can't delete a row or any row.
Here is my code
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.BaseAdapter;
import com.brl.loverfreedatingapp.R;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class WhoLikedMeAdapter extends BaseAdapter {
private Context context; //context
private ArrayList<String> masterID = new ArrayList<String>();
private ArrayList<String> slaveID = new ArrayList<String>();
private ArrayList<String> masterName = new ArrayList<String>();
private ArrayList<String> slaveName = new ArrayList<String>();
private ArrayList<String> slaveUrl = new ArrayList<String>();
public WhoLikedMeAdapter(Context context, ArrayList<String> masterID, ArrayList<String> slaveID, ArrayList<String> masterName, ArrayList<String> slaveName, ArrayList<String> slaveUrl) {
this.context = context;
this.masterID = masterID;
this.slaveID = slaveID;
this.masterName = masterName;
this.slaveName = slaveName;
this.slaveUrl = slaveUrl;
}
#Override
public int getCount() {
return masterID.size(); //returns total of items in the list
}
#Override
public Object getItem(int position) {
return masterID.get(position); //returns list item at the specified position
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// inflate the layout for each list row
if (convertView == null) {
convertView = LayoutInflater.from(context).
inflate(R.layout.wholikeme_row, parent, false);
}
CircleImageView profile_image = (CircleImageView) convertView.findViewById(R.id.profile_image);
TextView name = (TextView)convertView.findViewById(R.id.name);
ImageButton unlike = (ImageButton)convertView.findViewById(R.id.unlike);
ImageButton like = (ImageButton)convertView.findViewById(R.id.like);
name.setText(slaveName.get(position));
//--
if(slaveUrl.get(position).equals("")){
Picasso.with(context)
.load(R.drawable.image_placeholder)
//.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.placeholder(R.drawable.image_placeholder)
.into(profile_image);
}else {
Picasso.with(context)
.load(slaveUrl.get(position))
//.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.placeholder(R.drawable.image_placeholder)
.into(profile_image);
}
//-------------
like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
masterID.remove(position);// not working
notifyDataSetChanged();
}
});
unlike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
masterID.remove(position);// not working
notifyDataSetChanged();
}
});
// returns the view for the current row
return convertView;
}
}
Found the solution! I have to remove all the data in a row to remove a listview row. Previously I was trying with only 1 data (masterID).
masterID.remove(position);
slaveID.remove(position);
masterName.remove(position);
slaveName.remove(position);
slaveUrl.remove(position);
notifyDataSetChanged();
In my project there is a listview which contains textview and checkbox..Here is my listactivity class:
package com.pack.custom;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class DemolistActivity extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
ArrayAdapter<Model> adapter = new MyArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<Model> getModel() {
List<Model> list = new ArrayList<Model>();
list.add(get("Bangladesh"));
list.add(get("India"));
list.add(get("China"));
list.add(get("Japan"));
list.add(get("Australia"));
list.add(get("Denmark"));
list.add(get("Germany"));
list.add(get("Indonesia"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private Model get(String s) {
return new Model(s);
}
}
And here is myarrayadapter class:
package com.pack.custom;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MyArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public MyArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
Now i want when i select checbox then this specific listitem is deleted.How can i do this?please help
Try this code, and let me know what happen...
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
if(list.getCount() > position)
{
list.remove(position);
this.notifyDataSetChanged();
}
}
else
{
Log.e("CheckBox", isChecked+"");
}
}
});