Wednesday 18 October 2017

8. Using ListView and Adapter in Android Application

8. Using ListView and Adapter in Android Application


Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.

ADAPTER:  bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can takes the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.

The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.

ListView Attributes


1. id: id is used to uniquely identify a ListView.
2. divider: This is a drawable or color to draw between different list items.
3. dividerHeight: This specify the height of the divider between list items.
4. listSelector: listSelector property is used to set the selector of the listView.


Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/mainActivityLinearlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="tutorial.android.sachin4droid.androidtutorials.MainActivity">

  <ListView
      android:id="@+id/mainActivityListView"
      android:layout_width="match_parent"
      android:layout_height="400dp">

  </ListView>

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Main Activity"
      android:textColor="@color/colorPrimaryDark"
      android:textSize="30dp"
      android:textStyle="bold"
      android:id="@+id/textView" />

  <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Click Me !!!" />


</LinearLayout>

MainActivity:

package tutorial.android.sachin4droid.androidtutorials;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  private static String TAG = "MainActivity";
  Button button;
  ListView mainActivityListView;
  List<String> activityList = Arrays.asList(new String[] {"ActivityOne","AnotherActivity" });


  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.i(TAG,"onCreate called now!!!");
      mainActivityListView = (ListView) findViewById(R.id.mainActivityListView);
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,activityList );
      mainActivityListView.setAdapter(adapter);
      mainActivityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              Toast.makeText(MainActivity.this, "you have clicked "+activityList.get(i), Toast.LENGTH_SHORT).show();

          }
      });



      button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
              intent.putExtra("myname","sachin123");
              startActivity(intent);


          }
      });

  }

  @Override
  protected void onStart() {
      super.onStart();
      Log.i(TAG,"onStart called now!!!");
  }

  @Override
  protected void onResume() {
      super.onResume();
      Log.i(TAG,"onResume called now!!!");
  }

  @Override
  protected void onPause() {
      super.onPause();
      Log.i(TAG,"onPause called now!!!");
  }

  @Override
  protected void onStop() {
      super.onStop();
      Log.i(TAG,"onStop called now!!!");
  }

  @Override
  protected void onDestroy() {
      super.onDestroy();
      Log.i(TAG,"onDestroy called now!!!");
  }
}


No comments:

Post a Comment

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...