How to get the instances of all views created by an adapter? - java

I have a ArrayAdapter for a GridView, As you can see in the code I populate the GridView using the getView method with a custom view that called: GridViewCell.
Now on a click of this custom view there is a FlipAnimation method run that works great for the clicked view. What I want to achieve is to run the same animation method for all the other custom views currently in the adapter, so I need to get the instances of all the other views (there are 4 view in the adapter, so I need to get instances of the other 3).
public class AnswersGridAdapter extends ArrayAdapter<Answer>
{
private static final String TAG = AnswersGridAdapter.class.getSimpleName();
private ArrayList<Answer> answersList;
private Context context;
private MainActivity activity;
private QuestionGridFragment fragment;
private View view;
public AnswersGridAdapter(QuestionGridFragment fragment, Context context, int textViewResourceId, ArrayList<Answer> answersList)
{
super(context, textViewResourceId, answersList);
this.answersList = new ArrayList<Answer>();
this.answersList.addAll(answersList);
this.context = context;
this.activity = (MainActivity)context;
this.fragment = fragment;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
final View view = new GridViewCell(context);
if (answersList.get(position).isChecked())
{
((GridViewCell)view).FlipToBackWithoutAnimation();
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
((GridViewCell)view).FlipAnmiation();
if (answersList.get(position).isChecked())
{
answersList.get(position).setChecked(false);
}
else
{
answersList.get(position).setChecked(true);
}
activity.getEstimatedCostTextView().setText(String.valueOf(activity.getEstimatedCost()));
fragment.flipOtherChosenAnswer(position);
}
});
((GridViewCell)view).setTextToTitleTextView(answersList.get(position).getAnswerText());
return view;
}
}
I was trying to do something like this:
public void flipOtherChosenAnswer(int position)
{
int cellCount = answersAdapter.getCount();
for (int i=0; i < cellCount; i++ )
{
((GridViewCell)answersAdapter.getView(i, null, null)).FlipAnmiation();
}
}
but that didn't help me. How can this be done?

if you manages click in the onItemClick, the first argument should be the AdapterView, and trought it you can access its children.
int count = adpaterViewInstance.getChildCount();
for (int i = 0; i < count; i++) {
View childAt = adpaterViewInstance.getChildAt(i);
}
Edit:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adpaterViewInstance, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

Well this is what I finally did, from the fragment that contains the GridView I run this method, that get all the children of the GridView and run the animation method on each one of them.
public void flipOtherChosenAnswer(int position)
{
int cellCount = gvQuestionAnswers.getChildCount();
for (int i=0; i < cellCount; i++ )
{
if (i != position && qustionAnswersList.get(i).isChecked())
{
((GridViewCell)gvQuestionAnswers.getChildAt(i)).FlipAnmiation();
}
}
}

Related

Android ListView with Switch buttons - only one switch active at a time

I have a list view with two switches. I want the functionality to work were only one switch may be active at a time.
--UPDATED
My Adapter:
public class NotificationsAdapter extends ArrayAdapter<String> {
private Context context;
private String mTitle[];
private boolean onOff[];
public NotificationsAdapter(Context c, String mTitle[], boolean onOff[]) {
super(c, R.layout.adapter_notifications_layout, R.id.notificationsListTv, mTitle);
this.context = c;
this.mTitle = mTitle;
this.onOff = onOff;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.adapter_notifications_layout, parent, false);
Switch notificationSwitch = view.findViewById(R.id.switchNotificationsDaily);
TextView myTitle = view.findViewById(R.id.notificationsListTv);
myTitle.setText(mTitle[position]);
notificationSwitch.setChecked(onOff[position]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int size = onOff.length;
for (int i = 0; i < size; i++) {
if (i == position) {
break;
}
onOff[i] = false;
}
if (onOff[position]) {
notificationSwitch.setChecked(false);
onOff[position] = false;
} else {
onOff[position] = true;
notificationSwitch.setChecked(true);
}
}
});
return view;
}
My Class:
private String[] mTitle = new String[]{"Once Daily", "Twice Daily"};
private Switch notificationSwitch;
private boolean[] onOff = new boolean[] {false, false};
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(getActivity(), mTitle, onOff);
listView = view.findViewById(R.id.notificationsListView);
listView.setAdapter(notificationsAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
Currently with this code I can select both switches to be active at the same time. When I select more than one switch I would like the other to deactivate. Any assistance getting this functionality to work would be greatly appreciated.
In your activity:-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
resetValues(position);
if (onOff[position]) {
onOff[position] = false;
} else {
onOff[position] = true;
}
notifyDataSetChanged();
}
}
private void resetValues(int selectedPosition){
int size = onOff.length;
for(int i=0; i < size; i++){
if(i == selectedPosition){
continue;
}
onOff[i] = false;
}
}
And in your adapter:-
notificationSwitch.setChecked(onOff[position]);
Also, remove your click listener from adapter.
The logic is:- Making all other values as false except the selected item, then changing the state of the selected item based on its previous state.

How to add new item on button press in Base Adapter

I'm trying to crate something that creates a new ListView item on button press, I thing I have somewhere some bug, but as I'm new to this topic I don't know where.
I've tried rewriting the code several times, I've tried to use notifyDataSetChanged(); - it does nothing
tried googling looking on other topics here...
Here is my MainActivity.java:
public Button btn;
private ListView lv;
private CustomeAdapter customeAdapter;
public ArrayList<EditModel> editModelArrayList;
int populateListMaxNum =3;
int listNumber = populateListMaxNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
btn = (Button) findViewById(R.id.btn);
editModelArrayList = populateList();
customeAdapter = new CustomeAdapter(this,editModelArrayList);
lv.setAdapter(customeAdapter);
/* TODO activate button */
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addToList();
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<EditModel> populateList(){ //this part works perfectly
ArrayList<EditModel> list = new ArrayList<>();
for(int i = 0; i < populateListMaxNum; i++){
EditModel editModel = new EditModel();
//editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
}
return list;
}
/*TODO make it work = expand */
private void addToList(){ // this part doesn't work nor it doesn't fail
EditModel editModel = new EditModel();
editModelArrayList.add(editModel);
customeAdapter.notifyDataSetChanged();
}
}
Here is my CustomeAdapter.java class:
public class CustomeAdapter extends BaseAdapter {
private Context context;
public static ArrayList<EditModel> editModelArrayList;
public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) {
this.context = context;
CustomeAdapter.editModelArrayList = editModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return editModelArrayList.size();
}
#Override
public Object getItem(int position) {
return editModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.editText = convertView.findViewById(R.id.editid);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editText.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) {
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
return convertView;
}
private class ViewHolder {
protected EditText editText;
}
}
I expect to create a new list item (EditText + TextView) but nothing happens (except the Toast message)
(After some tweaks the app crashes due to arrayindexoutofboundsexception length=3 index=3 error, but not in this setting)
here are up to all files nessessary: https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965
Main linear layout in lv_item.xml has match_parent height. Try change it to wrap_content. It seems like one row is just taking whole screen.

Android Icon/Image setting pattern for multiple choices

What pattern do I have to use, if I have ListView in which ImageView and like 500 different icons that could be set on that ImageView. Should I just write If/Switch statement, or there is another way/pattern to do it?. Thanks in advance!
Let me assume that you know what icon(I mean the name of icon) to be loaded into the imageView and those icons are available in your drawable resource folder. In this case
#Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
DataItem dataItem = dataList.get(holder.getAdapterPosistion());
try {
int resID = activityContext.getResources().getIdentifier(dataItem.getIconName() , "drawable"/**resource folder name*/, activityContext.getPackageName());
holder.imageView.setBackgroundResource(resID);
} catch (Exception e) {
throw new RuntimeException("Error getting Resource ID.", e)
}
}
Where are these icons that you want to set? you are getting them from server or they are stored locally in your application file? or they are from user phone gallery?
Here is the code you want for your adapter:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<String> mIconNames;
public MyAdapter(Context context) {
mContext = context;
mIconNames = getIconNames();
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mIconNames.size();
}
#Override
public Object getItem(int position) {
return mIconNames.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.your_layout, parent, false);
ImageView thumbnailImageView =
(ImageView) rowView.findViewById(R.id.your_image_view_id);
Picasso.with(mContext).load(mIconNames.get(position)).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
return rowView;
}
//this method builds your icon names
private ArrayList<String> getIconNames() {
ArrayList<String> iconNames = new ArrayList<>();
int numberOfIcons = 99;
String iconBaseName = "icon";
for (int i = 1; i < numberOfIcons; i++) {
iconNames.add(iconBaseName + i);
}
return iconNames;
}
}

Unconditional layout inflation from view adapter warning

I am using GridView in my alert dialogue with below codes.its giving me error called in title. My codes and more required information is like below.
private void showGotoPageDialog() {
final Dialog mDialog = new Dialog(getActivity());
mDialog.setContentView(R.layout.grid_dialogue);
mDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
GridView mGridView = (GridView) mDialog.findViewById(R.id.grid_dialog);
ArrayList<String> tmp = new ArrayList<>(mPageOptions.length);
for(int i = 0; i < mPageOptions.length; i++)
{
tmp.add(mPageOptions[i].split(" ")[1]);
}
CustomAdapter adapter = new CustomAdapter(getActivity(), tmp, mPageIndx);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, tmp);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int item, long l) {
mDialog.dismiss();
mPageIndx = item + 1;
updateQuotesListServer();
updatePageInfo();
}
});
mDialog.show();
TextView dismiss = (TextView) mDialog.findViewById(R.id.dialog_dismiss);
dismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDialog.dismiss();
}
});
}
and Adapter like this
private class CustomAdapter extends BaseAdapter
{
private ArrayList<String> list;
private LayoutInflater inflater;
private int currentPage;
CustomAdapter(Activity activity, ArrayList<String> list, int currentPage)
{
this.list = new ArrayList<>();
this.list.addAll(list);
inflater = (LayoutInflater) activity.getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE);
this.currentPage = currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = View.inflate(getActivity(),R.layout.dialogue_item, null);
TextView txt = (TextView) view.findViewById(R.id.textview_dialogue);
txt.setText(list.get(i));
if(i == currentPage-1)
{
txt.setTextColor(Color.BLACK);
}
return view;
}
}
its giving me warning on this line
view = View.inflate(getActivity(),R.layout.dialogue_item, null);
warning is like below
Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling.
My code is working fine as expected and I am not facing any issue. I want know that What changes require for fix this and what is benefit of it ? Let me know if someone can help me for come out from this and solve my doubt.
Thanks
View adapter is built so that instead of creating a new view for each item, views that scroll off screen can be reused (they are passed in via the 2nd argument of getView). You are being warned that you should try to reuse these views instead of always inflating new ones. Change the line to
if (view == null) view = View.inflate(getActivity(),R.layout.dialogue_item, null);
Try this instead
view = View.inflate(R.layout.dialogue_item, viewGroup, null);
If not necessary, in other words if view is not null, then it is not necessary to call the following line in getView() method :
view = inflter.inflate(R.layout.spinner_rect, null);
the problem arises the same wrong source code available on the web:
https://abhiandroid.com/ui/custom-spinner-examples.html
https://demonuts.com/android-custom-spinner-image/
apply my solution for correctness
Thanks for useful answers: I had the same error but I was using view binding. Resolved using this approach.

Onclick listner to expandable listview

I have created an expandable listview, but the onclick listener to the child list items could not be attached.
The activity code:
public class MyActivity extends Activity {
private ExpandableListView mExpandableList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 2; i++){
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 3; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
}
}
The custom adapter for the lists:
public class MyCustomAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
#Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
#Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
#Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
#Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
}
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
}
The parent class:
public class Parent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}
What should I do to add onclick listener to the child list items?
Add this after you setAdapter of the Expandable list
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
/* You must make use of the View v, find the view by id and extract the text as below*/
TextView tv= (TextView) v.findViewById(R.id.childTextView);
String data= tv.getText().toString();
return true; // i missed this
}
});
You need to add
ChildClickListener
like this : mExpandableList.setOnChildClickListener
add this line to onCreate method
read here
also this is a good example
Declare your listView with an overridden onChildClick
ExpandableListView listView = getExpandableListView();
listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {
Log.d(TAG,"I got clicked childPosition:["+childPosition+"] groupPosition:["+groupPosition+"] id:["+id+"]");
return true;
}
});

Categories

Resources