Using SQLite Values to Change EditText Value



I am making an app that stores values and names for each value into an SQLite database. I am having some trouble with one function though, what I am trying to get it to do is the following, every time a button is clicked in the listview depending on the the row it will read from the database and take the value that is associated with that item in the row and add it to a running total at the bottom of the main activity. At the moment i have the app just adding or taking away one every time the button is clicked but how do I make it so that every click uses the database value and not the simple int counter I am using now? Thanks


This is my main activity where I am calling an interface to change the running total



import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.pinchtapzoom.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public class MyActivity extends Activity implements MyAdapterInterface{

private CustomCursorAdapter customAdapter;
public ListView list1;

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
public MyActivity mMyActivity;


//public EditText TotalCost;
//public EditText TotalLitres;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

list1 = (ListView)findViewById(R.id.data_list);
db.open();

mMyActivity = this;

Button addBtn = (Button)findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, addassignment.class);
startActivity(i);
}
});

Button deleteBtn = (Button)findViewById(R.id.delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Delete.class);
startActivity(i);
}
});

Button updateBtn = (Button)findViewById(R.id.update);
updateBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent i = new Intent(MyActivity.this, Update.class);
startActivity(i);
}
});


try {
String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


new Handler().post(new Runnable() {
@Override
public void run() {
//Log.d("test", "customadapter is " + customAdapter.toString());
//Log.d("test", "databaseHelper is " + databaseHelper.toString());
customAdapter = new CustomCursorAdapter(MyActivity.this, db.getAllRecords(), mMyActivity);
list1.setAdapter(customAdapter);
}
});

}

//chaning the running total
public void updateEditText(int value)
{
EditText myEditText = (EditText)findViewById(R.id.edit1);
EditText myEditText2 = (EditText)findViewById(R.id.edit2);
myEditText.setText(String.valueOf(value));
myEditText2.setText(String.valueOf(value));
}

private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
//private ArrayList<>

@Override
public int getCount() {

return 0;
}

@Override
public Object getItem(int arg0) {

return null;
}

@Override
public long getItemId(int arg0) {

return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

return null;
}

}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}


This is my custom adapter I am using



import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;

import java.util.ArrayList;


public class CustomCursorAdapter extends CursorAdapter{

//public int counter = 0;
public ArrayList<Integer> counter;
private MyAdapterInterface mMyInterface;

public CustomCursorAdapter(Context context, Cursor cursor, MyAdapterInterface myInterface) {

super(context, cursor);
this.context = context;
this.mMyInterface = myInterface;
counter = new ArrayList<Integer>();

//default all counters to 0
for(int i=0; i<cursor.getCount(); i++)
{
counter.add(0);
}
}
Context context;


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// when the view will be created for first time,
// we need to tell the adapters, how each item will look
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row, parent, false);

return retView;
}

public void bindView(View view, Context context, final Cursor cursor) {
// here we are setting our data
// that means, take the data from the cursor and put it in views
TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

final EditText runningTotal = (EditText) view.findViewById(R.id.runningTotal);

final Button plusButton = (Button)view.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener() {
private int counterPos;

public void onClick(View v) {
counterPos = counter.get(cursor.getPosition());
counterPos++;
counter.set(cursor.getPosition(), counterPos);
runningTotal.setText(Integer.toString(counterPos));
mMyInterface.updateEditText(counterPos);
}
});

final Button minusButton = (Button)view.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener() {
private int counterPos;

public void onClick(View v) {
counterPos = counter.get(cursor.getPosition());
counterPos--;
counter.set(cursor.getPosition(), counterPos);
runningTotal.setText(Integer.toString(counterPos));
mMyInterface.updateEditText(counterPos);
}
});
}
}


And this is the interface I am using to change the values



public interface MyAdapterInterface {

public void updateEditText(int value);
}

No comments:

Post a Comment