I'm trying to implement a listview but i had an error in the Adapter Class on
View view = LayoutInflater.from(context).inflate(R.layout.agences_items,parent,false);
when compiling and the error says error: 'unreachable statement' i don't really understand the problem.
i have made sure that the library of view.layoutInflater but the error is still persisting
`
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class MyAdapter extends ArrayAdapter {
List<String> listTitle;
List<Integer> imagelist;
Context context;
public MyAdapter(@NonNull Context context, List<String> title,List<Integer> image) {
super(context, R.layout.agences_items,title);
this.listTitle = title;
this.imagelist = image;
this.context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return super.getView(position, convertView, parent);
View view = LayoutInflater.from(context).inflate(R.layout.agences_items,parent,false);
ImageView imageView = view.findViewById(R.id.imgview);
TextView textView = view.findViewById(R.id.txtview);
textView.setText(listTitle.get(position));
imageView.setImageResource(imagelist.get(position));
return view;
}
}
In your
getView(...)method your are returning a value before the code to setup your List View is executed. Remove it and your List View should get inflated as expected.In most programming languages, if the compiler encounters a return statement, the rest of the code below that statement is usually not reached as a value is returned to the calling function and the code block is exited.