Hello friends Today I will write a code for Multiple List View .
Please Proceed following steps :
STEP 1 : CREATE A PROJECT MultipleListView
STEP 2 : PUT FOLLOWING CODE IN MainActivity
package com.ritesh.multiplelistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
private ArrayAdapter<String> adaptern;
private String[] namelist = new String[] {"android","aman","bharat","banana","calc","come","dell","dhoom","ele",
"fork","fail","goyal","home","imagine","jama","kaman","lawan","mohit","mohan","noida","oman","police","qatar","rest",
"satya","truth","ugly","volume","work","wow","xmas","yeah","yes","zebra"};
private String[] name = new String[] {"#","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z"};
private ListView listmain;
private ArrayList<String> list_name=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listside=(ListView)findViewById(R.id.listviewside);
listmain=(ListView)findViewById(R.id.listViewmain);
for (int i = 0; i < namelist.length; i++) {
list_name.add(namelist[i]);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, name);
listside.setAdapter(adapter);
adaptern=new ArrayAdapter<String>(MainActivity.this, R.layout.itemtext, list_name);
listmain.setAdapter(adaptern);
listside.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
list_name.clear();
if ((name[arg2]).equals("#")) {
for (int i = 0; i < namelist.length; i++) {
list_name.add(namelist[i]);
}
}
else {
for (int i = 0; i < namelist.length; i++) {
if((namelist[i].substring(0, 1)).equals(name[arg2])){
list_name.add(namelist[i]);
}
}
}
adaptern.notifyDataSetChanged();
}
}
Please Proceed following steps :
STEP 1 : CREATE A PROJECT MultipleListView
STEP 2 : PUT FOLLOWING CODE IN MainActivity
package com.ritesh.multiplelistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
private ArrayAdapter<String> adaptern;
private String[] namelist = new String[] {"android","aman","bharat","banana","calc","come","dell","dhoom","ele",
"fork","fail","goyal","home","imagine","jama","kaman","lawan","mohit","mohan","noida","oman","police","qatar","rest",
"satya","truth","ugly","volume","work","wow","xmas","yeah","yes","zebra"};
private String[] name = new String[] {"#","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z"};
private ListView listmain;
private ArrayList<String> list_name=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listside=(ListView)findViewById(R.id.listviewside);
listmain=(ListView)findViewById(R.id.listViewmain);
for (int i = 0; i < namelist.length; i++) {
list_name.add(namelist[i]);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, name);
listside.setAdapter(adapter);
adaptern=new ArrayAdapter<String>(MainActivity.this, R.layout.itemtext, list_name);
listmain.setAdapter(adaptern);
listside.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
list_name.clear();
if ((name[arg2]).equals("#")) {
for (int i = 0; i < namelist.length; i++) {
list_name.add(namelist[i]);
}
}
else {
for (int i = 0; i < namelist.length; i++) {
if((namelist[i].substring(0, 1)).equals(name[arg2])){
list_name.add(namelist[i]);
}
}
}
adaptern.notifyDataSetChanged();
}
}
STEP 3 : PUT THIS CODE IN activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android1:id="@+id/listViewmain"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:layout_alignParentLeft="true"
android1:layout_alignParentTop="true"
android1:layout_toLeftOf="@+id/listviewside" >
</ListView>
<ListView
android1:id="@+id/listviewside"
android1:layout_width="40dp"
android1:layout_height="wrap_content"
android1:layout_alignParentRight="true"
android1:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
STEP 4 : PUT THIS CODE IN itemtext.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TextView"
android:textColor="#00FF00"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:background="@android:color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
STEP 5 : PUT THIS CODE IN manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ritesh.multiplelistview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ritesh.multiplelistview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment