Selasa, 15 Agustus 2017

ListCheckbox Database




package app.undangan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import app.undangan.MemberList.load;

public class ListCHK extends Activity {
String ip="",kode_undangan="";
int jd=0;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
String kode_member,nama_member;

ArrayList<HashMap<String, String>> arrayList;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";

private static final String TAG_kode_member= "kode_member";
private static final String TAG_nama_member = "nama_member";
private static final String TAG_jenis_kelamin= "jenis_kelamin";
private static final String TAG_alamat= "alamat";
private static final String TAG_telepon = "telepon";
private static final String TAG_email= "email";

String all="";
String[]arKM;
String[]arNM;
String[]arJK;
String[]arAL;
String[]arTelp;
String[]arEmail;


TextView txtjudul;
MyCustomAdapter dataAdapter = null;
String judul,waktu, deskripsi;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.listchk);
 
 
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(ListCHK.this);
Boolean Registered = sharedPref.getBoolean("Registered", false);
       if (!Registered){
           finish();
       }else {
        kode_member = sharedPref.getString("kode_member", "");
        nama_member = sharedPref.getString("nama_member", "");
       }
     
  Intent i = getIntent();
judul= i.getStringExtra("judul");
waktu= i.getStringExtra("waktu");
deskripsi= i.getStringExtra("deskripsi");
kode_undangan= i.getStringExtra("kode_undangan");

  txtjudul = (TextView) findViewById(R.id.txtjudul);
  txtjudul.setText(judul +" /"+waktu +"("+kode_undangan+")");;
 
  ip=jParser.getIP();
  new load().execute();


  Button myButton = (Button) findViewById(R.id.findSelected);
  myButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    StringBuffer responseText = new StringBuffer();
    responseText.append("Pilih Peserta Undangan.\n");
all="";
   ArrayList<dataList> dataListList = dataAdapter.dataListList;
   for(int i=0;i<dataListList.size();i++){
    dataList dataList = dataListList.get(i);
    if(dataList.isSelected()){
   String nm=dataList.getName();
   String[]ar=nm.split("-");
   String nama=ar[0];
   String kode=ar[1];
   all+=kode+"#";
    responseText.append("\n" + nama);
    }
  }
   Toast.makeText(getApplicationContext(),responseText, Toast.LENGTH_LONG).show();
   new save().execute();
   }});
 }

 private void displayListView() {
  ArrayList<dataList> dataListList = new ArrayList<dataList>();
  dataList dataList = null;
 
  for(int i=0;i<jd;i++){
 dataList = new dataList(arTelp[i],arNM[i]+"-"+arKM[i],false);
 dataListList.add(dataList);
  }

  dataAdapter = new MyCustomAdapter(this,  R.layout.data_info, dataListList);
  ListView listView = (ListView) findViewById(R.id.listView1);
  listView.setAdapter(dataAdapter);

  listView.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    dataList dataList = (dataList) parent.getItemAtPosition(position);
    Toast.makeText(getApplicationContext(),"Pilih Nama: " + dataList.getName(), Toast.LENGTH_SHORT).show();
   }
  });

 }

 private class MyCustomAdapter extends ArrayAdapter<dataList> {
  private ArrayList<dataList> dataListList;
  public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<dataList> dataListList) {
   super(context, textViewResourceId, dataListList);
   this.dataListList = new ArrayList<dataList>();
   this.dataListList.addAll(dataListList);
  }

  private class ViewHolder {
   TextView code;
   CheckBox name;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder = null;
   Log.v("ConvertView", String.valueOf(position));

   if (convertView == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = vi.inflate(R.layout.data_info, null);

   holder = new ViewHolder();
   holder.code = (TextView) convertView.findViewById(R.id.code);
   holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
   convertView.setTag(holder);
    holder.name.setOnClickListener( new View.OnClickListener() {
     public void onClick(View v) {
      CheckBox cb = (CheckBox) v ;
      dataList dataList = (dataList) cb.getTag();
     // Toast.makeText(getApplicationContext(),  "Data Terpilih : " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
      dataList.setSelected(cb.isChecked());
     }
    });
   }
   else {
    holder = (ViewHolder) convertView.getTag();
   }

   dataList dataList = dataListList.get(position);
   holder.code.setText(" (" +  dataList.getCode() + ")");
   holder.name.setText(dataList.getName());
   holder.name.setChecked(dataList.isSelected());
   holder.name.setTag(dataList);

   return convertView;

  }
 }



 class save extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListCHK.this);
pDialog.setMessage("Menyimpan data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("kode_undangan", kode_undangan));
params.add(new BasicNameValuePair("all", all));
params.add(new BasicNameValuePair("kode_member", kode_member));

String url=ip+"add/add_addall.php";
Log.v("add",url);
JSONObject json = jParser.makeHttpRequest(url,"POST", params);
Log.d("add", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
Intent i = getIntent();
setResult(100, i);
finish();
} else {
// gagal update data
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String file_url) {pDialog.dismiss();}
}

 class load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListCHK.this);
pDialog.setMessage("Load data. Silahkan Tunggu...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("kode_member", kode_member));
JSONObject json = jParser.makeHttpRequest(ip+"member/member_shownotme.php", "GET", params);
Log.d("show: ", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
myJSON = json.getJSONArray(TAG_record);
jd= myJSON.length();
arKM=new String[jd];
arNM=new String[jd];
arJK=new String[jd];
arAL=new String[jd];
arTelp=new String[jd];
arEmail=new String[jd];
for (int i = 0; i < myJSON.length(); i++) {
JSONObject c = myJSON.getJSONObject(i);
String kode_member= c.getString(TAG_kode_member);
String nama_member = c.getString(TAG_nama_member);
String jenis_kelamin = c.getString(TAG_jenis_kelamin );
String alamat= c.getString(TAG_alamat);
String telepon= c.getString(TAG_telepon);
String email= c.getString(TAG_email);
arKM[i]=kode_member;
arNM[i]=nama_member;
arJK[i]=jenis_kelamin;
arAL[i]=alamat;
arTelp[i]=telepon;
arEmail[i]=email;
}
}
}
catch (JSONException e) {e.printStackTrace();}
return null;
}

protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
 displayListView();
}});}
}


}
+++++++++++++++++
package app.undangan;

public class dataList {
 
String code = null;
String name = null;
boolean selected = false;
 
public dataList(String code, String name, boolean selected) {
 super();
 this.code = code;
 this.name = name;
 this.selected = selected;
}
 
public String getCode() {
 return code;
}
public void setCode(String code) {
 this.code = code;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}

public boolean isSelected() {
 return selected;
}
public void setSelected(boolean selected) {
 this.selected = selected;
}
 
}
++++++++++++++++++++++++++++++++

listchk.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
         android:id="@+id/txtjudul"
        android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:padding="10dp"
  android:text="Pilih Member Undangan" android:textSize="20sp" />


    <Button
        android:id="@+id/findSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Simpan Daftar Peserta" />

 <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

</LinearLayout>




data_info.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="CheckBox" />

    <TextView
        android:id="@+id/code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox1"
        android:layout_alignBottom="@+id/checkBox1"
        android:layout_toRightOf="@+id/checkBox1"
        android:text="TextView" />

</RelativeLayout>





Tidak ada komentar:

Posting Komentar