Thursday 3 March 2011

Hi Friend How are you ?

Today I Write about Custom ListView.
How to Create Custom List View in Android. and to how to use in your Application.

To Create Custom List View You must Create one another xml layout file so plz take one xml file in your Layout Folder.

and then add control which u want to display in Listview Like TextView, Button, Checkbox, Radio Button.

Here is Example of TextView and Button.

new layout File Coding look like this..






/************************* Main.Xml look like this ***************************/





*************************************************************************
you must add one Class file and add this code in your new classfile

package com.drc.myCustomListViewApp;

import java.util.zip.Inflater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class myCustomeListClass extends BaseAdapter {
LayoutInflater mLafla;
String[] mNarray;
Context con;
myCustomeListClass(Context context,String[] nm){
mLafla=LayoutInflater.from(context);
mNarray=nm;
con=context;
}
public int getCount() {
// TODO Auto-generated method stub
return mNarray.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
String temp=mNarray[position];
return temp;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=mLafla.inflate(R.layout.customlistview,null);
TextView mNm=(TextView)convertView.findViewById(R.id.txtFirst);
Button btnOk=(Button)convertView.findViewById(R.id.btnOk);
for(int i=0;i
if(position==i)
mNm.setText(mNarray[i]);
}

btnOk.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(con, "You Clicked "+getItem(position).toString(),Toast.LENGTH_SHORT).show();
}
});
}
return convertView;
}

}
********************************************************
and mainjava file code look like this...

package com.drc.myCustomListViewApp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class myCustomlistviewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lst1;
String[] name={"Hitesh","Piyush","Dipak","Sam","Rahul"};
lst1=(ListView)findViewById(R.id.lstCustome);
myCustomeListClass mCustomAdapter = new myCustomeListClass(this,name);
lst1.setAdapter(mCustomAdapter);
}
}







thanks you !!!

if Any Error then ask me...