can select single row for detail view onselectlist(view detail ) or can select multiple checkbox for action(delete)
Create custom row layout to inflate on activity (Checkbox TextView TextView TextView)
row_for_ordered_list.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >
<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginBottom=”10dp” >
<CheckBox
android:id=”@+id/OrdercheckBox1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:focusable=”false”
/>
<TextView
android:id=”@+id/onTXTV”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Order No”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:layout_marginLeft=”30dp”/>
<TextView
android:id=”@+id/dateTXTV”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Date”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:layout_marginLeft=”50dp”/>
<TextView
android:id=”@+id/pnameTXTV”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Party Name”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:layout_marginLeft=”40dp”/>
</LinearLayout>
</LinearLayout>
—————————————————————–
view_order_list1
<?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” >
<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:layout_marginLeft=”50dp”>
<TextView
android:id=”@+id/OrderNOtV1″
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:text=”OrderNO”
android:textAppearance=”?android:attr/textAppearanceLarge”
/>
<TextView
android:id=”@+id/datetV2″
android:layout_width=”90dp”
android:layout_height=”wrap_content”
android:text=” Date ”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:layout_marginLeft=”30dp”/>
<TextView
android:id=”@+id/partyNMtV3″
android:layout_width=”120dp”
android:layout_height=”wrap_content”
android:text=”Party Name”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:layout_marginLeft=”20dp”/>
</LinearLayout>
<LinearLayout
android:scrollbars=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”320dp”
android:layout_marginTop=”20dp”
>
<ListView
android:id=”@+id/OviewListView1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”
android:layout_marginLeft=”50dp”>
<Button
android:id=”@+id/backbtn1″
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:text=”Back” />
<Button
android:id=”@+id/ButtonDel”
android:layout_width=”100dp”
android:layout_height=”wrap_content”
android:text=”Delete”
android:layout_marginLeft=”20dp”/>
</LinearLayout>
</LinearLayout>
——————————————-
ViewPlaced_OrderList1 class
public class ViewPlaced_OrderList1 extends Activity
{
Button back,DeleteOrder;
ArrayList<String> ViewOList=new ArrayList<String>();
ArrayAdapter<String> adapter;
//String OrderDate,OrderNO,PartyName;
String TakeOno,TakePName;
ArrayList<GetterSetter> results = new ArrayList<GetterSetter>();
BaseAdapter_OrderedList base;
private SQLiteDatabase newDB;
DBHelper1 dbHelper;
String OrderNO,OmasterID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_order_list1);
back=(Button)findViewById(R.id.backbtn1);
DeleteOrder=(Button)findViewById(R.id.ButtonDel);
//OListaddTOListView();
final ArrayList<GetterSetter> searchResults = getGetterSetter();
final ListView lv = (ListView) findViewById(R.id.OviewListView1);
lv.setAdapter(new BaseAdapter_OrderedList(this, searchResults));
DeleteOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println(“DELETE: acc size: “+searchResults.size());
for (int i = 0; i < searchResults.size(); i++) {
if(searchResults.get(i).isSelected()){
System.out.println(“DELETE: pos: “+i);
// Toast.makeText(getApplicationContext(), “Selected Position No”+searchResults.get(i).getName(), Toast.LENGTH_LONG).show();
OrderNO=searchResults.get(i).getOrderNO();
System.out.println(“DELETE: N “+i);
dbHelper = new DBHelper1(getBaseContext());
newDB = dbHelper.getWritableDatabase();
//take out order_master_id for deleting rows from both table
{
Cursor c=newDB.rawQuery(“select order_master_id from order_master where order_number='”+OrderNO+”‘”, null);
if(c!=null)
{
if(c.moveToFirst())
{
do{
OmasterID=c.getString(c.getColumnIndex(“order_master_id”));
}while(c.moveToNext());
}
}
c.close();
//delete rows from order_detail table
Cursor c11=newDB.rawQuery(“delete from order_detail where order_master_id='”+OmasterID+”‘”, null);
if(c!=null)
{
if(c11.moveToFirst())
{
do{
}while(c11.moveToNext());
}
}
c11.close();
Toast.makeText(getApplicationContext(), “Items deleted”, Toast.LENGTH_LONG).show();
//delete rows from order_master table
Cursor c1=newDB.rawQuery(“delete from order_master where order_number='”+OrderNO+”‘”, null);
if(c!=null)
{
if(c1.moveToFirst())
{
do{
}while(c1.moveToNext());
}
}
c1.close();
Toast.makeText(getApplicationContext(), “Order deleted”, Toast.LENGTH_LONG).show();
}
newDB.close();
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
try{
Object o = lv.getItemAtPosition(position);
GetterSetter fullObject = (GetterSetter)o;
Toast.makeText(ViewPlaced_OrderList1.this, “You have chosen: ” + ” ” + fullObject.getOrderNO() + “:Phone:= ” + fullObject.getpname(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(ViewPlaced_OrderList1.this, “Not Click”+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
private ArrayList<GetterSetter> getGetterSetter() {
GetterSetter sr = new GetterSetter();
dbHelper = new DBHelper1(ViewPlaced_OrderList1.this);
newDB = dbHelper.getWritableDatabase();
Cursor c=newDB.rawQuery(“select om.order_number,om.order_date,pm.party_name from order_master om inner join party_master pm on pm.party_id=om.party_id “,null);
if (c != null ) {
if (c.moveToFirst()) {
do {
sr = new GetterSetter();
// a1= c.getString(c.getColumnIndex(“order_number”));
sr.setOrderNO(c.getString(c.getColumnIndex(“order_number”)));
// b1=c.getString(c.getColumnIndex(“order_date”));
sr.setdate(c.getString(c.getColumnIndex(“order_date”)));
// c1=c.getString(c.getColumnIndex(“order_master_id”));
sr.setpname(c.getString(c.getColumnIndex(“party_name”)));
//Toast.makeText(getApplicationContext(),a1.toString()+”:”+b1+”:”+c1, Toast.LENGTH_SHORT).show();
results.add(sr);
}while (c.moveToNext());
}
}
c.close();
newDB.close();
return results;
}
}
public class ViewPlaced_OrderList1 extends Activity
{
Button back,DeleteOrder;
ArrayList<String> ViewOList=new ArrayList<String>();
ArrayAdapter<String> adapter;
//String OrderDate,OrderNO,PartyName;
String TakeOno,TakePName;
ArrayList<GetterSetter> results = new ArrayList<GetterSetter>();
BaseAdapter_OrderedList base;
private SQLiteDatabase newDB;
DBHelper1 dbHelper;
String OrderNO,OmasterID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_order_list1);
back=(Button)findViewById(R.id.backbtn1);
DeleteOrder=(Button)findViewById(R.id.ButtonDel);
//OListaddTOListView();
final ArrayList<GetterSetter> searchResults = getGetterSetter();
final ListView lv = (ListView) findViewById(R.id.OviewListView1);
lv.setAdapter(new BaseAdapter_OrderedList(this, searchResults));
DeleteOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println(“DELETE: acc size: “+searchResults.size());
for (int i = 0; i < searchResults.size(); i++) {
if(searchResults.get(i).isSelected()){
System.out.println(“DELETE: pos: “+i);
// Toast.makeText(getApplicationContext(), “Selected Position No”+searchResults.get(i).getName(), Toast.LENGTH_LONG).show();
OrderNO=searchResults.get(i).getOrderNO();
System.out.println(“DELETE: N “+i);
dbHelper = new DBHelper1(getBaseContext());
newDB = dbHelper.getWritableDatabase();
//take out order_master_id for deleting rows from both table
{
Cursor c=newDB.rawQuery(“select order_master_id from order_master where order_number='”+OrderNO+”‘”, null);
if(c!=null)
{
if(c.moveToFirst())
{
do{
OmasterID=c.getString(c.getColumnIndex(“order_master_id”));
}while(c.moveToNext());
}
}
c.close();
//delete rows from order_detail table
Cursor c11=newDB.rawQuery(“delete from order_detail where order_master_id='”+OmasterID+”‘”, null);
if(c!=null)
{
if(c11.moveToFirst())
{
do{
}while(c11.moveToNext());
}
}
c11.close();
Toast.makeText(getApplicationContext(), “Items deleted”, Toast.LENGTH_LONG).show();
//delete rows from order_master table
Cursor c1=newDB.rawQuery(“delete from order_master where order_number='”+OrderNO+”‘”, null);
if(c!=null)
{
if(c1.moveToFirst())
{
do{
}while(c1.moveToNext());
}
}
c1.close();
Toast.makeText(getApplicationContext(), “Order deleted”, Toast.LENGTH_LONG).show();
}
newDB.close();
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
try{
Object o = lv.getItemAtPosition(position);
GetterSetter fullObject = (GetterSetter)o;
Toast.makeText(ViewPlaced_OrderList1.this, “You have chosen: ” + ” ” + fullObject.getOrderNO() + “:Phone:= ” + fullObject.getpname(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(ViewPlaced_OrderList1.this, “Not Click”+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
private ArrayList<GetterSetter> getGetterSetter() {
GetterSetter sr = new GetterSetter();
dbHelper = new DBHelper1(ViewPlaced_OrderList1.this);
newDB = dbHelper.getWritableDatabase();
Cursor c=newDB.rawQuery(“select om.order_number,om.order_date,pm.party_name from order_master om inner join party_master pm on pm.party_id=om.party_id “,null);
if (c != null ) {
if (c.moveToFirst()) {
do {
sr = new GetterSetter();
// a1= c.getString(c.getColumnIndex(“order_number”));
sr.setOrderNO(c.getString(c.getColumnIndex(“order_number”)));
// b1=c.getString(c.getColumnIndex(“order_date”));
sr.setdate(c.getString(c.getColumnIndex(“order_date”)));
// c1=c.getString(c.getColumnIndex(“order_master_id”));
sr.setpname(c.getString(c.getColumnIndex(“party_name”)));
//Toast.makeText(getApplicationContext(),a1.toString()+”:”+b1+”:”+c1, Toast.LENGTH_SHORT).show();
results.add(sr);
}while (c.moveToNext());
}
}
c.close();
newDB.close();
return results;
}
}
——————————————————–
an Baseadapter class to bind data with component
public class BaseAdapter_OrderedList extends BaseAdapter {
private static ArrayList<GetterSetter> searchArrayList;
private LayoutInflater mInflater;
public BaseAdapter_OrderedList(Context context, ArrayList<GetterSetter> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return searchArrayList.size();
}
@Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null)
{
convertView=mInflater.inflate(R.layout.row_for_ordered_list, null);
//convertView.setClickable(true);
holder =new ViewHolder();
holder.txtOrderNo=(TextView)convertView.findViewById(R.id.onTXTV);
holder.txtDate = (TextView) convertView
.findViewById(R.id.dateTXTV);
holder.txtPname = (TextView) convertView.findViewById(R.id.pnameTXTV);
holder.checkbox=(CheckBox)convertView.findViewById(R.id.OrdercheckBox1);
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition=(Integer) buttonView.getTag();
searchArrayList.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setTag(position);
holder.txtOrderNo.setText(searchArrayList.get(position).getOrderNO());
holder.txtDate.setText(searchArrayList.get(position)
.getdate());
holder.txtPname.setText(searchArrayList.get(position).getpname());
holder.checkbox.setChecked(searchArrayList.get(position).isSelected());
return convertView;
}
static class ViewHolder {
TextView txtOrderNo;
TextView txtDate;
TextView txtPname;
CheckBox checkbox;
}
}
————————————————————–
an Getter Setter Class for data
public class GetterSetter {
private String Ono = “”;
private String date = “”;
private String pname = “”;
private boolean selected;
public void setOrderNO(String Ono) {
this.Ono = Ono;
}
public String getOrderNO() {
return Ono;
}
public void setdate(String date) {
this.date = date;
}
public String getdate() {
return date;
}
public void setpname(String pname) {
this.pname = pname;
}
public String getpname() {
return pname;
}
public Boolean isSelected() {
return selected;
}
public void setSelected(Boolean selected) {
this.selected = selected;
}
}