Add and update entries!
This commit is contained in:
parent
da9a01ce24
commit
43f1acb736
3 changed files with 87 additions and 2 deletions
app/src/main/java/info/nerull7/mysqlbrowser
|
@ -8,6 +8,7 @@ import android.widget.ArrayAdapter;
|
|||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -46,4 +47,12 @@ public class ElementArrayAdapter extends ArrayAdapter<String> {
|
|||
|
||||
return rowView;
|
||||
}
|
||||
|
||||
public List<String> getFieldArray(){
|
||||
return fields;
|
||||
}
|
||||
|
||||
public List<String> getValueArray(){
|
||||
return values;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,17 @@ import android.os.Bundle;
|
|||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListAdapter;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
|
||||
|
@ -22,13 +25,13 @@ import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
|
|||
/**
|
||||
* Created by nerull7 on 2014-08-06.
|
||||
*/
|
||||
public class ElementFragment extends Fragment implements AsyncDatabaseConnector.ListReturnListener {
|
||||
public class ElementFragment extends Fragment implements AsyncDatabaseConnector.ListReturnListener, AsyncDatabaseConnector.StringReturnListener {
|
||||
public static final String EDIT_ELEMENT = "edit_element";
|
||||
public static final String EDIT_LIST = "edit_element_list";
|
||||
|
||||
private String databaseName;
|
||||
private String tableName;
|
||||
private ListAdapter listAdapter;
|
||||
private ElementArrayAdapter listAdapter;
|
||||
|
||||
private ProgressBar progressBar;
|
||||
private ListView listView;
|
||||
|
@ -50,6 +53,22 @@ public class ElementFragment extends Fragment implements AsyncDatabaseConnector.
|
|||
return rootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if(item.getItemId() == R.id.action_save ){
|
||||
List<String> fields = listAdapter.getFieldArray();
|
||||
|
||||
Static.asyncDatabaseConnector.setStringReturnListener(this);
|
||||
if(getArguments().getBoolean(EDIT_ELEMENT))
|
||||
Static.asyncDatabaseConnector.updateElement(tableName, fields, values, getNewValues());
|
||||
else
|
||||
Static.asyncDatabaseConnector.addNewElement(tableName, fields, getNewValues());
|
||||
return true;
|
||||
} else {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private void initArguments() {
|
||||
databaseName = getArguments().getString(Static.DATABASE_NAME_ARG);
|
||||
tableName = getArguments().getString(Static.TABLE_NAME_ARG);
|
||||
|
@ -76,4 +95,19 @@ public class ElementFragment extends Fragment implements AsyncDatabaseConnector.
|
|||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
private List<String> getNewValues(){
|
||||
List<String> newValues = new ArrayList<String>();
|
||||
|
||||
for(int i=0; i<listView.getChildCount();i++){
|
||||
EditText editText = (EditText) listView.getChildAt(i).findViewById(R.id.editFieldValue);
|
||||
newValues.add(String.valueOf(editText.getText()));
|
||||
}
|
||||
return newValues;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStringReturn(String data) { //TODO Better UI handling
|
||||
Static.showErrorAlert(data,getActivity());
|
||||
getActivity().finish();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.util.Log;
|
|||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
@ -25,6 +26,8 @@ public class AsyncDatabaseConnector {
|
|||
public static final String ACTION_FIELD_LIST = "fieldlist";
|
||||
public static final String ACTION_ENTRIES_COUNT = "numrows";
|
||||
public static final String ACTION_DATA_MATRIX = "getrows";
|
||||
public static final String ACTION_ADD_ELEMENT = "addelement";
|
||||
public static final String ACTION_UPDATE_ELEMENT = "updateelement";
|
||||
|
||||
private String login;
|
||||
private String password;
|
||||
|
@ -169,6 +172,43 @@ public class AsyncDatabaseConnector {
|
|||
downloader.execute(urlQuery);
|
||||
}
|
||||
|
||||
public void addNewElement(String table, List<String> header, List<String> values) {
|
||||
this.updateElement(table, header, null, values);
|
||||
}
|
||||
|
||||
public void updateElement(String table, List<String> header, List<String> oldValues, List<String> newValues){
|
||||
JSONArray headerJSON = new JSONArray();
|
||||
JSONArray newValuesJSON = new JSONArray();
|
||||
String request;
|
||||
|
||||
for(int i=0;i<header.size();i++){
|
||||
headerJSON.put(header.get(i));
|
||||
}
|
||||
|
||||
for(int i=0;i<newValues.size();i++){
|
||||
newValuesJSON.put(newValues.get(i));
|
||||
}
|
||||
|
||||
if(oldValues!=null){
|
||||
JSONArray oldValuesJSON = new JSONArray();
|
||||
for(int i=0;i<newValues.size();i++){
|
||||
oldValuesJSON.put(oldValues.get(i));
|
||||
}
|
||||
request = actionUrlBuilder(ACTION_UPDATE_ELEMENT)+"&d="+database+"&t="+table+"&h="+headerJSON+"&v="+newValuesJSON+"&o="+oldValuesJSON;
|
||||
} else
|
||||
request = actionUrlBuilder(ACTION_ADD_ELEMENT)+"&d="+database+"&t="+table+"&h="+headerJSON+"&v="+newValuesJSON;
|
||||
|
||||
Downloader downloader = new Downloader(new Downloader.OnFinishedListener() {
|
||||
@Override
|
||||
public void onFinished(String data, String error) {
|
||||
if(stringReturnListener!=null){
|
||||
stringReturnListener.onStringReturn(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
downloader.execute(request);
|
||||
}
|
||||
|
||||
public void setBooleanReturnListener(BooleanReturnListener booleanReturnListener){
|
||||
this.booleanReturnListener = booleanReturnListener;
|
||||
}
|
||||
|
@ -227,6 +267,8 @@ public class AsyncDatabaseConnector {
|
|||
InputStream inputStream = null;
|
||||
String response;
|
||||
|
||||
Log.d("URL REQUEST", urlRequest);
|
||||
|
||||
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // TODO Handling no connection
|
||||
urlConnection.setReadTimeout(READ_TIMEOUT);
|
||||
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue