Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make ListView in Android studio ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 289
    Comment on it

    Here I have create a ListView app in android. In the Listview numbers of items shown in vertical scrollable list. In Listview items are automatically inserted to the list and In ListView Adapter pulls content and image from a source database class, here I have described how to make ListView step by step.

    Step(1)-Create activity_main.xml layout.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <EditText
            android:id="@+id/txtItem"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="@string/hintTxtItem"
            />
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/lblBtnAdd"
            android:layout_toRightOf="@id/txtItem"
            />
        <TextView
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtItem"
            android:text="@string/txtEmpty"
            android:gravity="center_horizontal"
            />
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtItem"
            android:choiceMode="multipleChoice" >
        </ListView>
        <Button
            android:id="@+id/btnDel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/lblBtnDel" />
    </RelativeLayout>
    

    Step(2)-Add string name in string resources file.

    <string name="hintTxtItem">Enter an item here..</string>
    <string name="lblBtnAdd">Add Name</string>
    <string name="txtEmpty">List is empty</string>
    <string name="lblBtnDel">Delete Selected Name</string>
    

    Step(3)-Now In MainActivity add adapter,ArrayList.

    package com.tiwari.rajshekhar.countrylist;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.util.SparseBooleanArray;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import java.util.ArrayList;
    public class MainActivity extends ListActivity {
        /** Items entered by the user is stored in this ArrayList variable */
        ArrayList list = new ArrayList();
        /** Declaring an ArrayAdapter to set items to ListView */
        ArrayAdapter adapter;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            /** Setting a custom layout for the list activity */
            setContentView(R.layout.activity_main);
            /** Reference to the add button of the layout main.xml */
            Button btn = (Button) findViewById(R.id.btnAdd);
            /** Reference to the delete button of the layout main.xml */
            Button btnDel = (Button) findViewById(R.id.btnDel);
            /** Defining the ArrayAdapter to set items to ListView */
            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, list);
            /** Defining a click event listener for the button "Add" */
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText edit = (EditText) findViewById(R.id.txtItem);
                    list.add(edit.getText().toString());
                    edit.setText("");
                    adapter.notifyDataSetChanged();
                }
            };
            /** Defining a click event listener for the button "Delete" */
            View.OnClickListener listenerDel = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /** Getting the checked items from the listview */
                    SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                    int itemCount = getListView().getCount();
                    for(int i=itemCount-1; i >= 0; i--){
                        if(checkedItemPositions.get(i)){
                            adapter.remove(list.get(i));
                        }
                    }
                    checkedItemPositions.clear();
                    adapter.notifyDataSetChanged();
                }
            };
            /** Setting the event listener for the add button */
            btn.setOnClickListener(listener);
            /** Setting the event listener for the delete button */
            btnDel.setOnClickListener(listenerDel);
            /** Setting the adapter to the ListView */
            setListAdapter(adapter);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: