Skip to content Skip to sidebar Skip to footer

How Can I Make A ListView In Android Studio?

I'm trying to make a ListView for my Android project but unfortunately when I import these libraries: import android.app.ListActivity; import android.os.Bundle; import android.wid

Solution 1:

CustomAdapter class :-

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


public class CustomAdapter extends BaseAdapter
{

    Context context;
    String[] rooms;
    public CustomAdapter(Context c)
    {
        context = c;
        Resources res = c.getResources();
        rooms = res.getStringArray(R.array.images);
    }
    @Override
    public int getCount() {
        return rooms.length;
    }

    @Override
    public Object getItem(int position) {
        return rooms[position];
    }

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

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

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view =  inflater.inflate(R.layout.single_row,parent,false);
        TextView txt = (TextView) view.findViewById(R.id.txt_item);
        String temp = rooms[position];
        txt.setText(temp);
        return view;
    }
}

MainActivity class :-

public class MainActivity extends ActionBarActivity
{


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomAdapter(this));
     }

}

Add this to your string.xml :-

<string-array name="images">
        <item>Living Room</item>
        <item>Master Bedroom</item>
        <item>Children Bedroom</item>
        <item>Kitchen1</item>
        <item>Kitchen2</item>
        <item>Gallery</item>
</string-array>

Add this to your activity_main.xml :-

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="0.1dp"
    android:divider="#666666"
    android:id="@+id/listView" />

single_row.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:gravity="start"
        android:text="Large Text"
        android:typeface="serif"
        android:id="@+id/txt_item"
        android:padding="3dp"
        android:paddingLeft="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Post a Comment for "How Can I Make A ListView In Android Studio?"