2

I am using Realm Database to store and retrieve data. Data are inserted successfully. I am using Custom list view which has two text views. I am trying to fetch data from DB and trying to display it inside a fragment with the custom view I defined. But I could see only empty list view on the screen when called.

My Custom View XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:id="@+id/Text1"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="25sp"
android:layout_marginLeft="25dp"
android:id="@+id/Text2"/>
 </LinearLayout>

Custom View Adapter

public class CustomView extends BaseAdapter {

String [] field1;
String [] filed2;
Context context;
private static LayoutInflater inflater=null;


public CustomView()
{

}

public CustomView(Context context,String[] one, String[] two)
{
    this.context=context;
    field1=one;
    filed2=two;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

public class Holder
{
    TextView tv;
    TextView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.customlist, null);
    holder.tv=(TextView) rowView.findViewById(R.id.Text1);
    holder.img=(TextView) rowView.findViewById(R.id.Text2);
    holder.tv.setText(field1[position]);
    holder.img.setText(filed2[position]);


    return rowView;
}
}

Fragment class from which i am calling the above

public void check()
{
    realm=Realm.getDefaultInstance();
    RealmResults<DataBase> realmResults=realm.where(DataBase.class).findAll();
    for(DataBase db:realmResults)
    {
         strings.add(db.getName());
         two.add(db.getAge());
    }

    String[] name= new String[strings.size()];
    name=strings.toArray(name);
    String[] name1= new String[two.size()];
    name1=two.toArray(name1);

    listView.setBackgroundColor(Color.WHITE);
    listView.setAdapter(new CustomView(getActivity(),name,name1));



}

What I am missing here?

Sriram S
  • 169
  • 14

0 Answers0