How to save state of CheckBox while scrolling in ListView? - android

I have used ListView with custom adapter, so my ListView includes CheckBox, but problem is that when I scroll down, checked CheckBoxes uncheck, how can I do it with this code?
UPDATE
My custom adapter:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
}
And my Activity:
public class AppsActivity extends ListActivity {
private final String DefaultApps = "def_apps";
private List<Integer> checkedItems = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems);
setListAdapter(adapter);
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position);
else checkedItems.add((Integer) position);
}

// initialise the list
private List<Integer> selection = new ArrayList<Integer>();
for (String str: prefNameDefaultApps.split("\n")) {
selection.add(0);
}
// setting the list
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(CheckBox is checked) {//(check if the checkbox is checked )
selection.set(position, 1);
}
else{
selection.set(position, 0);
}
}
// show in the listView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
....
....
check.setChecked(selection.get(position) == 0 ? false : true);
}
This should get you going :)

I'm not entirely sure whether there is a standard solution in Android, but this should do the trick:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;//Here we save the position of the selected items.
private final String DefaultApps = "def_apps";
//This will be invoked when one of the CheckBoxes gets clicked. It will update the checkedItems list.
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
if(checkedItems.contains(position)) checkedItems.remove(position);
else checkedItems.add(position);
}
};
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
//You shouldn't inflate the View everytime getView() is called,
/*LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);*/
//rather let the superclass handle it as it's more efficient.
View rowView = super.getView(position, convertView, parent);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));//XXX
check.setTag((Integer) position);//XXX
check.setOnClickListener(onClickListener);//XXX
return rowView;
}
}
In your ListActivity, add a field private List<Integer> checkedItems = new ArrayList<Integer>;. You have to pass this List in the Adapter constructor!
If you want to check whether the nth item in your ListView was selected, just look whether n is saved in checkedItems, i.e.
if(checkedItems.contains(requestedItemPosition)) {
//Item was selected.
} else {
//Item was not selected.
}
I really recommend you not to inflate the views by yourself.

Try this: (your custom adapter)
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
LayoutInflater inflater;
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
View rowView = convertView;
ViewHolder viewH = new ViewHolder();
if ( rowView == null){
rowView = inflater.inflate(R.layout.apps, parent, false);
viewH.text_apps = (TextView)rowView.findViewById(R.id.text_apps);
viewH.check = (CheckBox)rowView.findViewById(R.id.check_apps);
rowView.setTag(viewH);
}else{
viewH = (ViewHolder) rowView.getTag();
}
viewH.text_apps.setText(prefNameDefaultApps.split("\n")[position]);
viewH.check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
static class ViewHolder {
public TextView text_apps;
public CheckBox check;
}
}
Hope this helps :)

you should use
MyAdapter newad=(MyAdapter)adapter.getAdapter();
newad.notifyDataSetChanged();
this code in onitem click method
and MyAdapter is class of Adapter and make new object like this one
and in adapter class you should have to write a line
LayoutInflater in;
in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
in adapter constructor simple

Related

Custom ListView is not working properly when clicked

So I have a simple Custom ListView. When a button in the ListView is clicked, a Toast is supposed to be created (as a test, will later switch to an activity depending on the pressed category).
The problem is, that nothing happens when any of the buttons are clicked.
Not sure how to fix it at the moment.
Category Table
public class CategoryTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_table);
populateTable();
}
private void populateTable() {
final ListView mListView = findViewById(R.id.listView);
Category One = new Category(" One");
Category Two = new Category(" Two");
Category Three = new Category(" Three");
Category Four = new Category(" Four");
final ArrayList<Category> categoryList = new ArrayList<>();
categoryList.add(One);
categoryList.add(Two);
categoryList.add(Three);
categoryList.add(Four);
CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
Custom Adapter
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
return convertView;
}
}
I've also added
android:descendantFocusability="blocksDescendants"
to the custom adapter xml file (didn't seem to change anything though).
Will include more code if needed.
Any help would be greatly appreciated.
its because you have the listener for when you click the actual row, not the button inside the row. if you want it on button click you will need to do something like this
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
catName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make call back to activity to do whatever you want
}
});
return convertView;
}
}

ListView modify other view from specific view

I have a ListView that contains 5 rows with 5 button:
When I click on first button, I replace it text with "B".
Then, I click on second button, I replace it text with "B", but how can I replace the previous clicked button (the first) with "A"?
This is what really happen:
This is my code for the button click:
private int resource;
private LayoutInflater inflater;
ViewHolder holder;
private Context ctx;
public RingtoneAdapter(Context context, int resourceId, List<Ringtone> objects) {
super(context, resourceId, objects);
this.ctx = context;
resource = resourceId;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View v, ViewGroup parent) {
// Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
final Ringtone ringtone = getItem(position);
if (v == null) {
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.btnPlay = (Button) v.findViewById(R.id.btnPlay);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button btnadd = (Button) view;
btnadd.setText("B");
notifyDataSetChanged();
}
});
return v;
}
private static class ViewHolder {
Button btnPlay;
TextView txtTitolo;
TextView txtCategoria;
}
If I'm reading this correctly, you only want a single position to be 'selected' at a time. If that's the case, you could keep a reference to the selected position, and use the Adapter's functionality instead of having multiple buttons know about each other. e.g.
private int selectedPosition = -1;
#Override
public View getView(final int position, View convertView, ViewGroup parent){
// ... Your ViewHolder / Ringtone initialization
if(position == selectedPosition)
holder.btnPlay.setText("B");
else holder.btnPlay.setText("A");
holder.btnPlay.setOnClickListener(new View.OnClickListener(){
if(holder.btnPlay.getText().toString().equals("A"))
selectedPosition = position;
else selectedPosition = -1;
notifyDataSetChanged();
});
return convertView;
}
The key here is allowing notifyDataSetChanged() to internally call your getView method, which then really only needs to know how to render the data in its current state, instead of dealing with the complex logic required to map between multiple Views.
you need to reset the items which were not clicked,
btn.setText("B"); // if the item was clicked
btn.setText("A"); // for all other items
what you can do is, cycle through all elements setting the text for button like this btn.setText("A"), once this is done call btn.setText("B")
you should be using OnItemClickListener on the listview, it is easy that way, as you are setting the value in view holder, it does not give you access to the other items in the listview, but by using OnItemClickListener you can cover all the elements in the listview once you update things as you like call notifyDataSetChanged(), As this can be done there is another way to look at the problem
ListView listView = (ListView) findViewById(R.id.ListViewTestSevenActivity_listView);
ArrayList<Item> items = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Item item = new Item("A");
items.add(item);
}
MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
This will take care of initialization and rest of the code as you want it to work. code is also available on github
private class MyAdapter extends ArrayAdapter {
private Context a_context;
private ArrayList<Item> items;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<Item> items) {
super(context, resource, items);
this.a_context = context;
this.items = items;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_listview_seven, parent, false);
holder = new ViewHolder();
holder.button = (Button) row.findViewById(R.id.ListViewTestSevenActivity_text_button);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Item item = items.get(position);
System.out.println("" + item.getText());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (Item current_item : items) {
current_item.setText("A");
}
item.setText("B");
notifyDataSetChanged();
}
});
holder.button.setText("" + item.getText());
return row;
}
private class ViewHolder {
Button button;
}
}
private class Item {
String text;
public Item(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

ListView item selection issue. Deselect others when one selected

I have a custom listview: http://i.stack.imgur.com/oBCCG.png
Up on clicking an item of 'ListView' the 'ImageView' with that "mark" becomes visible.
But the problem is that I need only ONE item to be marked. How can I deselect all others items up on click?
CustomList adapter:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.listview_item);
viewHolder.postImageView = (ImageView) convertView.findViewById(R.id.image_mark);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position));
viewHolder.postNameView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to deselect all other items here?
if (viewHolder.postImageView.getVisibility() != View.VISIBLE) {
viewHolder.postImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.postImageView.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
class ViewHolder {
TextView postNameView;
ImageView postImageView;
}
}
setting the adapter:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time_zone, container, false);
// Set the adapter
mListView = (ListView) view.findViewById(R.id.listView_timezones);
imageCheck = (ImageView) view.findViewById(R.id.image_mark);
mListView.setAdapter(new TimeZoneItemAdapter(
getActivity(),
R.layout.listview_item,
dataTimezones.getZonesList()
));
return view;
}
Thanks for any help!
You might want to keep track of the currently selected list view item position in a dedicated integer variable. Then you would only need to call notifyDataSetChanged() on the adapter each time an item is selected and set the ImageView as visible in getView() if yourSelectedPosition == position.
Also check out the setOnItemClickListener() method on ListView.
EDIT:
Here's some code.
First in the Activity attach a listener to the ListView:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
timeZoneItemAdapter.selectedPosition = position;
timeZoneItemAdapter.notifyDataSetChanged();
}
});
and the adapter should look something like this:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public int selectedPosition = -1;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = myContext.getLayoutInflater().inflate(R.layout.listview_item, null);
TextView postNameView = (TextView) convertView.findViewById(R.id.listview_item);
ImageView imageMark = (ImageView) convertView.findViewById(R.id.image_mark);
postNameView.setText(datas.get(position));
imageMark.setVisibility(selectedPosition == position ? View.VISIBLE : View.GONE);
return convertView;
}
}
This solution gets rid of the ViewHolder, but would it work for you?

Android set items into Spinner by custom Adapter

in my application i have custom BaseAdapter for Spinner for insert data from List into that,
in this method i can return List as an data:
public List<String> getSmsNumberList() {
return Arrays.asList(smsNumbers.split("\\s*,\\s*"));
}
after geting successfull data from that i'm prepare to insert into Spinner:
smsNumbers = G.getInfo().getSmsNumberList();
panel_numbers = (String[]) smsNumbers.toArray(new String[smsNumbers.size()]);
SpinnerArrayAdapter dataAdapter = new SpinnerArrayAdapter(this);
spinner_panel_numbers.setAdapter(dataAdapter);
smsNumbers have some data returned by getSmsNumberList() function, now i'm using this below custom BaseAdapter for view data into Spinner :
Adapter code UPDATED
private class SpinnerArrayAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
List<String> list;
public SpinnerArrayAdapter(ActivitySmsSendGroup con, List<String> list) {
mInflater = LayoutInflater.from(con);
this.list = list;
}
#Override
public int getCount() {
return list.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.panel_number_spinner_style, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.textView1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.name.setText(list[position] + "");
return v;
}
}
static class ListContent {
TextView name;
}
unfortunately my Spinner dialog dont have data and that empty
Here:
panel_numbers = (String[]) smsNumbers.toArray(new
String[smsNumbers.size()]);
Because already have List so no need to convert it to Array just add one more parameter to constructor of SpinnerArrayAdapter class :
List<String> list;
public SpinnerArrayAdapter(ActivitySmsSendGroup con,List<String> list) {
mInflater = LayoutInflater.from(con);
this.list=list;
}
Now use list instead of panel_numbers everywhere in SpinnerArrayAdapter class
Pass List from Activity in SpinnerArrayAdapter class constructor when create object:
SpinnerArrayAdapter dataAdapter = new SpinnerArrayAdapter(this,smsNumbers);
In your adapter you should add a method, and variable for your ArrayList (add your own object here)
public static ArrayList<ListObject> listItems;
public void addItems(ArrayList<ListObject> mListItems) {
listItems = new ArrayList<ListObject>();
listItems.clear();
listItems.addAll(mListItems);
notifyDataSetChanged();
}
Then in your main class call
SpinnerArrayAdapter.addItems(myList);
Note: Ensure within getView that your'e setting variables based on listItem in the class ie. title.setText(listItems.get(position).name);
Your adapter should look like this:
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_row, parent, false);
spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);
spnItemName.setText(iName.get(position)+"");
spnItemDel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
//iName[position] = null;
iName.remove(position);
notifyDataSetChanged();
}
});
return rowView;
}
For full source code please see my github project

The get View function in my custom adapter class is not returning any value

The data source which i am passing to constructor of custom adapter class contains correct value. here is the code for calling custom adapter,
CAforSearchResults ca = new CAforSearchResults(this, user, s1);
lv.setAdapter(ca);
"user" contains correct value which i am passing to constructor.
here is my custom adapter class
public class CAforSearchResults extends ArrayAdapter {
Context ctx;
ArrayList<User_table> user;
SharedPreferences sf;
int userId;
public CAforSearchResults(Context ctx, ArrayList<User_table> user,
ArrayList<String> s) {
super(ctx, R.layout.searchresult, R.id.textView1, s);
this.ctx = ctx;
this.user = user;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.searchresult, parent, false);
}
ImageView iv1 = (ImageView) row.findViewById(R.id.imageView1);
TextView tv1 = (TextView) row.findViewById(R.id.textView1);
final int p = position;
if (position < user.size()) {
iv1.setImageBitmap(BitmapFactory.decodeFile(user.get(position).getDp()));
tv1.setText(user.get(position).getName());
tv1.setClickable(true);
tv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
return row;
}
}
Now this list view lv is not showing the results.
You need to give the list of elements to display to the super constructor. Here, your ArrayList<String> s is maybe empty...

Categories

Resources