Bucky Bucky - 5 months ago 57
Android Question

Why List View is not showing in the fragment?

I am not familiar with how to add items to listview of a fragment , But i tried. It's not showing any error . But It's not showing anything on the application too.
well i learnt the steps for doing it from youtube . It also worked perfectly in main activity class but in fragment class it's not working.
So HELP.

Item list view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical"
>

<LinearLayout
android:id="@+id/lyt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="1px"
android:gravity="left|top"
android:padding="2dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:textStyle="bold"/>

<TextView
android:gravity="left|top"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text=":" />

<TextView
android:textStyle="bold"
android:gravity="left|top"
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content"
android:textColor="@color/darker_grey"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"/>
</LinearLayout>

</LinearLayout>


ussdz.java
:

package com.buckydroid.app.droidcpu;

/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdz
{
private int id;
private String USSD;
private String Description;

public ussdz(String description, String s) {
Description = description;
this.USSD = USSD;
this.id = id;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUSSD() {
return USSD;
}

public void setUSSD(String USSD) {
this.USSD = USSD;
}

public String getDescription() {
return Description;
}

public void setDescription(String description) {
Description = description;
}
}


ussdadaper.java
:

package com.buckydroid.app.droidcpu;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;


/**
* Created by BuckyDroid on 14-06-2016.
*/
public class ussdadaper extends BaseAdapter {
private Context ussds;
private List<ussdz> ussdlist;

public ussdadaper(Context ussds, List<ussdz> ussdlist) {
this.ussds = ussds;
this.ussdlist = ussdlist;
}

@Override
public int getCount() {
return ussdlist.size();

}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v= View.inflate(ussds,R.layout.item_listview,null);
TextView ussdcode = (TextView)v.findViewById(R.id.textView1);
TextView ussddesc = (TextView)v.findViewById(R.id.textView2);
ussdcode.setText(ussdlist.get(position).getUSSD());
ussddesc.setText(ussdlist.get(position).getDescription());
return v;


}
}


ussd.java
(the Fragment):

package layout;

import android.app.Activity;
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.ListView;

import com.buckydroid.app.droidcpu.R;
import com.buckydroid.app.droidcpu.ussdadaper;
import com.buckydroid.app.droidcpu.ussdz;
import com.google.android.gms.tagmanager.Container;

import java.util.ArrayList;
import java.util.List;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link ussd.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link ussd#newInstance} factory method to
* create an instance of this fragment.
*/
public class ussd extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private ListView listView;
private ussdadaper ussd;
private List<ussdz> ussdlist;

private OnFragmentInteractionListener mListener;

public ussd() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ussd.
*/
// TODO: Rename and change types and number of parameters
public static ussd newInstance(String param1, String param2) {
ussd fragment = new ussd();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

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


listView = (ListView)container.findViewById(R.id.listView);
ussdlist = new ArrayList<>();
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussdlist.add(new ussdz("*#44336#","Software Version Info"));
ussd = new ussdadaper(getActivity().getApplicationContext(),ussdlist);


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

// TODO: Rename method, update argument and hook method into UI event

@Override
public void onAttach(Context context) {
super.onAttach(context);
}

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

public interface OnFragmentInteractionListener {
public void onFragmentInteractionHome(Uri uri);
public void openHome(View view);
}
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/

Answer

You are not setting the adapter to the listview Change you onCreateView like this

View rootView = inflater.inflate(R.layout.fragment_offer_detail_item, container, false);

 listView = (ListView)view.findViewById(R.id.listView);
    ussdlist = new ArrayList<>();
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussdlist.add(new ussdz("*#44336#","Software Version Info"));
    ussd = new ussdadaper(getActivity(),ussdlist);
    listView .setAdapter(ussd);   //you should setthe adapter to the listview
        return rootView;

Also your adapter class is not following a viewholder pattern which is error prone it would be better you change it to viewHolder pattern

//Edit: You can easily create Adapter class by pasting the list_item xml in Layout Finder. You could make a try its simple and saves you lot of time

Comments