Advertisement
samadhanmedge

TableLayout

Feb 11th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 KB | None | 0 0
  1. package com.example.sqlitedemo1;
  2.  
  3. import java.util.List;
  4.  
  5. import android.annotation.SuppressLint;
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.graphics.Bitmap;
  10. import android.graphics.Color;
  11. import android.graphics.drawable.BitmapDrawable;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.Menu;
  16. import android.view.MenuInflater;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.AutoCompleteTextView;
  20. import android.widget.EditText;
  21. import android.widget.ImageView;
  22. import android.widget.ScrollView;
  23. import android.widget.TableLayout;
  24. import android.widget.TableRow;
  25. import android.widget.TextView;
  26. import android.widget.Toast;
  27.  
  28. public class ShowAllContacts extends Activity
  29. {
  30.     AlertDialog alertDialog;
  31.     DatabaseHandler db;
  32.     List<Contact> contacts;
  33.     static Contact contact;
  34.     AutoCompleteTextView  textView1,textView2;
  35.     TableLayout ll;
  36.     ScrollView sv;
  37.    
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState)
  40.     {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.main);
  43.        
  44.         showtable();
  45.        
  46.     }
  47.    
  48.     @SuppressLint("NewApi")
  49.     private void showtable()
  50.     {
  51.         db = new DatabaseHandler(this);
  52.         contact = new Contact();
  53.  
  54.         contacts = db.getAllContacts();  
  55.         sv = new ScrollView(this);
  56.         ll=new TableLayout(this);
  57.         //ll.setOrientation(LinearLayout.VERTICAL);
  58.         sv.addView(ll);
  59.         for (final Contact cn : contacts)
  60.         {
  61.              // Writing Contacts to log
  62.             final TableRow tbrow = new TableRow(this);
  63.            
  64.             tbrow.setDividerPadding(1);
  65.             tbrow.setId(cn.getID());
  66.             TextView tv1 = new TextView(this);
  67.             tv1.setText("  " + cn.getID() + "  ");
  68.             //tv1.setBackgroundColor(Color.YELLOW);
  69.             TextView tv2 = new TextView(this);
  70.             tv2.setText("   " + cn.getName() + "  ");
  71.             //tv2.setBackgroundColor(Color.GREEN);
  72.             TextView tv3 = new TextView(this);
  73.             tv3.setText("   " + cn.getPhoneNumber() + "  ");
  74.             //tv3.setBackgroundColor(Color.YELLOW);
  75.            
  76.             ImageView imageView = new ImageView(this);
  77.             final byte[] image = db.fetchSingle(cn.getID());
  78.             Log.d("image",""+image);
  79.             //imageView.setBackgroundColor(Color.GREEN);
  80.             imageView.setImageBitmap(Contact.getImage(image));
  81. //          imageView.setImageResource(R.drawable.plusminus);
  82.             //imageView.setImageBitmap(Contact.getImage(image));
  83.             tbrow.setClickable(true);
  84.             tbrow.setBackgroundColor(R.drawable.gradient_box);
  85.             tbrow.addView(tv1);
  86.             tbrow.addView(tv2);
  87.             tbrow.addView(tv3);
  88.             tbrow.addView(imageView);
  89.             ll.addView(tbrow);
  90.             tbrow.setOnClickListener(new View.OnClickListener()
  91.             {
  92.                 @Override
  93.                 public void onClick(View v)
  94.                 {
  95.                     showDialog1(tbrow.getId(),cn.getName(),cn.getPhoneNumber(),image);
  96.                    
  97.                 }
  98.             });
  99.            
  100.            
  101.         }
  102.         db.close();
  103.         setContentView(sv);
  104.     }
  105.    
  106.     @SuppressWarnings("deprecation")
  107.     private void showDialog1(final int i, String strName, String strContact, byte[] image)
  108.     {
  109.          LayoutInflater factory = LayoutInflater.from(this);
  110.  
  111.          final View textEntryView = factory.inflate(R.layout.dialog_layout, null);
  112.                 //text_entry is an Layout XML file containing two text field to display in alert dialog
  113.  
  114.          final EditText input1 = (EditText) textEntryView.findViewById(R.id.text1);
  115.          final EditText input2 = (EditText) textEntryView.findViewById(R.id.text2);
  116.                    
  117.          input1.setText(strName, TextView.BufferType.EDITABLE);
  118.          input2.setText(strContact, TextView.BufferType.EDITABLE);
  119.  
  120.          final AlertDialog.Builder alert = new AlertDialog.Builder(this);
  121.          alert.setIcon(new BitmapDrawable(Contact.getImage(image)));
  122.          alert.setTitle("Edit or Delete Entry ");
  123.          alert.setMessage("Id = "+i);
  124.          alert.setView(textEntryView);
  125.          alert.setPositiveButton("Delete",
  126.                  new DialogInterface.OnClickListener()
  127.                 {
  128.                     public void onClick(DialogInterface dialog,int whichButton)  
  129.                     {
  130.                         contact = db.getContact(i);
  131.                         if(contact!=null)
  132.                         {
  133.                             db.deleteContact(contact);
  134.                             contact=null;
  135.                             showtable();
  136.                         }
  137.                         else
  138.                         {
  139.                             showToast("Information Not Available");
  140.                         }
  141.                     }
  142.  
  143.                    
  144.                 });
  145.          alert.setNegativeButton("Cancel",
  146.                  new DialogInterface.OnClickListener()
  147.                 {
  148.                     public void onClick(DialogInterface dialog,
  149.                     int whichButton)
  150.                     {
  151.          
  152.                     }
  153.                  });
  154.          alert.setNeutralButton("Edit",
  155.                  new DialogInterface.OnClickListener()
  156.                 {
  157.                     public void onClick(DialogInterface dialog,
  158.                     int whichButton)
  159.                     {
  160.                         contact = db.getContact(i);
  161.                         db.updateContact(contact,input1.getText().toString().trim(),input2.getText().toString().trim());
  162.                         showtable();
  163.                     }
  164.                  });
  165.          alert.show();
  166.        
  167. //      AlertDialog.Builder builder = new AlertDialog.Builder(ShowAllContacts.this);
  168. ////        textView1 = new AutoCompleteTextView(this);
  169. ////        textView2 = new AutoCompleteTextView(this);
  170. //     
  171. //      builder.setTitle("Id = "+i);
  172. //      builder.setMessage("Edit or Delete Entry");
  173. ////   
  174. ////        builder.setView(textView1);
  175. ////        builder.setView(textView2);
  176. //     
  177. //      builder.setPositiveButton("Delete",
  178. //              new DialogInterface.OnClickListener()
  179. //              {
  180. //                  public void onClick(DialogInterface dialog, int whichButton)
  181. //                  {
  182. //                     
  183. //                  }
  184. //              });
  185. //      builder.setNegativeButton("Cancel",
  186. //              new DialogInterface.OnClickListener()
  187. //              {
  188. //                  public void onClick(DialogInterface dialog, int whichButton)
  189. //                  {
  190. //                     
  191. //                  }
  192. //              });
  193. //      builder.setNeutralButton("Edit",
  194. //              new DialogInterface.OnClickListener()
  195. //              {
  196. //                  public void onClick(DialogInterface dialog, int whichButton)
  197. //                  {
  198. //                     
  199. //                  }
  200. //              });
  201. //      builder.show();
  202.     }
  203.    
  204.     private void showToast(String string)
  205.     {
  206.         Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
  207.     }
  208.     @Override
  209.     public boolean onCreateOptionsMenu(Menu menu)
  210.     {
  211.         MenuInflater inflater = getMenuInflater();
  212.         inflater.inflate(R.menu.activity_android_sqlite_tutorial, menu);
  213.         return true;
  214.     }
  215.  
  216.  
  217.     @Override
  218.     public boolean onOptionsItemSelected(MenuItem item)
  219.     {
  220.         switch(item.getItemId())
  221.         {
  222.             case R.id.menu_Edit:
  223.                 Toast.makeText(getApplicationContext(), "Hiiiii", Toast.LENGTH_SHORT).show();
  224.                 break;
  225.             case R.id.menu_Delete:
  226.                 Toast.makeText(getApplicationContext(), "Hiiiii", Toast.LENGTH_SHORT).show();
  227.                 break;
  228.         }
  229.         return super.onOptionsItemSelected(item);
  230.     }
  231.  
  232.    
  233.    
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement