Reputation: 5757
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...
}
});
Upvotes: 2
Views: 1439
Reputation: 2732
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.
Upvotes: 1
Reputation: 5757
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;
}
});
Upvotes: 1
Reputation: 5234
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;
}
};
Upvotes: 2