Search code examples
listviewandroid-listfragment

implementing listview in fragment


I am trying to create a list of items with that row layout in listView and those are located in fragment but each time i run the code i get nothing in my listview

rowlist_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_marginBottom="1dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/listIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/no_image"
        android:padding="5dp"/>
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_margin="16dp"
        android:textSize="16sp"
        android:textStyle="bold"
        />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginLeft="10dp"/>
        </LinearLayout>
</LinearLayout>

fragment_display_lists.xml

 <?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="layout.Display_lists"
    android:layout_marginTop="10dp">





        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>



</FrameLayout>

my ListAdapter.java

package layout;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class ListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] itemname;
    private final Integer[] imgid;

    public ListAdapter (Activity context,String[] itemname, Integer[] imgid){
        super(context, R.layout.fragment_display_lists,itemname);
        this.context=context;
        this.itemname=itemname;
        this.imgid=imgid;

    }
    public View getView (int position,View view,ViewGroup parent)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.rowlist_layout,null,true);
        TextView title = (TextView) rowView.findViewById(R.id.list_item);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.listIcon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView);

        title.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description "+itemname[position]);
        return rowView;

    };


    }

Display_lists.java

package layout;


import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;
import android.widget.Toast;




public class Display_lists extends Fragment{

    ListView list;
    String[] itemname ={
            "Item 1",
            "item 2",
            "item 3",
            "item 4",
            "item 5",

    };
    Integer[] imgid= {
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
    };


    private OnFragmentInteractionListener mListener;

    public Display_lists() {
        // Required empty public constructor

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        ListAdapter adapter=new ListAdapter(getActivity(),itemname, imgid);
        list=(ListView) getActivity().findViewById (R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String Slecteditem= itemname[+position];
                Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

            }
        });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_display_lists, container, false);
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

i am still new in android help :/


Solution

  • First of all your getView method doesn't implemented correctly see this

    second, your TextView id that defiend in rowlist_layout.xml is textView1 not textView,correct that

    third put breakpoint in your getView method and check if your arrays have values or not?

    and if you still stuck, see this and do step by step