Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.somsakelect.sqltesting;
import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
//Input enter
EditText nameEnter, passEnter;
//List
private ArrayList<String> itemsID;
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
//URL
private static final String USER_LIST_URL = "http://somsakelect.com/my-test/get-user-table-json.php";
private static final String ADD_USER_URL = "http://somsakelect.com/my-test/add-user.php";
private static final String USER_LOGIN_URL = "http://somsakelect.com/my-test/user-login.php";
private static final String DELETE_USER_URL = "http://somsakelect.com/my-test/delete-user.php";
//Other
private ProgressDialog dialog;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEnter = findViewById(R.id.name_enter);
passEnter = findViewById(R.id.pass_enter);
ListView listView = findViewById(R.id.list_view);
items = new ArrayList<>();
itemsID = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get id from list and delete user from database
deleteUser(itemsID.get(position));
}
});
findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set url to php script for user login
userLoginOrAdd(USER_LOGIN_URL);
}
});
//Get current user list
findViewById(R.id.list_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showUserList();
}
});
//Add user
findViewById(R.id.add_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set url to php script for adding user
userLoginOrAdd(ADD_USER_URL);
}
});
}
private void showUserList(){
//Show progress
showProgress();
//Connect and get data
Ion.with(this)
.load(USER_LIST_URL) //URL to PHP script for getting data
.asJsonArray()
.setCallback(new FutureCallback<JsonArray>() {
@Override
public void onCompleted(Exception e, JsonArray result) {
dialog.dismiss();
Log.w(TAG, "Error: "+e);
Log.w(TAG, "Result: "+result);
if (result!=null){
//clear previously result on item
items.clear();
itemsID.clear();
//Loop add item
for (int i=0; i<result.size(); i++){
JsonObject object = (JsonObject)result.get(i);
String id = object.get("id").getAsString();
String name = object.get("name").getAsString();
String pass = object.get("password").getAsString();
//Set detail on item
String r = "id = "+id+" name = "+name+" password = "+pass;
Log.w(TAG, r);
//Add item
items.add(r);
//Save id
itemsID.add(id);
}
//Update item on showing
adapter.notifyDataSetChanged();
//Check list
if (result.size()<1){
Toast.makeText(MainActivity.this,
"Not found data!", Toast.LENGTH_LONG).show();
}
}else {
//Show error
Toast.makeText(MainActivity.this, "Error: "+e, Toast.LENGTH_LONG).show();
}
}
});
}
private void userLoginOrAdd(@NonNull final String url){
//Get data
String name = nameEnter.getText().toString();
String pass = passEnter.getText().toString();
//Check data
if (name.isEmpty() || pass.isEmpty()){
Toast.makeText(MainActivity.this, "Please enter name and password!", Toast.LENGTH_LONG).show();
}else {
Ion.with(MainActivity.this)
.load(url)
.setBodyParameter("name", name)
.setBodyParameter("password", pass)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
Log.e(TAG, "Network error: "+ e);
Log.w(TAG, "Result: "+result);
if(result!=null){
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
//Update list
if (url.equals(ADD_USER_URL)){ showUserList(); }
}else {
Toast.makeText(MainActivity.this, "Error: "+e, Toast.LENGTH_LONG).show();
}
}
});
}
}
private void deleteUser(String id){
Log.w(TAG, "Delete ID: "+id);
Ion.with(MainActivity.this)
.load(DELETE_USER_URL)
.setBodyParameter("userId", id)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {
Log.e(TAG, "Network error: "+ e);
Log.w(TAG, "Result: "+result);
if(result!=null){
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
//Update list
showUserList();
}else {
Toast.makeText(MainActivity.this, "Error: "+e, Toast.LENGTH_LONG).show();
}
}
});
}
private void showProgress(){
dialog = new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Progressing...");
dialog.setIndeterminate(true);
dialog.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment