Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sqlitedemo1;
- import java.util.List;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AutoCompleteTextView;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ScrollView;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ShowAllContacts extends Activity
- {
- AlertDialog alertDialog;
- DatabaseHandler db;
- List<Contact> contacts;
- static Contact contact;
- AutoCompleteTextView textView1,textView2;
- TableLayout ll;
- ScrollView sv;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- showtable();
- }
- @SuppressLint("NewApi")
- private void showtable()
- {
- db = new DatabaseHandler(this);
- contact = new Contact();
- contacts = db.getAllContacts();
- sv = new ScrollView(this);
- ll=new TableLayout(this);
- //ll.setOrientation(LinearLayout.VERTICAL);
- sv.addView(ll);
- for (final Contact cn : contacts)
- {
- // Writing Contacts to log
- final TableRow tbrow = new TableRow(this);
- tbrow.setDividerPadding(1);
- tbrow.setId(cn.getID());
- TextView tv1 = new TextView(this);
- tv1.setText(" " + cn.getID() + " ");
- //tv1.setBackgroundColor(Color.YELLOW);
- TextView tv2 = new TextView(this);
- tv2.setText(" " + cn.getName() + " ");
- //tv2.setBackgroundColor(Color.GREEN);
- TextView tv3 = new TextView(this);
- tv3.setText(" " + cn.getPhoneNumber() + " ");
- //tv3.setBackgroundColor(Color.YELLOW);
- ImageView imageView = new ImageView(this);
- final byte[] image = db.fetchSingle(cn.getID());
- Log.d("image",""+image);
- //imageView.setBackgroundColor(Color.GREEN);
- imageView.setImageBitmap(Contact.getImage(image));
- // imageView.setImageResource(R.drawable.plusminus);
- //imageView.setImageBitmap(Contact.getImage(image));
- tbrow.setClickable(true);
- tbrow.setBackgroundColor(R.drawable.gradient_box);
- tbrow.addView(tv1);
- tbrow.addView(tv2);
- tbrow.addView(tv3);
- tbrow.addView(imageView);
- ll.addView(tbrow);
- tbrow.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- showDialog1(tbrow.getId(),cn.getName(),cn.getPhoneNumber(),image);
- }
- });
- }
- db.close();
- setContentView(sv);
- }
- @SuppressWarnings("deprecation")
- private void showDialog1(final int i, String strName, String strContact, byte[] image)
- {
- LayoutInflater factory = LayoutInflater.from(this);
- final View textEntryView = factory.inflate(R.layout.dialog_layout, null);
- //text_entry is an Layout XML file containing two text field to display in alert dialog
- final EditText input1 = (EditText) textEntryView.findViewById(R.id.text1);
- final EditText input2 = (EditText) textEntryView.findViewById(R.id.text2);
- input1.setText(strName, TextView.BufferType.EDITABLE);
- input2.setText(strContact, TextView.BufferType.EDITABLE);
- final AlertDialog.Builder alert = new AlertDialog.Builder(this);
- alert.setIcon(new BitmapDrawable(Contact.getImage(image)));
- alert.setTitle("Edit or Delete Entry ");
- alert.setMessage("Id = "+i);
- alert.setView(textEntryView);
- alert.setPositiveButton("Delete",
- new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog,int whichButton)
- {
- contact = db.getContact(i);
- if(contact!=null)
- {
- db.deleteContact(contact);
- contact=null;
- showtable();
- }
- else
- {
- showToast("Information Not Available");
- }
- }
- });
- alert.setNegativeButton("Cancel",
- new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog,
- int whichButton)
- {
- }
- });
- alert.setNeutralButton("Edit",
- new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog,
- int whichButton)
- {
- contact = db.getContact(i);
- db.updateContact(contact,input1.getText().toString().trim(),input2.getText().toString().trim());
- showtable();
- }
- });
- alert.show();
- // AlertDialog.Builder builder = new AlertDialog.Builder(ShowAllContacts.this);
- //// textView1 = new AutoCompleteTextView(this);
- //// textView2 = new AutoCompleteTextView(this);
- //
- // builder.setTitle("Id = "+i);
- // builder.setMessage("Edit or Delete Entry");
- ////
- //// builder.setView(textView1);
- //// builder.setView(textView2);
- //
- // builder.setPositiveButton("Delete",
- // new DialogInterface.OnClickListener()
- // {
- // public void onClick(DialogInterface dialog, int whichButton)
- // {
- //
- // }
- // });
- // builder.setNegativeButton("Cancel",
- // new DialogInterface.OnClickListener()
- // {
- // public void onClick(DialogInterface dialog, int whichButton)
- // {
- //
- // }
- // });
- // builder.setNeutralButton("Edit",
- // new DialogInterface.OnClickListener()
- // {
- // public void onClick(DialogInterface dialog, int whichButton)
- // {
- //
- // }
- // });
- // builder.show();
- }
- private void showToast(String string)
- {
- Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.activity_android_sqlite_tutorial, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- switch(item.getItemId())
- {
- case R.id.menu_Edit:
- Toast.makeText(getApplicationContext(), "Hiiiii", Toast.LENGTH_SHORT).show();
- break;
- case R.id.menu_Delete:
- Toast.makeText(getApplicationContext(), "Hiiiii", Toast.LENGTH_SHORT).show();
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement