Friday 6 March 2015

Custom listview

1)MainActivity.java
===============
package com.example.multiselectdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
private ListView lstProfile;
private MainActivityAdapter mActivityAdapter;
private ArrayList<ProfileModel> alstProfile;
ProfileModel profileModel1,profileModel2,profileModel3,profileModel4;
private Button btnShow;
String str="";

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

alstProfile = new ArrayList<ProfileModel>();
btnShow = (Button)findViewById(R.id.btnShow);
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < alstProfile.size(); i++) {
int size = alstProfile.size();
if(alstProfile.get(i).isSelected()){
Log.e("", "size="+size);
Log.e("","i==>>"+(i-1));
if(size == i+1){
str = str+alstProfile.get(i).getName().toString();
}else{
str = str+alstProfile.get(i).getName().toString()+"\n";
}
//Toast.makeText(MainActivity.this, ""+alstProfile.get(i).getName(),Toast.LENGTH_LONG).show();
}
}
//Toast.makeText(MainActivity.this, "",Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, ""+str.toString(),Toast.LENGTH_LONG).show();
str="";
}
});

profileModel1 = new ProfileModel();
profileModel1.setName("Manisha");
profileModel1.setCity("Ahmedabad");
profileModel1.setSelected(false);
alstProfile.add(profileModel1);

profileModel2 = new ProfileModel();
profileModel2.setName("Khyati");
profileModel2.setCity("Rajkot");
   profileModel2.setSelected(false);
alstProfile.add(profileModel2);

profileModel3 = new ProfileModel();
profileModel3.setName("Khyati");
profileModel3.setCity("Delhi");
profileModel3.setSelected(false);
alstProfile.add(profileModel3);

profileModel4 = new ProfileModel();
profileModel4.setName("Hrishik");
profileModel4.setCity("Mumbai");
profileModel4.setSelected(false);
alstProfile.add(profileModel4);

lstProfile = (ListView)findViewById(R.id.lstProfile);
mActivityAdapter = new MainActivityAdapter(this,alstProfile);
lstProfile.setAdapter(mActivityAdapter);
lstProfile.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(alstProfile.get(arg2).isSelected()){
alstProfile.get(arg2).setSelected(false);
}else{
alstProfile.get(arg2).setSelected(true);
}
Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
mActivityAdapter.notifyDataSetChanged();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

2) MainActivityAdapter.java
=====================
package com.example.multiselectdemo;

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivityAdapter extends BaseAdapter{
public Context mContext;
public ArrayList<ProfileModel>alstProfile;
private LayoutInflater mInflater;

public MainActivityAdapter(Context mContext,ArrayList<ProfileModel>alstProfile){
this.mContext = mContext;
this.alstProfile = alstProfile;
this.mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return alstProfile.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.mainactivity_adapter, null);
LinearLayout lyt = (LinearLayout)convertView.findViewById(R.id.lytList);
TextView txtName = (TextView)convertView.findViewById(R.id.txtName);
Log.e("",""+alstProfile.get(position).getName());
txtName.setText(alstProfile.get(position).getName());
TextView txtCity = (TextView)convertView.findViewById(R.id.txtCity);
txtCity.setText(alstProfile.get(position).getCity());
if(alstProfile.get(position).isSelected()){
lyt.setBackgroundResource(R.color.gray);
}else{
lyt.setBackgroundResource(R.color.Red);
}
return convertView;
}

}

3)ProfileModel.java
===============
package com.example.multiselectdemo;

public class ProfileModel {
private String name;
private String city;
private boolean selected;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}

}

4) activity_main.xml
===============
<RelativeLayout 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"
    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=".MainActivity" >

   <ListView 
       android:id="@+id/lstProfile"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       ></ListView>
   
   <Button 
       android:id="@+id/btnShow"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:text="Show"/>
</RelativeLayout>

5) mainactivity_adapter.xml
=====================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lytList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Name" />

    <TextView
        android:id="@+id/txtCity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="City" />

</LinearLayout>

No comments:

Post a Comment