Disabled click Item of ListView - java

I am using SimpleAdapter to bind ListView. Now, When User Clicks on Item, I want to disable that time from Click() event. I found some tutorial for isEnabled() but I am not understanding that how to use it ?
Please help me to solve this problem. I am using Custom ListView.
This below code disables ListView.
SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
R.layout.topicwisequestion, new String[] { "option" },
new int[] { R.id.option }) {
public boolean areAllItemsEnabled() {
return ignoreDisabled;
}
public boolean isEnabled(int position) {
if (areAllItemsEnabled()) {
return true;
}
return false;
}
};
lvTWOptions.setAdapter(adapter);
lvTWOptions.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Code...
}
});

Do it like this.
define a boolean variable
boolean ignoreDisabled = false;
Then in areAllItemsEnabled:
public boolean areAllItemsEnabled() {
return ignoreDisabled;
}
and then at the beginning of isEnabled:
public boolean isEnabled(int position) {
if (areAllItemsEnabled()) {
return true;
}
}
or else second way is define following code in your adapter class
private int mLastClicked;
public void setLastClicked(int lastClicked){
mLastClicked = lastClicked;
}
and then
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
// return false if position == position you want to disable
}
add position of your last click event with isEnabled method.
For further reference you can check this link
Hope this is gonna help you.
Do it like this.
ListAdapter adapter = new SimpleCursorAdapter(MyList, Layout, c,
new String[] { "Name", "Score" }, to)
{
public boolean areAllItemsEnabled()
{
return false;
}
public boolean isEnabled(int position)
{
return false;
}
};

I have used below code and solved my problem. It is very simple and easy. No need to use any extra codes.
What I have done is I have put condition to check position and then based on it, I disabled Clicked Item.
My Code :
int pos;
SimpleAdapter adapter = new SimpleAdapter(this, arrlist, R.layout.topicwisequestion, new String[] { "option" }, new int[] { R.id.option }) {
public boolean isEnabled(int position) {
if (position != 0) {
if (position == pos) {
return false;
} else {
return true;
}
} else {
return true;
}
}
};
lvTWOptions.setAdapter(adapter);
lvTWOptions.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pos = position;
}
});

You'll have to extend the simple adapter, maintain a list of indexes that have been clicked on, implement isEnabled(int) and check if the integer passed is in your list. You can then selectively disable list items after they've been clicked.
Adding some code:
public class MySimpleAdapter extends SimpleAdapter {
private List<Integer> mDisabledRows;
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
mDisabledRows = new ArrayList<Integer>();
}
public void disableRow(int index) {
mDisableRows.clear();
mDisabledRows.add(index);
}
#Override
public boolean isEnabled(int index) {
return !mDisabledRows.contains(index);
}
}
From your onItemClick method you would call adapter.disableRow(position)
I read that as you want to disable a row once it's clicked. If you only want to disable the last clicked row you could just store the last index clicked.

Related

RecyclerView first item is always selected

I tried every solution from all sites, but I can't fix this problem. As shown in the code I'm trying to add onClickListener to the RecyclerView, but the first item is always checked. I also tried to make the default checkedPosition to -1, but the first item of the list is unselectable. All other items work properly except for the first item.
public class JsonListAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
private static final int TYPE = -1;
private final List <Object> listRecyclerItem;
private int checkedPosition = 0;
public JsonListAdapter(Context context, List <Object> listRecyclerItem) {
this.context = context;
this.listRecyclerItem = listRecyclerItem;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public ItemViewHolder(#NonNull View itemView) {
super(itemView);
//findViewById here
}
}
#NonNull#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
switch (i) {
case TYPE:
default:
//LayoutInflater here
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
int viewType = getItemViewType(i);
switch (viewType) {
case TYPE:
default:
//add data to list here
}
row_linear.setOnClickListener(view - >{
checkedPosition = i;
notifyDataSetChanged();
});
radioButton.setOnClickListener(view - >{
checkedPosition = i;
notifyDataSetChanged();
});
if (checkedPosition == i) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}
}
}
when you get your list, initiate the list items with with extra Boolean, and create getter and setter:
private boolean isChecked = false;
public boolean getIsChecked() {
return isChecked;
}
public void setIsChecked(boolean checked) {
isChecked = checked;
}
Then in your adapter modify your condition to check both:
if (listRecyclerItem.get(i).getIsChecked() && checkedPosition == i)
Next, you'll need to uncheck an item as soon as another item is checked (assuming you're making it single select):
radioButton.setOnClickListener(view- > {
listRecyclerItem.get(i).setIsChecked(false); // to uncheck previous item
checkedPosition=i;
listRecyclerItem.get(i).setIsChecked(true); // to check current item
notifyDataSetChanged();
});
if (listRecyclerItem.get(i).isChecked&&checkedPosition==i) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}

Prevent to add data in ArrayList of type Class

How can i prevent the dublicate data to be added in my ArrayList of class
When i will add a data to array list then it will check that whether it is dublicate or not if yes it will not add the data to ArrayList
I am using this code to prevent the adding of dublicate elements but it's not working
Code
public class ScrollingActivity extends AppCompatActivity implements AdaptreForRecycler.OnItemClickListener {
private RecyclerView recyclerView;
private AdaptreForRecycler adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<ExampleItem> mExampleList;
private Gson gson;
private String json;
private Type type;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
loadData();
buildRecyclerView();
editText = findViewById(R.id.editText);
fabButoonClick();
}
public void saveData() {
sharedPreferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
editor = sharedPreferences.edit();
gson = new Gson();
json = gson.toJson(mExampleList);
editor.putString("text", json);
editor.apply();
}
public void loadData() {
sharedPreferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
gson = new Gson();
json = sharedPreferences.getString("text", null);
type = new TypeToken<ArrayList<ExampleItem>>() {
}.getType();
mExampleList = gson.fromJson(json, type);
if (mExampleList == null) {
mExampleList = new ArrayList<>();
}
}
public void insertItem(String text) {
ExampleItem ex = new ExampleItem(text);
if ( mExampleList.contains(ex)) {
Toast.makeText(this, "Already Exists", Toast.LENGTH_SHORT).show();
} else {
mExampleList.add(ex);
}
adapter.notifyDataSetChanged();
}
ExampleItem deletedIndex = null;
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition();
String name = mExampleList.get(position).getText1();
deletedIndex = (mExampleList.get(position));
mExampleList.remove(position);
sharedPreferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.remove("text");
saveData();
editor.apply();
adapter.notifyItemRemoved(position);
Snackbar.make(recyclerView, name + " Deleted", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
mExampleList.add(position, deletedIndex);
adapter.notifyItemInserted(position);
saveData();
}
}).show();
}
#Override
public void onChildDraw(#NonNull Canvas c, #NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addSwipeLeftBackgroundColor(ContextCompat.getColor(ScrollingActivity.this, R.color.my_background))
.addSwipeLeftActionIcon(R.drawable.ic_delete_black_24dp)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
};
private void buildRecyclerView() {
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(ScrollingActivity.this);
adapter = new AdaptreForRecycler(this, mExampleList);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
}
public void fabButoonClick() {
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder b = new AlertDialog.Builder(ScrollingActivity.this);
View mview = getLayoutInflater().inflate(R.layout.dialogbox_frontpage, null);
final EditText editText = mview.findViewById(R.id.editText);
b.setView(mview);
b.setTitle("Add subject name");
b.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String text = editText.getText().toString();
if (text.isEmpty()) {
Toast.makeText(ScrollingActivity.this, "Please add subject name", Toast.LENGTH_SHORT).show();
} else {
insertItem(text);
saveData();
}
}
});
b.setCancelable(false);
b.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(int position) {
Intent i = new Intent(ScrollingActivity.this, StudentListActivity.class);
i.putExtra("Subject Name", mExampleList.get(position).getText1());
startActivity(i);
}
}
There's a pretty simple way to check if an item already exists in the list:
Override the equals method.
You will have to override the equals method inside your ExampleItem class.
I don't know the contents, ie. code, of your ExampleItem but this is just my guess so I suggest you to change your code accordingly.
public class ExampleItem {
private String text1, text2;
#Override
public boolean equals(Object o) {
// Check if the given Object 'o' is an instance,
// ie. same class, of this ExampleItem class
if (o instanceof ExampleItem) {
// Cast the object to ExampleItem
ExampleItem other = (ExampleItem) o;
// Check if both have same text
// (I'm guessing that you only wanted to
// compare the text1 variable, otherwise,
// check comment below.)
// Objects.equals() will evaluate whether
// both text are the same
return Objects.equals(this.text1, other.text1); // 'this' is unnecessary, only to show
// If you want to check both then uncomment
// the code below and remove the one above
// return Objects.equals(text1, other.text1) && Objects.equals(text2, other.text2);
}
// If above fails then I'll just use the
// before-overwritten equals to avoid
// writing myself each tiny bit of
// its implementation
return super.equals(o);
}
public ExampleItem(String text1) {
this.text1 = text1;
}
public ExampleItem(String text1, String text2) {
this.text1 = text1;
this.text2 = text2;
}
public String getText1() {
return text1;
}
public void setText1(String t1) {
text1 = t1;
}
public String getText2() {
return text2;
}
public void setText2(String t2) {
text2 = t2;
}
}
The contains method of the List then will use your overridden/custom equals method to compare the given item to other items in it.
And that's it, just like a plug-and-play kinda code thing. You can now expect your mExampleList.contains(ex) to work.
And surely I hope that it does work and as always, happy coding!
I am guessing you want to prevent duplicated elements to be added to ArrayList. But it is not ArrayList's strength. If you want to keep just unique elements, just use the HashSet data structure which will only retain unique elements.
But what need extra attention is that how you define duplicates, whether two elements has same contents or they have same id(which is exactly the same object).
This solution is a further extension adopted from this post.Unique value ArrayList
It has all behaviors of array list since you can add and retrieve element as an ArrayList but keep uniqueness at the same time.
Below is the code of the ArrayListSet that you can create on your own.
import java.util.*;
public class ArrayListSet<E> implements Iterable<E>, Set<E> {
private ArrayList<E> list;
private HashSet<E> set;
public ArrayListSet() {
list = new ArrayList<>();
set = new HashSet<>();
}
public boolean add(E e) {
return set.add(e) && list.add(e);
}
public boolean add(int i, E e) {
if (!set.add(e)) return false;
list.add(i, e);
return true;
}
public void clear() {
list.clear();
set.clear();
}
public boolean contains(Object o) {
return set.contains(o);
}
public E get(int i) {
return list.get(i);
}
public boolean isEmpty() {
return list.isEmpty();
}
public E remove(int i) {
E e = list.remove(i);
set.remove(e);
return e;
}
public boolean remove(Object o) {
if (set.remove(o)) {
list.remove(o);
return true;
}
return false;
}
public boolean set(int i, E e) {
if (set.contains(e)) return false;
set.add(e);
set.remove(list.set(i, e));
return true;
}
public int size() {
return list.size();
}
public void sort(Comparator<? super E> c) {
Collections.sort(list, c);
}
public Iterator<E> iterator() {
return list.iterator();
}
public boolean addAll(Collection<? extends E> c) {
int before = size();
for (E e : c) add(e);
return size() == before;
}
public boolean containsAll(Collection<?> c) {
return set.containsAll(c);
}
public boolean removeAll(Collection<?> c) {
return set.removeAll(c) && list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
return set.retainAll(c) && list.retainAll(c);
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
}
Yes it is possible, you can use arrayList.contains(val) after scanning the val variable.
If it return 1, then print("val already exits.") otherwise,
If it returns 0, then you can add it in the arrayList
See how .contains() method work
You can also use .indexOf() method to find out the position. Refer here
To use .contains() with an Object You need to override hashcode and equals method . Refer here, watch the comments of marked Answer

How to change the text of a Button in recyclerview when adapter class is loading second time?

This is what I want according to picture
I am sharing my code
Model Class
class Msg{
private Boolean BtnName;
public Boolean getBtnName() {
return BtnName;
}
public void setBtnName(Boolean btnName) {
BtnName = btnName;
}
}
AdapterClass:
#Override
public void onBindViewHolder(final TeachersTodayScheduleViewHolder holder, final int position) {
mTeacherScheduleArrayList.get(position).setBtnName(false);
holder.btn_checkin.setTag(position);
holder.btn_checkin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Object data = v.getTag();
if (data != null) {
// I have set the Boolean value to true after clicking this particular button, but whn the arraylist again loading, the boolean value disaapear
mTeacherScheduleArrayList.get(position).setBtnName(true);
if(mFlagtrue!=null && mFlagtrue.contentEquals("flag_true"))
{
if(mTeacherScheduleArrayList.get(position).getBtnName().equals(true))// when it return true, then below colde work, this is not happening
{
holder.btn_checkin.setText("Attended");
holder.btn_checkin.setBackgroundResource(R.color.green_btn);
holder.btn_checkin.setEnabled(false);
}
}
}
What am I doing wrong, please help,
Thanks in advance
public void onBindViewHolder(final TeachersTodayScheduleViewHolder holder, final int position) {
mTeacherScheduleArrayList.get(position).setBtnName(false); // <<<<<<
.......
You're setting the value to false every time the view is bound. Remove this line.

How do I hide specific Group in ExpandableListView using CheckBox

In my ExpandableListView I have a list of planets as groups, and I want one of those groups to disappear when a CheckBox from the main activity is checked.
Unfortunately, this happens:
Filter Unchecked
Filter Checked
Here is the code that affects the view visibility. As you can see, i put 1 as the int for 'groupPosition' so the view that gets hidden should be Saturn and not Jupiter and the result is the same no matter what the value of 'groupPosition' is.
final MyAdapter myAdapter = new MyAdapter(this, headings, childList);
expandableListView.setAdapter(myAdapter);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(checkBox.isChecked()){
myAdapter.getGroupView(1,false,findViewById(R.id.planet), expandableListView).setVisibility(View.GONE);
myAdapter.notifyDataSetChanged();
}
if(!checkBox.isChecked()){
myAdapter.getGroupView(1,false,findViewById(R.id.planet), expandableListView).setVisibility(View.VISIBLE);
myAdapter.notifyDataSetChanged();
}
}
});
Thank you in advance.
EDIT: That worked Kris, thank you. Now, how can I effectively apply this filter to one of the childViews here? (the children are HashMap) eg.
public void childFilter(int position, boolean filterCheck) {
planet_child_shown.clear();
String str;
if(!filterCheck) {
for (List<String> l : planet_child.values()) {
for (int i = 0; i < l.size(); i++) {
if (i!=position) {
str = l.get(i);
l.add(str);
}
}
}
}
notifyDataSetChanged();
}
I am trying to filter out a String depending on its position in the List<> here and put it back into its own "shown" HashMap> called planet_child_shown. This always throws out a nullPointer however, and points to my getChildrenCount() method which i modelled after your getGroupCount():
public int getChildrenCount(int groupPosition) {
return planet_child_shown == null ? 0 : planet_child_shown.get(mPlanetShown.get(groupPosition).name)
.size();
}
With ListViews, you will avoid a lot of problems if you stay strict MVC:
Controller (i.e. onCheckedChanged) updates the Model.
Model is converted to View by getView (in your case, getGroupView).
What you are doing is trying to update the view directly from the controller. This will always cause problems.
You need to do something like this:
Model class (edited):
public class Planet {
public String name;
public boolean filtered;
private List<String> mChildren;
private List<Boolean> mChildrenFiltered;
private List<String> mChildrenShown;
public Planet(String name, List<String> children) {
this.name = name;
mChildren = children;
mChildrenFiltered = new ArrayList<>();
for (String child : children) {
mChildrenFiltered.add(false);
}
mChildrenShown = new ArrayList<>(children);
}
public int getChildrenCount() {
return mChildrenShown == null ? 0 : mChildrenShown.size();
}
public String getChild(int position) {
return mChildrenShown.get(i);
}
public void filter(String child, boolean filter) {
mChildrenShown.clear();
for (int i = 0; i < mChildren.size(); i++) {
if (mChildren.get(i).equals(child)) {
mChildrenFiltered.set(i, filter);
}
if (! mChildrenFiltered.get(i)) {
mChildrenShown.add(mChildren.get(i));
}
}
}
}
Controller:
final MyAdapter myAdapter = new MyAdapter(this, headings, childList);
expandableListView.setAdapter(myAdapter);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myAdapter.filter("Saturn", isChecked);
}
});
Now in your adapter, you will have two lists: the list of all the planets and the list of the planets that will show up in your ExpandableListView. Checking the checkbox will change only the list of planets that are shown, which is what you will use when creating the group view.
MyAdapter.java: (Editied, added a child filter method)
public class MyAdapter extends BaseExpandableListAdapter {
private List<Planet> mPlanets;
private List<Planet> mPlanetsShown;
public MyAdapter(List<Planet> planets) {
mPlanets = planets;
mPlanetsShown = new ArrayList<>(mPlanets);
}
#Override
public int getGroupCount() {
return mPlanetsShown == null ? 0 : mPlanetsShown.size();
}
#Override
public Object getGroup(int groupPosition) {
return mPlanetsShown == null ? null : mPlanetsShown.get(groupPosition);
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Planet planet = (Planet) getGroup(groupPosition);
// TODO create the view
return null;
}
#Override
public int getChildrenCount(int groupPosition) {
return mPlanetsShown.get(groupPosition).getChildrenCount();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return mPlanetsShown.get(groupPosition).getChild(childPosition);
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
String child = mPlanetsShown.get(groupPosition).getChild(childPosition);
// TODO create the view
return null;
}
public void filter(String planetName, boolean filter) {
mPlanetsShown.clear();
for (Planet planet : mPlanets) {
// if this is the planet we are changing, set the flag
if (planet.name.equals(planetName)) {
planet.filtered = filter;
}
// whether this is the planet we are changing or not,
// add this to planets shown if the flag is not set
if (! planet.filtered) {
mPlanetsShown.add(planet);
}
}
notifyDataSetChanged();
}
public void filter(String planetName, String childName, boolean filter) {
for (Planet planet : mPlanets) {
if (planet.name.equals(planetName)) {
planet.filter(childName, filter);
}
}
notifyDataSetChanged();
}
// TODO some code left out here
}

How to Delete Item From Listview on Android

I want to delete item when I long clicked. First ı want to get position and then ı want to remove any item.
My MainActivity
public class MainActivity extends AppCompatActivity {
final List<Kisi> kisiler = new ArrayList<Kisi>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kisiler.add(new Kisi("Ahmet Yılmaz", false, 0));
kisiler.add(new Kisi("Ayşe Küçük", true, 1));
kisiler.add(new Kisi("Fatma Bulgurcu", true, 2));
kisiler.add(new Kisi("İzzet Altınmeşe", false, 3));
kisiler.add(new Kisi("Melek Subaşı", true, 4));
kisiler.add(new Kisi("Selim Serdilli", false, 5));
kisiler.add(new Kisi("Halil İbrahim", false, 6));
final ListView listemiz = (ListView) findViewById(R.id.liste);
final ozelAdapter adaptorumuz = new ozelAdapter(this, kisiler);
listemiz.setAdapter(adaptorumuz);
}
}
My Adapter:
package com.endroidteam.customlistview;
public class ozelAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Kisi> mKisiListesi;
public ozelAdapter(Activity activity, List<Kisi> kisiler) {
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mKisiListesi = kisiler;
}
#Override
public int getCount() {
return mKisiListesi.size();
}
#Override
public Object getItem(int position) {
return mKisiListesi.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View satirView;
satirView = mInflater.inflate(R.layout.satir, null);
TextView textView =
(TextView) satirView.findViewById(R.id.isimsoyisim);
ImageView imageView =
(ImageView) satirView.findViewById(R.id.simge);
Kisi kisi = mKisiListesi.get(position);
textView.setText(kisi.getIsim());
if (kisi.isKadinMi()) {
imageView.setImageResource(R.drawable.android_icon_big_13);
}
else {
imageView.setImageResource(R.drawable.android_icon_big_4);
}
return satirView;
}
}
My GetterSetter:
private String isim;
private boolean kadinMi;
private int konum;
public Kisi(String isim, boolean kadinMi, int konum) {
super();
this.isim = isim;
this.kadinMi = kadinMi;
this.konum = konum;
}
#Override
public String toString() {
return isim;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public int getKonum() {
return konum;
}
public void setKonum(int konum) {
this.konum = konum;
}
public boolean isKadinMi() {
return kadinMi;
}
public void setKadinMi(boolean kadinMi) {
this.kadinMi = kadinMi;
}
}
Please help me how can I remove the selected item from the following listview:
I assume you want to remove an item on the list when it gets button press - In the function where the button press gets called, get item position and remove the item like so:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
return true;
}
});
kissiler.remove(position)
and then call
adapter.notifyDataSetChanged()
That will solve your problem.
Full code:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
kissiler.remove(position);
adapter.notifyDataSetChanged();
return true;
}});
EDIT: I made a terrible mistake and used id instead of position - obviously you'll have to use the position variable in the functions parameters as it gives an integer where the item is located in your listview.
remove the elements from your adapter and notify the listView with
adapter.notifyDataSetChanged()

Categories

Resources