Facebook
From Gray Gibbon, 4 Years ago, written in Plain Text.
This paste is a reply to 123 from Cobalt Pintail - view diff
Embed
Download Paste or View Raw
Hits: 333
  1. package com.example.maciek.projekt;
  2.  
  3. import android.content.Intent;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Bundle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.BaseAdapter;
  15. import android.widget.EditText;
  16. import android.widget.ImageView;
  17. import android.widget.ListView;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import java.util.ArrayList;
  22.  
  23. public class kontaktylista2 extends AppCompatActivity {
  24.     ArrayList<listacustom2> lista = new ArrayList<listacustom2>();
  25.     SQLiteDatabase myDB;
  26.     EditText szukanytekst;
  27.     ListView list;
  28.     CustomAdapter2 customAdapter;
  29.     public void listaprzypomnien(String string) {
  30.         Cursor c;
  31.         if(string == null || string.equals("")) {
  32.             c = myDB.rawQuery("select Imie, Nazwisko, Telefon, Email, Plec, id, Avatar from kontakty", null);
  33.         }
  34.         else {
  35.             c = myDB.rawQuery("select Imie, Nazwisko, Telefon, Email, Plec, id, Avatar from kontakty WHERE Imie LIKE '%"+string+"%' OR Nazwisko LIKE '%"+string+"%' OR Telefon LIKE '%"+string+"%' OR Email LIKE '%"+string+"%'", null);
  36.         }
  37.         while (c.moveToNext()) {
  38.             String Imie = c.getString(0);
  39.             String Nazwisko = c.getString(1);
  40.             String Telefon = c.getString(2);
  41.             String Email = c.getString(3);
  42.             String Plec = c.getString(4);
  43.             int id = c.getInt(5);
  44.             byte[] avatar = c.getBlob(6);
  45.             lista.add(new listacustom2(Imie, Nazwisko, Telefon, Email, Plec, id, avatar));
  46.        }
  47.        c.close();
  48.        // lista.add(new listacustom2("aa","aa","aa","aa","aa",5));
  49.     }
  50.     @Override
  51.     protected void onCreate(Bundle savedInstanceState) {
  52.         super.onCreate(savedInstanceState);
  53.         setContentView(R.layout.activity_kontaktylista2);
  54.         myDB = openOrCreateDatabase("my.db", MODE_PRIVATE, null);
  55.         myDB.execSQL(
  56.                 "CREATE TABLE IF NOT EXISTS kontakty (id INTEGER PRIMARY KEY AUTOINCREMENT, Imie VARCHAR(200), Nazwisko VARCHAR(200), Telefon VARCHAR(200), Email VARCHAR(200), Plec VARCHAR(1), Avatar BLOB, Wybrany INTEGER)"
  57.         );
  58.         list = (ListView) findViewById(R.id.kontaktylista);
  59.         customAdapter = new CustomAdapter2();
  60.         aktualizuj(null);
  61.         szukanytekst=(EditText) findViewById(R.id.editText10);
  62.         szukanytekst.addTextChangedListener(szukajkatextwatcher);
  63.     }
  64.     public void aktualizuj(String string) {
  65.         lista.clear();
  66.         listaprzypomnien(string);
  67.         if(lista.size()==0) {
  68.             Toast.makeText(getApplicationContext(), "Brak kontaktow!", Toast.LENGTH_SHORT).show();
  69.         }
  70.         list.setAdapter(customAdapter);
  71.     }
  72.     private TextWatcher szukajkatextwatcher = new TextWatcher() {
  73.         @Override
  74.         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  75.            // aktualizuj(szukanytekst.getText().toString());
  76.         }
  77.  
  78.         @Override
  79.         public void onTextChanged(CharSequence s, int start, int before, int count) {
  80.             aktualizuj(szukanytekst.getText().toString());
  81.         }
  82.  
  83.         @Override
  84.         public void afterTextChanged(Editable s) {
  85.           //  aktualizuj(szukanytekst.getText().toString());
  86.         }
  87.     };
  88. class CustomAdapter2 extends BaseAdapter {
  89.     @Override
  90.     public int getCount() {
  91.         return lista.size();
  92.     }
  93.     @Override
  94.     public Object getItem(int i) {
  95.         return null;
  96.     }
  97.     @Override
  98.     public long getItemId(int i) {
  99.         return 0;
  100.     }
  101.     @Override
  102.     public View getView(int i, View view, ViewGroup viewGroup) {
  103.         view = getLayoutInflater().inflate(R.layout.rowkontakty,null);
  104.         TextView textImie = (TextView)view.findViewById(R.id.textcontactname);
  105.         TextView textNazwisko = (TextView)view.findViewById(R.id.textcontactlastname);
  106.         TextView textTelefon = (TextView)view.findViewById(R.id.textcontactphone);
  107.         textImie.setText(lista.get(i).Imie);
  108.         textNazwisko.setText(lista.get(i).Nazwisko);
  109.         textTelefon.setText(lista.get(i).Telefon);
  110.         if(lista.get(i).Avatar.length >= 1) {
  111.            Bitmap Avatar = BitmapFactory.decodeByteArray(lista.get(i).Avatar, 0, lista.get(i).Avatar.length);
  112.             ImageView imageview = (ImageView) view.findViewById(R.id.imageViewavatar);
  113.             imageview.setImageBitmap(Avatar);
  114.         }
  115.         final int ii = i;
  116.         view.setOnClickListener(new View.OnClickListener() {
  117.             @Override
  118.             public void onClick(View view) {
  119.                 view.setBackgroundResource(R.color.colorPrimaryDark);
  120.                 Intent intent = new Intent(kontaktylista2.this, KontaktyDisplay.class);
  121.                 intent.putExtra("id", lista.get(ii).id);
  122.                 startActivity(intent);
  123.                 finish();
  124.             }
  125.         });
  126.         return view;
  127.     }
  128. }
  129. class listacustom2 {
  130.     public int id = 0;
  131.     public String Imie="";
  132.     public String Nazwisko="";
  133.     public String Telefon="";
  134.     public String Email="";
  135.     public String Plec="";
  136.     public byte[] Avatar=null;
  137.     listacustom2(String Imie, String Nazwisko, String Telefon, String Email, String Plec, int id, byte[] Avatar) {
  138.         this.Imie = Imie;
  139.         this.Nazwisko = Nazwisko;
  140.         this.Telefon = Telefon;
  141.         this.Email = Email;
  142.         this.Plec = Plec;
  143.         this.Avatar=Avatar;
  144.         this.id = id;
  145.     }
  146. }
  147. }
  148.