View v is unreachable statement - java

Please help me.
Is this an error or a warning?
How can I solve this?
Error is that the View v is unreachable statement
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return super.getView(position, convertView, parent);
View v;
v = convertView;
LayoutInflater li = LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.custom_layout, null);
TextView champ = (TextView)v.findViewById(R.id.textView);
TextView year = (TextView)v.findViewById(R.id.textView2);
ImageView pic = (ImageView)v.findViewById(R.id.imageView);
champ.setText(adaptArray.get(position).getChamp()); year.setText(adaptArray.get(position).getYear()); pic.setImageResource(adaptArray.get(position).getSport());
return v;
}

Anything else is written after the return keyword it's unreachable.
Remove return super.getView(position, convertView, parent); from the first line of your function.

This is a warning, telling you that static analysis of the code shows that some of your code will never be reached, because the method returns before.
The following is how you can resolve the issue and make proper use of the convertView by passing it to super.getView(...):
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if(v == null){
LayoutInflater li = LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.custom_layout, null);
}
TextView champ = (TextView)v.findViewById(R.id.textView);
TextView year = (TextView)v.findViewById(R.id.textView2);
ImageView pic = (ImageView)v.findViewById(R.id.imageView);
champ.setText(adaptArray.get(position).getChamp());
year.setText(adaptArray.get(position).getYear());
pic.setImageResource(adaptArray.get(position).getSport());
return v;
}

Remove this
return super.getView(position, convertView, parent);

It's because of this line of code at the Top of the method:
return super.getView(position, convertView, parent);
When you return from a method, the rest of the code inside the method doesn't run.

Related

ListView specific item color

I list site addresses with Android listview.I tried for hours, but I couldn't find a solution.
for example
http://site .com
http://site2 .com
http://site3 .com
http://site4 .com
if have http://site3.com in listView i want to change the color of this line.
final ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
resultsdatalist,
R.layout.list_item,
new String[]{"resultid", "result"},
new int[]{R.id.resultid, R.id.result}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
};
lv.setAdapter(adapter);
Try this:
#Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if(getItem(position).equals("site3.com"))
{
// do something change color
view.setBackgroundColor (Color.RED); // some color
}
else
{
// default state
view.setBackgroundColor (Color.WHITE); // default coloe
}
return view;
}

ArrayAdapter for country flag and image spinner slowing view down

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;
}
}

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);
}

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 ?

Categories