0

I have a custom Adapter called MyAdapter where i'm trying to load a layout which supposedly should be inserted into a listview, but even when Android is not giving errors the view is showing in blank.

In the next screenshot, i show what i get after build my app.

enter image description here

What i'm expecting to load is my layout into my listview:

ListView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.NewsFragment">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</FrameLayout>

enter image description here

Layout

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/list_item"
    android:layout_height="80dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageabdcf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="25dp"
        tools:srcCompat="@android:drawable/btn_star_big_on" />

    <TextView
        android:id="@+id/textabcdf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

NewsFragment.java

public class NewsFragment extends Fragment {

    private ListView listView;
    private List<String> names;
    public NewsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_news, container, false);

        listView = (ListView) v.findViewById(R.id.listView);

        names = new ArrayList<String>();
        names.add("Fernando");
        names.add("Roberto");
        names.add("Torres");
        names.add("Urban");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
            }
        });

        MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
        listView.setAdapter(myAdapter);

        // Inflate the layout for this fragment
        return v;
    }

}

MyAdapter.java

public class MyAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private List<String> names;

    public MyAdapter(Context context, int layout, List<String> names) {
        this.context = context;
        this.layout = layout;
        this.names = names;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return this.names.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        View v = convertView;
        LayoutInflater layoutInflater = LayoutInflater.from(this.context);
        v = layoutInflater.inflate(R.layout.fragment_news, null);
        String currentName = names.get(position);
        //currentName = (String) getItem(position);


        TextView textView = (TextView) v.findViewById(R.id.textabcdf);
        textView.setText(currentName);

        return v;
    }
}

And what i'm expecting for is something like:

enter image description here

Fernando Torres
  • 460
  • 7
  • 24
  • You're returning `0` from `MyAdapter`'s `getCount()` method. You need to return the size of your `List` there; i.e., `return names.size();`. Btw, that `ArrayAdapter` setup is essentially unused, as you're setting a `MyAdapter` instance on the `ListView` afterward. Not sure if that's just left over from testing. – Mike M. Dec 17 '18 at 03:37
  • That solution only made my application now close on its own and is not solving my problem. – Fernando Torres Dec 17 '18 at 03:42
  • Then you've got an issue elsewhere, as well. Look at [the stack trace](https://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Dec 17 '18 at 03:43
  • Actually, I see the likely culprit. You're inflating the wrong layout in `getView()`. You need to inflate the list item layout, not the `Fragment`'s layout. – Mike M. Dec 17 '18 at 03:45
  • Ok, thanks, @Mike M., but why you marked my question as duplicated? As far as i know you or SO needs to provide me the duplicate question which will solving my issue. – Fernando Torres Dec 17 '18 at 03:47
  • 1
    Oh, I'm sorry, i saw it already at the top of the question. My best regards. – Fernando Torres Dec 17 '18 at 03:47

0 Answers0