Good day, I'm making a mistake where the search view always opens the zero position
Continuously opens zero position when filtering.
how can I solve this problem
Kitalar Adaptor
public class KitalarAdaptor extends ArrayAdapter {
ArrayList<KitalarVersiyon> items;
public KitalarAdaptor(Context context, int layout, ArrayList<KitalarVersiyon> items) {
super(context,layout);
this.items=items;
}
public void update(ArrayList<KitalarVersiyon> result){
items=new ArrayList<>();
items.addAll(result);
notifyDataSetChanged();
}
public class ViewHolder{
TextView textview;
ImageView ımageView;
TextView yüzölçümü;
TextView nufus;
TextView ülkesayısı;
}
#Override
public int getCount() {
return items.size();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View row;
row=convertView;
ViewHolder viewHolder;
if (row==null){
row=LayoutInflater.from(getContext()).inflate(R.layout.kitalricinozzellayout,parent,false);
viewHolder=new ViewHolder();
viewHolder.ımageView=row.findViewById(R.id.kitalarimage);
viewHolder.textview=row.findViewById(R.id.kitalartext);
viewHolder.yüzölçümü=row.findViewById(R.id.yüzölçümü);
viewHolder.nufus=row.findViewById(R.id.nufus);
viewHolder.ülkesayısı=row.findViewById(R.id.ülkesayisi);
row.setTag(viewHolder);
}
else{
viewHolder=(ViewHolder) row.getTag();
}
viewHolder.ımageView.setImageResource(items.get(position).kitalarimage);
viewHolder.textview.setText(items.get(position).kitalarname);
viewHolder.yüzölçümü.setText(items.get(position).yüzölçümüaaaa);
viewHolder.nufus.setText(items.get(position).nufus);
viewHolder.ülkesayısı.setText(items.get(position).ülkesayısı);
return row;
}
}
KitalarVersiyon
public class KitalarVersiyon {
String kitalarname;
int kitalarimage;
String yüzölçümüaaaa;
String nufus;
String ülkesayısı;
public KitalarVersiyon(String kitalarname, int kitalarimage, String yüzölçümüaaaa, String nufus, String ülkesayısı) {
this.kitalarname = kitalarname;
this.kitalarimage = kitalarimage;
this.yüzölçümüaaaa = yüzölçümüaaaa;
this.nufus = nufus;
this.ülkesayısı = ülkesayısı;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu,menu);
MenuItem menuItem=menu.findItem(R.id.searchmenu);
final SearchView searchView=(SearchView)menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
ArrayList<KitalarVersiyon> result=new ArrayList<>();
for (KitalarVersiyon x:items){
if (x.kitalarname.contains(s))
result.add(x);
}
((KitalarAdaptor)listView.getAdapter()).update(result);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent ıntent=new Intent(view.getContext(),Afrikakitasi2.class);
startActivity(ıntent);
}
}
});
Related
I have a recycler view, which consists of edittext and buttons for each view. I would like to have functionality in such a way that if I enter some text or attach an image for a view, then some of the buttons' functionality needs to be disabled in that view as soon as I click the button.
With the below code, I can attach images when ProofClick function is called via the interface, and then I set the button as disabled for the same position, but the button is not disabled, and I can attach images to the same view twice, which I do not want.
Adapter class:
// Check if the button is clicked and disable
private boolean clicked = false;
public InsStepsContentAdapter(List<InspectonScroll> scrollList,
ClickOnRecycle clickOnRecycle, String[] options) {
this.scrollList = scrollList;
this.clickOnRecycle = clickOnRecycle;
this.options = options;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_recycler_steps, parent, false));
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setIsRecyclable(false);
holder.progressbar.setMax(scrollList.size());
holder.title.setText(scrollList.get(position).getSteps().get(position).getTitle());
holder.btnAttach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(clicked){
clicked = true;
clickOnRecycle.ProofClick(holder.getAdapterPosition());
holder.btnAttach.setEnabled(true);
} else{
holder.btnAttach.setEnabled(false);
}
}
});
}
public int getItemCount() {
return scrollList.size();
}
#Override
public int getItemViewType(int position) {
return position;
}
public void updateList(List<ScrollList> scrollList){
this.scrollList = scrollList;
notifyDataSetChanged();
}
Model class:
public class InspectonScroll {
private List<Steps> Steps = new ArrayList<>();
public List<Steps> getSteps() {
return steps;
}
public void setSteps(List<Steps> steps) {
this.steps = steps;
}
}
ButtonAdapter
public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.ViewHolder> {
Activity activity;
ArrayList<ButtonModel> arrayList;
public ButtonAdapter(Activity activity, ArrayList<ButtonModel> arrayList) {
this.activity = activity;
this.arrayList = arrayList;
}
#NonNull
#Override
public ButtonAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(activity.getLayoutInflater().inflate(R.layout.row_item,parent,false));
}
#Override
public void onBindViewHolder(#NonNull ButtonAdapter.ViewHolder holder, int position) {
holder.button.setText("item"+position);
ButtonModel item = arrayList.get(position);
holder.button.setEnabled(item.isEnabled);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(item.isEnabled){
item.isEnabled = false;
notifyDataSetChanged();
}
Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
Button button;
public ViewHolder(#NonNull View itemView) {
super(itemView);
button = itemView.findViewById(R.id.button);
}
}
}
ButtonModel
public class ButtonModel {
boolean isEnabled = true;
}
one thing can be done here is maintaining the state of the button in the model as shown in the example and by using that state we can enable or disable the button in the onBindViewHolder method .
I have a display problem with my 4-stage expandableList.
I can display my data, expand them but the last child does not display the whole list. How would you do it?
Here is a picture and the part of the code in question, I think it comes from the fact that I declare the size of the last list.
public static class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> materiau;
private HashMap<String, ArrayList<MatosItem>> materiauItemList;
int c = 1;
LayoutInflater inflater3;
public ThirdLevelAdapter(Context context,HashMap<String, ArrayList<MatosItem>> materiauItemList,ArrayList<String> materiau) {
this.context = context;
this.materiauItemList = materiauItemList;
this.materiau = materiau;
this.inflater3 = LayoutInflater.from(context);
}
#Override
public Object getGroup(int groupPosition) {
return materiau.get(groupPosition);
}
#Override
public int getGroupCount() {
return materiau.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1,
parent,false);
//Initialization and assign variable
TextView textView = convertView.findViewById(android.R.id.text1);
//Initial string pour le nom de famille
String sGroup = String.valueOf(getGroup(groupPosition));
//Set text on text view
textView.setText(sGroup);
//Set text style bold
textView.setTypeface(null, Typeface.BOLD);
//Set text color
//textView.setBackgroundColor(Color.parseColor("#FF018786"));
textView.setTextColor(Color.parseColor("#FF018786"));
textView.setTextSize(1,20);
//Return View
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return materiauItemList.get(materiau.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view = inflater3.inflate(R.layout.adapter_item2, null);
//get information about item
MatosItem currentItem = (MatosItem) getChild(i,i1);
String itemName = currentItem.getName();
String itemNumber = currentItem.getNumber();
Boolean intemCheck = currentItem.getCheck();
String itemRef = currentItem.getRef();
String itemCodefourn = currentItem.getCodefourn();
//get item name view
TextView itemNameView = view.findViewById(R.id.name);
itemNameView.setText(itemName);
//get item number view
TextView itemNumberView = view.findViewById(R.id.item_number);
itemNumberView.setText(itemNumber);
//get item ref view
TextView itemRefView = view.findViewById(R.id.ref);
itemRefView.setText(itemRef);
//get item codefournisseur
TextView itemCodefournView = view.findViewById(R.id.codefourn);
itemCodefournView.setText(itemCodefourn);
LinearLayout layout = view.findViewById(R.id.compteur);
Button btnplus = view.findViewById(R.id.btnplus);
Button btnmoins = view.findViewById(R.id.btnmoins);
TextView text = view.findViewById(R.id.text);
if(intemCheck){
//checkBox.setChecked(true);
mdescription.add(itemName);
mid.add(itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layout.setVisibility(View.VISIBLE);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
//checkBox.setChecked(true);
//Toast.makeText(viewGroup.getContext(),itemName , Toast.LENGTH_SHORT).show();
}
});
btnplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c+1;
text.setText(""+c);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
});
btnmoins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c-1;
text.setText(""+c);
mdescription.remove(""+itemName);
mid.remove(""+itemNumber);
mref.remove(itemRef);
mcodefourn.remove(itemCodefourn);
}
});
return view;
}
#Override
public int getChildrenCount(int groupPosition) {
return materiauItemList.get(materiau.get(groupPosition)).size();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
level 1-2 and 3 work fine and i can scroll
level 4 is not displayed completely and I can't scroll
Thank you for your help
I want to make use of expandlelistView to display products and the sub products, which is the childHolder, in the childHolder, it contains a textView and an Edittext which holds the value for the product count for each sub product, the problem here is this.
When I input the values in the edittext, upon collapsing the group, the data is lost in the edittext.
The values get duplicated in the other group edittext.
3.How to save the data for later use in the Application.
The code below.
The activity layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.ShelfCheckActivity">
<ExpandableListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:dividerHeight="0dp"
android:groupIndicator="#null"
android:descendantFocusability="beforeDescendants"/>
</RelativeLayout>
The adapter class
public class ShelfCheckAdapter extends BaseExpandableListAdapter {
ArrayList<ListItemModel> groupItem;
GroupViewHolder groupViewHolder;
ChildViewHolder childViewHolder;
Context context;
public LayoutInflater layoutInflater;
public ShelfCheckAdapter(ArrayList<ListItemModel> groupItem, Context
context) {
this.groupItem = groupItem;
this.context = context;
}
public void setInflater(LayoutInflater inflater)
{
this.layoutInflater = inflater;
}
#Override
public int getGroupCount() {
return groupItem.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return groupItem.get(groupPosition).getArrayList().size();
}
#Override
public Object getGroup(int groupPosition) {
return groupItem.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groupItem.get(groupPosition).getArrayList().get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View
convertView, ViewGroup parent) {
if(convertView == null)
{
groupViewHolder = new GroupViewHolder();
convertView = layoutInflater.inflate(R.layout.list_row_group,null);
groupViewHolder.groupTitle = (TextView)
convertView.findViewById(R.id.textViewGroup);
convertView.setTag(groupViewHolder);
}
else{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.groupTitle.setText(groupItem.get(groupPosition).getTitle());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean
isLastChild, View convertView, ViewGroup parent) {
if(convertView == null)
{
childViewHolder = new ChildViewHolder();
convertView =
LayoutInflater.from(context).inflate(R.layout.list_row_child,null);
childViewHolder.childTitle =
convertView.findViewById(R.id.textViewChild);
childViewHolder.et = convertView.findViewById(R.id.productCount);
convertView.setTag(childViewHolder);
}
else{
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.childTitle.setText(groupItem.get(groupPosition)
.getChildTitles().get(childPosition));
if (!groupItem.get(groupPosition).getArrayList()
.get(childPosition).getValue().equals(""))
childViewHolder.et.setText(groupItem.get(groupPosition)
.getArrayList().get(childPosition).getValue());
else
childViewHolder.et.setText("");
childViewHolder.et.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus){
final EditText Caption = (EditText) v;
groupItem.get(groupPosition)
.getArrayList().get(childPosition).setValue(Caption.
getText().toString());
}
});
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private class GroupViewHolder {
public TextView groupTitle;
}
private class ChildViewHolder {
public TextView childTitle;
public EditText et;
}
}
The model class
public class ListItemModel {
String title;
ArrayList<EdittextValues> arrayList = new ArrayList<>();
public ListItemModel(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ArrayList<EdittextValues> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<EdittextValues> arrayList) {
this.arrayList = arrayList;
}
}
The pojo class
public class EdittextValues {
String value;
public EdittextValues(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The activity class
public class ShelfCheckActivity extends AppCompatActivity implements
SearchView.OnQueryTextListener {
private ExpandableListView expandableListView;
ShelfCheckAdapter shelfCheckAdapter;
ArrayList<ListItemModel> listItemModels;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shelf_check);
// initializing the views
initViews();
// preparing list data
//initListData();
}
/**
* method to initialize the views
*/
private void initViews() {
expandableListView = findViewById(R.id.listview);
initListData();
shelfCheckAdapter = new
ShelfCheckAdapter(listItemModels,ShelfCheckActivity.this);
shelfCheckAdapter
.setInflater((LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE));
expandableListView
.setAdapter(shelfCheckAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_store_check, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
private void initListData() {
ArrayList<EdittextValues> edittextValues = new ArrayList<>();
ArrayList<String> childTitle = new ArrayList<>();
childTitle.add("Product a");
childTitle.add("Product b");
childTitle.add("Product c");
childTitle.add("Product d");
childTitle.add("Product e");
childTitle.add("Product f");
childTitle.add("Product g");
childTitle.add("Product h");
childTitle.add("Product i");
childTitle.add("Product j");
childTitle.add("Product k");
childTitle.add("Product l");
for(int i = 0; i < childTitle.size(); i++)
{
edittextValues.add(new EdittextValues(""));
}
listItemModels = new ArrayList<>();
listItemModels.add(new ListItemModel("Product
1",edittextValues,childTitle));
listItemModels.add(new ListItemModel("Product
2",edittextValues,childTitle));
listItemModels.add(new ListItemModel("Product
3",edittextValues,childTitle));
listItemModels.add(new ListItemModel("Product
4",edittextValues,childTitle));
listItemModels.add(new ListItemModel("Product
5",edittextValues,childTitle));
listItemModels.add(new ListItemModel("Product
6",edittextValues,childTitle));
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
}
enter image description here
Just change below methods and check its working or not!
#Override
public View getChildView(int groupPosition, int childPosition, boolean
isLastChild, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.list_row_child,null);
childViewHolder = new ChildViewHolder(convertView, groupPosition, childPosition);
convertView.setTag(childViewHolder);
}
else{
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.childTitle.setText(groupItem.get(groupPosition)
.getChildTitles().get(childPosition));
return convertView;
}
Another change is
private class ChildViewHolder {
public TextView childTitle;
public EditText et;
public ChildViewHolder(View itemView, int groupPosition, int childPosition) {
super(itemView);
childTitle = itemView.findViewById(R.id.textViewChild);
et = itemView.findViewById(R.id.productCount);
et.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence != null){
groupItem.get(groupPosition).getArrayList().get(childPosition).setValue(charSequence.toString());
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
I am using checkboxes in all child view elements in ExpandableListView. To clear all checkboxes in one go, I am using a button clearFilters and the code for the Adapter class and the button click event is below.
Clicking the button doesn't do anything at all. I want to clear all checkboxes when the button is clicked.Any help is greatly appreciated.
ExpandableListAdapterClass:
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private Map<String, List<ChildViewModel>> expandableListDetail;
private static List<ChildViewHolder> checkedViewHolders=new ArrayList<>();
static int checkedBoxesCount;
public ExpandableListViewAdapter(Context context, List<String> expandableListTitle, Map<String,
List<ChildViewModel>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public int getGroupCount() {
return expandableListTitle.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return expandableListDetail.get(expandableListTitle.get(groupPosition)).size();
}
#Override
public String getGroup(int groupPosition) {
return expandableListTitle.get(groupPosition);
}
#Override
public ChildViewModel getChild(int groupPosition, int childPosition) {
return expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
String listTitle=getGroup(groupPosition);
GroupViewHolder groupViewHolder;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.expanded_list_group,null);
groupViewHolder=new GroupViewHolder();
groupViewHolder.listTitleTextView=(TextView)view.findViewById(R.id.txtExpandedListTitle);
view.setTag(groupViewHolder);
}else {
groupViewHolder=(GroupViewHolder)view.getTag();
}
groupViewHolder.listTitleTextView.setText(listTitle);
return view;
}
#Override
public View getChildView(final int groupPosition, final int childPosition, boolean b, View view, ViewGroup viewGroup) {
final ChildViewModel expandedListText=getChild(groupPosition,childPosition);
final ChildViewHolder childViewHolder;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.expanded_list_item,null);
childViewHolder=new ChildViewHolder();
childViewHolder.expandedListTextView=(TextView)view.findViewById(R.id.txtExpandedListItem);
childViewHolder.checkBox=(CheckBox)view.findViewById(R.id.expandeditem_chkbox);
view.setTag(childViewHolder);
}else {
childViewHolder=(ChildViewHolder)view.getTag();
}
childViewHolder.expandedListTextView.setText(expandedListText.getName());
if(expandedListText.isCheckStatus()){
childViewHolder.checkBox.setChecked(true);
}else {
childViewHolder.checkBox.setChecked(false);
}
childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChildViewModel model;
if(childViewHolder.checkBox.isChecked()){
checkedBoxesCount++;
model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition);
model.setCheckStatus(true);
expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model);
checkedViewHolders.add(childViewHolder);
notifyDataSetChanged();
Toast.makeText(context,"Checked value is"+expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
}else {
checkedBoxesCount--;
if(checkedBoxesCount==0){
Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
}
model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition);
model.setCheckStatus(false);
checkedViewHolders.remove(childViewHolder);
expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model);
notifyDataSetChanged();
}
}
});
return view;
}
public void clearChecks(){
for(int i=0;i<checkedViewHolders.size();i++){
checkedViewHolders.get(i).checkBox.setChecked(false);
notifyDataSetChanged();
}
checkedViewHolders.clear();
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public class GroupViewHolder {
TextView listTitleTextView;
}
public class ChildViewHolder {
TextView expandedListTextView;
CheckBox checkBox;
}
}
Here is the MainActivity class:
public class MainActivity extends AppCompatActivity {
ExpandableListView mExpandableListView;
ExpandableListViewAdapter mExpandableListAdapter;
Button clearFilters;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandableListView=(ExpandableListView)findViewById(R.id.expandedListView);
clearFilters=(Button)findViewById(R.id.btnClearFilter);
mExpandableListAdapter=new ExpandableListViewAdapter(MainActivity.this,getTitles(),getNames());
mExpandableListView.setAdapter(mExpandableListAdapter);
clearFilters.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mExpandableListAdapter.clearChecks();
}
});
}
}
If the motive is to simply uncheck all checkboxes, we can use this technique to iterate through the Hashmap and set all checked status to false. It will clear all checkboxes in all childViews.
public void clearChecks(){
for (List<ChildViewModel> value:expandableListDetail.values()) {
for(ChildViewModel sample:value){
sample.setCheckStatus(false);
}
}
notifyDataSetChanged();
I'm using a custom adapter to load 20 items from a JSON string as well as implement ads in the 4th and 14th position. What I'm encountering is that when I scroll to view item #19, it crashes with the error in the title.
I know that the error is saying that it's trying to access an index that is not in the array but I think it's because it's included the adViews in the index (which it shouldn't). I feel like this is something simple that I am missing but please help. Here are my 2 adapters:
public class ListViewAdapter extends BaseAdapter implements AdListener {
private final Activity activity;
private final BaseAdapter delegate;
public ListViewAdapter(Activity activity, BaseAdapter delegate) {
this.activity = activity;
this.delegate = delegate;
}
#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() + 2;
}
#Override
public Object getItem(int position) {
// Return null if an item is an ad. Otherwise return the delegate item.
if (isItemAnAd(position)) {
return null;
}
return delegate.getItem(position - 1);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (isItemAnAd(position)) {
if (convertView instanceof AdView) {
return convertView;
} else {
AdView adView = new AdView(activity, AdSize.SMART_BANNER,"AD ID (removed for post)" );
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
return adView;
}
} else {
return delegate.getView(position-1, convertView, parent);
}
}
#Override
public int getViewTypeCount() {
return delegate.getViewTypeCount() + 1;
}
#Override
public int getItemViewType(int position) {
if (isItemAnAd(position)) {
return delegate.getViewTypeCount();
} else {
return delegate.getItemViewType(getOffsetPosition(position));
}
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return (!isItemAnAd(position)) && delegate.isEnabled(getOffsetPosition(position));
}
private boolean isItemAnAd(int position) {
// Place an ad at the first and last list view positions.
return (position == 4 || position == 14);
}
#Override
public void onDismissScreen(Ad arg0) {
}
#Override
public void onFailedToReceiveAd(Ad arg0, AdRequest.ErrorCode arg1) {
}
#Override
public void onLeaveApplication(Ad arg0) {
}
#Override
public void onPresentScreen(Ad arg0) {
}
#Override
public void onReceiveAd(Ad arg0) {
}
private int getOffsetPosition(int position) {
return position - 1;
}
}
Here's the custom adapter which sets the listview:
class CustomMovieAdapter extends BaseAdapter {
private ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public CustomMovieAdapter(Context context, ArrayList<SearchResults> results){
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return searchArrayList.size()+2;
}
#Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
assert convertView != null;
holder.txtRating = (TextView) convertView.findViewById(R.id.movieRating);
holder.txtMovieName = (TextView) convertView.findViewById(R.id.movieName);
holder.txtMovieSize = (TextView) convertView.findViewById(R.id.movieSize);
holder.txtImdbRating = (TextView) convertView.findViewById(R.id.imdbScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtRating.setText(searchArrayList.get(position+1).getRating());
holder.txtMovieName.setText(searchArrayList.get(position+1).getName());
holder.txtMovieSize.setText(searchArrayList.get(position+1).getSize());
holder.txtImdbRating.setText(searchArrayList.get(position+1).getImdbRating());
return convertView;
}
class ViewHolder {
TextView txtRating;
TextView txtMovieName;
TextView txtMovieSize;
TextView txtImdbRating;
}
}
#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() + 2;
}
you should return the size of the dataset without changing it. If you need to show more items, add those to the dataset. Changing it with
#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() ;
}
Should fix your exception.
Edit, in getView you should avoid tampering the position android is providing you:
position+1