Beginner Beginner - 6 months ago 30
Java Question

How to put data into ListView of XML?

I just started learning Android and I am trying to create a simple view where a portion of the main XML is a listview.

Here is my main.xml

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


<RelativeLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="@+id/Menu">

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/menu_item_1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="129dp" />

</RelativeLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_toEndOf="@+id/Menu"
android:layout_toRightOf="@+id/Menu"
android:layout_alignParentTop="true"
android:id="@+id/Banner">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Account Log"
android:layout_gravity="center"
android:inputType="none"
android:textStyle="bold"/>
</FrameLayout>

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Log"
android:layout_toRightOf="@+id/Menu"
android:layout_below="@+id/Banner"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:choiceMode="none"
android:clickable="false"
android:contextClickable="true"
android:fadeScrollbars="true" />

</RelativeLayout>


Assuming that I have a function called
getDatafromFile()
which returns an ArrayList of data, how do I put those data into my listView?

I've heard of using ArrayAdapter but the examples I've seen puts ListView to the entire layout.

Answer

The best way to put data in your ListView is by making an ArrayAdapter Class. I always recommend you to try using a custom adapter class so that you can change it as per your requirement. You can create a class that extends ArrayAdapter. Then pass the arraylist that you want inside your list view to that adapter constructor. For Example, below is a custom adapter class

public class CustomAdapter extends ArrayAdapter<DataInfo> {


ArrayList<DataInfo> itemList =new ArrayList<>();
Context context;
LayoutInflater inflater;
public SaleAdapter(Context context, ArrayList<VegInfo> list) {
    super(context, R.layout.list_item, list);
    this.context = context;
    itemList = list;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //getting an item from the list
    DataInfo item = itemList.get(position);
    if(convertView==null)
        convertView = inflater.inflate(R.layout.list_item,parent,false);

        //displaying some data from your data info in a textview
    TextView textView = (TextView) convertView.findViewById(R.id.tv);
    textView.setText(item.subitem);


    return convertView;
}
}

Inside your Activity, You can set the data to that adapter.

 CustomAdapter yourAdapter = new CustomAdapte(activity,yourArrayList);
 yourListView.setAdapter(yourAdapter);

Now, your list view have the data inside the array list. Whenever there is a change in the array list, it can be reflected in the list view just by calling

yourAdapter.notifydatasetchanged();

Here list_item is a custom XML file that you should create for a single list item view.

Hope this helps you. If not please tell me and let me make it more clear. happy coding.

Comments