Thursday, 1 October 2015

CustomList View in Android

activity_main.xml
---------------------------------
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
ListView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/listView"
       
android:divider="#5c5aff"
       
android:dividerHeight="5dp" />
</
LinearLayout>

data.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical">

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Large Text"
       
android:id="@+id/textView2" />
</
LinearLayout>
MainActivity.Java
-----------------------------
package com.example.kishore.customlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ListView
listView;
    String
names[]={"Name1","Name2","Name3","Name4","Name5","Name6","Name7","Name8","Name9","Name10"};
    String
loc[]={"Location1","Location2","Location3","Location4","Location5","Location6","Location7","Location8","Location9","Location10"};
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        
listView=(ListView)findViewById(R.id.listView);
        Myadapter m1=
new Myadapter();
       
listView.setAdapter(m1);
    }
   
class Myadapter extends BaseAdapter{
       
@Override
       
public int getCount() {
           
return names.length;
        }

       
@Override
       
public Object getItem(int position) {
           
return null;
        }

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

       
@Override
       
public View getView(int position, View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(R.layout.
data,null);
            TextView t1=(TextView)convertView.findViewById(R.id.
textView);
            TextView t2=(TextView)convertView.findViewById(R.id.
textView2);
            t1.setText(
names[position]);
            t2.setText(
loc[position]);
           
return convertView;
        }
    }


}


Output is:
-------------


No comments:

Post a Comment