Wednesday, November 18, 2015

ANDROID SELECT QUERY AND BINDING WITH TABLE LAYOUT



REDIRECTING TO ANOTHER ACTIVITY:
    public void viewall(View v)
    {
       try
       {
       showdisplaysmall("Redirecting...");
       Intent in=new Intent(this,ViewActivity.class);
       startActivity(in);
       }
       catch(Exception ex)
       {
              showdisplaylong(ex.getMessage());
       }
    }
BINDING SQLITE DATA TO TABLE LAYOUT:

public class ViewActivity extends Activity {

 TableLayout table_layout;
 EditText rowno_et, colno_et;
 Button build_btn;
 List<String> ids=new ArrayList<String>();
 List<String> fnames=new ArrayList<String>();
 List<String> snames=new ArrayList<String>();

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

  table_layout = (TableLayout) findViewById(R.id.tableLayout1);
  table_layout.removeAllViews();
  FetchData();


 }
private void FetchData()
{
       try
       {
       String id,fname,sname;
       String query="select * from table1";
              Cursor c = MainActivity.db.rawQuery(query,null);
      
          if(c.moveToFirst()){
                 while(c.moveToNext()){
             
                 TableRow row = new TableRow(this);
                   row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                     LayoutParams.WRAP_CONTENT));
             
                   for (int j =0; j <= 2; j++) {

                    TextView tv = new TextView(this);
                    tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                      LayoutParams.WRAP_CONTENT));
                    tv.setBackgroundResource(R.drawable.cell_shape);
                    tv.setPadding(5, 5, 5, 5);
                    if(j==0)
                    {
                    tv.setText(c.getString(0));
                    }
                    if(j==1)
                    {
                       tv.setText(c.getString(1));
                    }
                    if(j==2)
                    {
                       tv.setText(c.getString(2));
                    }
                  

                    row.addView(tv);
                    
                    
              }
                   table_layout.addView(row);
                  
                    
                    

           }
      
 
      

          }
          }
       catch(Exception ex)
       {
              showdisplaylong("ERROR.."+ex.getMessage());
       }

}
public void showdisplaylong(String val)
 {
       Toast.makeText(this, val, Toast.LENGTH_LONG).show();
      
      
 }
 public void showdisplaysmall(String val)
 {
       Toast.makeText(this, val, Toast.LENGTH_SHORT).show();
      
      
 }

DYNAMIC TABLE CREATION FUNCTION IN ANDROID:

private void BuildTable(int rows, int cols) {

  // outer for loop
  for (int i = 0; i < ids.size(); i++) {

   TableRow row = new TableRow(this);
   row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
     LayoutParams.WRAP_CONTENT));

   // inner for loop
   for (int j = 0; j < 3; j++) {

    TextView tv = new TextView(this);
    tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
      LayoutParams.WRAP_CONTENT));
    tv.setBackgroundResource(R.drawable.cell_shape);
    tv.setPadding(5, 5, 5, 5);
    tv.setText("R " + i + ", C" + j);

    row.addView(tv);

   }
  

   table_layout.addView(row);

  }
 }

DRAWABLE RESOURCE FOR CELL SHAPE:
Cell_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape= "rectangle"  >
        <solid android:color="#fff"/>
        <stroke android:width="1dp"  android:color="#000"/>
</shape>

Activity_view.xml (FOR TABLE LAYOUT SETUP)

<?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:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="5dp" >
    </LinearLayout>

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:shrinkColumns="*"
        android:stretchColumns="*" >
    </TableLayout>

</LinearLayout>

OUTPUT:





No comments:

Post a Comment