Scrollabale listview in both direction with header using Horizontal scrollview

In this code i have used two linear layout in Horizontal Scrollview.=>[

first Outerlinear layout(orientation:vertical)  contain==>[ inner linear layout(orientation:horizontal) which includes textview’s which work as header and scroll with listview.] that listview is in outer layout after closing inner layout.]

 

<HorizontalScrollView
            android:layout_width=”wrap_content”
            android:layout_height=”wrap_content”
             android:layout_alignParentLeft = “true”
        android:layout_alignParentRight = “true”>

            <LinearLayout
                android:layout_width=”match_parent”
                android:layout_height=”wrap_content”
                 android:orientation=”vertical” >
        <LinearLayout
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
            android:orientation=”horizontal”   >
                <TextView
                    android:id=”@+id/textView7″
                    android:layout_width=”85dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”5dp”
                    android:text=”Deal_NO”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView8″
                    android:layout_width=”190dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”15dp”
                    android:text=”Buyer_Party_Name”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView9″
                    android:layout_width=”190dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”15dp”
                    android:text=”Seller_Party_Name”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView09″
                    android:layout_width=”130dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”11dp”
                    android:text=”Item_Name”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView10″
                    android:layout_width=”135dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”8dp”
                    android:text=”Deal_Amount”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView12″
                    android:layout_width=”125dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”5dp”
                    android:text=”Deal_Date”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView014″
                    android:layout_width=”135dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”10dp”
                    android:text=”Loading_Date”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />

                <TextView
                    android:id=”@+id/textView14″
                    android:layout_width=”160dp”
                    android:layout_height=”wrap_content”
                    android:layout_marginLeft=”10dp”
                    android:text=”Unloading_Date”
                    android:textAppearance=”?android:attr/textAppearanceLarge” />
            </LinearLayout>
            
       

        <View
            android:layout_width=”fill_parent”
            android:layout_height=”3dp”
            android:layout_marginBottom=”5dp”
            android:layout_marginTop=”5dp”
            android:background=”@android:color/black” />

        <ListView
            android:id=”@+id/OviewListView1″
            android:layout_width=”fill_parent”
            android:layout_height=”200dp” >
        </ListView>
        </LinearLayout>
         </HorizontalScrollView>

Listview with Scrollable header

As per scrolling list header will be  scroll with appropriate  component.

you need two layout:

suppose one is main.xml  which contain listview.

& another lvheader.xml which containg the textview in horizontal layout

Now we will add the lvheader content as listview header of main.xml listview.

and add the given code to activity  in which we are using

setcontentview(main.xml)

==Given code>

ListView lv = (ListView)findViewById(R.id.listvw);

    LayoutInflater inflater = getLayoutInflater(); 
    ViewGroup header = (ViewGroup)inflater.inflate(R.layout.lvheader, lv, false); 
    lv.addHeaderView(header, null, false);

CustomListview with checkbox.

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&#8221;
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&#8221;
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;
}

}