Listview in android studio java

47 views Asked by At

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;

    }
}

`enter image description here

1

There are 1 answers

0
AudioBubble On

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.

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //remove the line below
        //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 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.