Sunday, 13 August 2017

ListView || Simple ListView || Static ListView

ListView :

It is one of the very useful tool for Android Application.

What is ListView :

  • Fetching data from database and display it on activity using custom designed view.
  • Same view which is repeating, for that we can use ListView.
How to create ListView in Android:
  • First you need one main activity. There you should define <ListView /> tag in .xml file
  • Then only MainActivity.java is need to be coded. 
 Example 
  • Show all days using ListView 
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.tricktechs.listview.MainActivity"> 

    <ListView         android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/lv_lst" >
    </ListView>

</RelativeLayout>

MainActivity.java

package com.tricktechs.listview;

import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.ArraySet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {



    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] name = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        ListView ls = (ListView) findViewById(R.id.lv_lst);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
        ls.setAdapter(adapter);



    }


}



1 comment: