ArrayAdapter for country flag and image spinner slowing view down - java

I have the following class:
private class CountryAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
inflater = getLayoutInflater();
}
#Override
public View getDropDownView(int position, View convertView, #NonNull ViewGroup parent) {
return getCustomDropdown(position, convertView, parent);
}
#NonNull
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
View getCustomDropdown(int position, View convertView, ViewGroup parent) {
DropViewHolder dropViewHolder;
if (convertView == null) {
dropViewHolder = new DropViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row, parent, false);
convertView.setTag(dropViewHolder);
dropViewHolder.label = convertView.findViewById(R.id.spinner_countryNameRow);
dropViewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
dropViewHolder = (DropViewHolder) convertView.getTag();
}
dropViewHolder.label.setText(String.format("%s (+%s)", countryNameList.get(position), countryList.optJSONObject(position).optString("phoneCountryCode")));
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
dropViewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
View getCustomView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row_dropdown, parent, false);
convertView.setTag(viewHolder);
viewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase(); viewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
private class DropViewHolder {
TextView label;
AdapterImageView icon;
}
private class ViewHolder {
AdapterImageView icon;
}
}
I think it complies with all the standards for an ArrayAdapter, reusing views etc. However when opening the keyboard, the view redraws and there is significant lag on button positioning updating.
Is there anything glaringly obvious I'm missing?
Thanks in advance :)
EDIT: for extra clarity, AdapterImageView is just a class that extends ImageView and overrides requestLayout without calling super to prevent expensive redraws.
EDIT2: By contrast, this piece of code, which is older and in theory worse performing, does not lag the UI.
public class CountryAdapter extends ArrayAdapter<String> {
public CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
}
#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(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.country_spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_countryNameRow);
label.setText(countryNameList.get(position).toString());
ImageView icon = (ImageView) row.findViewById(R.id.spinner_countryFlagRow);
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return row;
}
}

Related

How to remove Toolbar goes transparent with a tint

I have made a new method in my app to set certain text and items to colors, as themes. But for some reason when I code it in a OOP way I get the toolbar to be tinted for some odd reason.
With the non OOP method i get:
https://gyazo.com/6084992a654573b85adfe0c01077627e
public void setOffTheme(){
slotsActivity.getToolbar().setTitle("\"Roulette\"");
slotsActivity.getPlaceBetText().setHint("\"Bet\"");
slotsActivity.getInstructions().setText("\"Pick a color\"");
slotsActivity.getStart().setText(" \"Start\" ");
Typeface tf =Typeface.createFromAsset(getResources().getAssets(), "Helvetica Bold.ttf");
slotsActivity.getInstructions().setTypeface(tf);
slotsActivity.getThemeImage().setImageResource(R.drawable.offtemplate);
slotsActivity.getmConstraintLayout().setBackgroundColor(getTheColor( R.color.colorAccent));
slotsActivity.getColorSpinner().setBackgroundColor(getTheColor( R.color.colorOffWhite));
slotsActivity.getInstructions().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getPlaceBetText().setTypeface(tf);
slotsActivity.getPlaceBetText().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getPlaceBetText().setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorGucciBlack) ));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner_style, slotsActivity.getBettingColors()){
//changes color of the drop down and selected
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(getResources().getColorStateList(R.color.colorGucciBlack) );
((TextView) v).setBackgroundColor(Color.TRANSPARENT);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextColor(getResources().getColorStateList(R.color.colorGucciBlack) );
v.setBackgroundColor(ContextCompat.getColor(settingsActivity.this, R.color.colorOffWhite));
((TextView) v).setGravity(Gravity.LEFT);
return v;
}
};
slotsActivity.getColorSpinner().setAdapter(dataAdapter);
slotsActivity.getStart().setBackgroundResource(R.drawable.buttonshapeoff);
slotsActivity.getStart().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getStart().setTypeface(tf);
slotsActivity.getToolbar().setBackgroundColor(getTheColor(R.color.colorOffWhite));
}
While the OOP code shows: https://gyazo.com/15e2ded1ccdc5a7514c370651c638499
//this is the call to the method
setTheme(R.color.colorAccent, R.color.colorOffWhite, R.color.colorGucciBlack, R.drawable.buttonshapeoff,R.drawable.offtemplate,"\"Roulette\"","\"Bet\"","\"Pick a color\"", " \"Start\" ","Helvetica Bold.ttf");
// this is the method
public void setTheme(final int backgroundColor, final int primaryColor, final int textColor,int buttonShape, int templete,String title,String betName,String pickName,String startName,String typrface){
Typeface tf =Typeface.createFromAsset(getResources().getAssets(), typrface);
slotsActivity.getInstructions().setTypeface(tf);
slotsActivity.getToolbar().setTitle(title);
slotsActivity.getPlaceBetText().setHint(betName);
slotsActivity.getInstructions().setText(pickName);
slotsActivity.getStart().setText(startName);
slotsActivity.getThemeImage().setImageResource(templete);
slotsActivity.getmConstraintLayout().setBackgroundColor(getTheColor( backgroundColor));
slotsActivity.getColorSpinner().setBackgroundColor(getTheColor( primaryColor));
slotsActivity.getInstructions().setTextColor(getTheColor(textColor));
slotsActivity.getPlaceBetText().setTypeface(tf);
slotsActivity.getPlaceBetText().setTextColor(getTheColor(textColor));
slotsActivity.getPlaceBetText().setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(textColor) ));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner_style, slotsActivity.getBettingColors()){
//changes color of the drop down and selected
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(getResources().getColorStateList(textColor) );
((TextView) v).setBackgroundColor(Color.TRANSPARENT);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextColor(getResources().getColorStateList(textColor) );
v.setBackgroundColor(ContextCompat.getColor(settingsActivity.this, primaryColor));
((TextView) v).setGravity(Gravity.LEFT);
return v;
}
};
slotsActivity.getColorSpinner().setAdapter(dataAdapter);
slotsActivity.getStart().setBackgroundResource(buttonShape);
slotsActivity.getToolbar().setBackgroundColor(primaryColor);
}

EditText Values getting cleared on scrolling listview android

public class Custom_Student_marks_list_faculty_adapter extends ArrayAdapter<MarksStudentListFacultyObject> {
private Activity context;
private List<MarksStudentListFacultyObject> studentlist;
public Custom_Student_marks_list_faculty_adapter(Activity context,List<MarksStudentListFacultyObject> studentlist) {
super(context,R.layout.custom_listview_marks_faculty,studentlist);
this.context=context;
this.studentlist=studentlist;
}
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView==null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id);
holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id);
String mark = holder.marks.getText().toString();
MarksStudentListFacultyObject student_list = studentlist.get(position);
holder.name.setText(student_list.getName());
holder.marks.setText(mark);
holder.marks.setTag(position);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}
holder.name.setText(studentlist.get(position).getName());
holder.marks.setText(studentlist.get(position).getMakrs());
return convertView;
}
}
class ViewHolder {
protected TextView name;
protected EditText marks;
}
I have seen many solution for it but it is not working out for me. Every time on scrolling, ListView values are getting cleared.
Hope this will work...
public Custom_Student_marks_list_faculty_adapter(Activity context,List<MarksStudentListFacultyObject> studentlist) {
super(context,R.layout.custom_listview_marks_faculty,studentlist);
this.context=context;
this.studentlist=studentlist;
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView==null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id);
holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}
MarksStudentListFacultyObject student_list = studentlist.get(position);
holder.name.setText(student_list.getName());
//holder.marks.setText(student_list.getMarks());
return convertView;
}
You will need to update the edittext values again in your student list because of the recycling nature of list view and also set the value on each time getview method is called.
Something like:
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView==null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id);
holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}
MarksStudentListFacultyObject student_list = studentlist.get(position);
holder.name.setText(student_list.getName());
holder.marks.setText(student_list.getMarks());
holder.marks.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
//set data to array when changed
student_list.setMarks(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
Try:
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView==null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id);
holder.marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}
String mark = holder.marks.getText().toString();
MarksStudentListFacultyObject student_list = studentlist.get(position);
holder.name.setText(student_list.getName());
holder.marks.setText(mark);
return convertView;
}
}
Try this:
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView==null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom_listview_marks_faculty, null);
}
TextView name = (TextView) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_name_id);
EditText marks = (EditText) convertView.findViewById(R.id.custom_listview_marks_faculty_stu_marks_id);
name.setText(studentlist.get(position).getName());
marks.setText(studentlist.get(position).getMakrs());
return convertView;
}
Edit :
Override getCount() method in ArrayAdaptor:
#Override public int getCount(){ return studentlist.size(); }

ExpandableListView.getGroupView() convertView arg null in fragment

Im new to android programming, the current architecture of my application is a Main Activity with a Fragment that fills the display. I am trying to create an ExpandableListView inside the Fragment which also fills the display. Everything works as i am expecting it, i think, however I cannot populate either the Child View or Group View.
In my Fragment Java class file i have;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ExpandableListView lv = (ExpandableListView) getView().findViewById(R.id.list);
lv.setAdapter(new ParentLevel(null));
}
ParentLevel which extends BaseExpandableListAdapter has;
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
return convertView;
}
convertView returns null, but parent returns the ExpandableListView in my layout file.
I'm not sure how to work with this or what to do next, if i wanted to just have a simple TextView, for a title, and a the group arrow what would i do?
I plan to use an enum for the data, which I am already inspecting for getGroupCount() and getChildCount()
I was able to solve my problem with this peice of code;
private final LayoutInflater inf;
private FragmentActivity activity;
public ParentLevel(FragmentActivity act, MenuItem current) {
activity = act;
inf = LayoutInflater.from(act);
};
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null) {
convertView = inf.inflate(R.layout.expandable_list_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(activity.getString(siblings.get(groupPosition).getName()));
return convertView;
}
and this class;
private class ViewHolder {
TextView text;
}

getView Custom Spinner Android not Called?

I want to create custom spinner and this is CustomSpinnerAdapter :
#Override
public int getCount() {
// TODO Auto-generated method stub
return (data == null) ? 0 : data.size();
}
#Override
public long getItemId (int position) {
return position;
}
#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(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_item_ip, parent, false);
TextView ip = (TextView) row.findViewById(R.id.ip);
ip.setText(data.get(position).toString());
return row;
}
and implement in Fragment
List<String> dataIpList = new ArrayList<String>();
dataIpList.add("192.168.1.1");
dataIpList.add("192.168.1.2");
dataIpList.add("192.168.1.3");
dataIpList.add("192.168.1.4");
_spinnerIpAdapter = new spinnerIpAdapter(getActivity(), R.layout.spinner_item_ip, dataIpList);
publicIP.setAdapter(_spinnerIpAdapter);
the result is getDropDownView us called , but getView never called .
so how to fix it ?

Changing the background color of a listview row conditionally

I'm trying to change the background row color of a list view based on a parameter and on the line View v = super.getView(position, convertView, parent) its giving me the error "Cannot directly invoke the abstract method getView(int, View, ViewGroup) for the type Adapter". Here is the all of the code.
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ListViewItem item = items.get(position);
if(convertView==null)
v = inflater.inflate(R.layout.my_hospitals, null);
TextView status = (TextView) v.findViewById(R.id.status);
TextView name = (TextView) v.findViewById(R.id.name);
TextView table = (TextView) v.findViewById(R.id.id);
TextView info = (TextView) v.findViewById(R.id.info);
status.setText(item.status);
name.setText(item.name);
table.setText(item.table);
info.setText(item.info);
if (item.status == "green"){
v.setBackgroundColor(Color.GREEN);
}
return v;
}
}
Change getView()
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v=convertView;
ListViewItem item = items.get(position);
if(v==null){
v = inflater.inflate(R.layout.my_hospitals, null);
}
TextView status = (TextView) v.findViewById(R.id.status);
TextView name = (TextView) v.findViewById(R.id.name);
TextView table = (TextView) v.findViewById(R.id.id);
TextView info = (TextView) v.findViewById(R.id.info);
status.setText(item.status);
name.setText(item.name);
table.setText(item.table);
info.setText(item.info);
String state=tem.status;
if (state.equals("green"){
v.setBackgroundColor(Color.GREEN);
}
return v;
}
well like the error says you cannot do this
View v = super.getView(position, convertView, parent);
instead you should be doing
View v = convertView;
then check if v is null
if(v == null){
//get the view
}
Maybe i didn't understand well your question but instead of this
item.status == "green"
Use this
item.status.equals("green")

Categories