将SQLite中的值汇总到EditText中

时间:2014-11-16 20:40:43

标签: java android sqlite

在我的应用程序中我有一个包含项目名称及其升的sql数据库,我有一个打印出每行的列表视图,在每行中我打印每个项目的名称和加号和减号按钮。按下按钮时,我希望将该项目的升数添加到运行总计中。我不能让它工作,我真的很感激我能得到的任何帮助。

目前我正在将每个项目的升数读入变量'test'然后我通过界面发送此变量以在edittext中打印但是当我单击一个按钮时,editText不会得到它只是打印出来的升某种字符串。

这是我试图在升读中读取的适配器,我应该尝试在这里或其他地方这样做吗?

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.TextView;

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) {

    //instantiating the values
    super(context, cursor);
    this.context = context;
    this.mMyInterface = myInterface;

    //array to sort each value per row
    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) {

    //getting the first value for the custom row (the item name)
    TextView textViewItemName = (TextView) view.findViewById(R.id.item1);
    textViewItemName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));


    final TextView test = (TextView) view.findViewById(R.id.item1);
    test.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));

    //editText for custom row
    final EditText runningTotal = (EditText) view.findViewById(R.id.runningTotal);

    //setting up the plus button
    final Button plusButton = (Button)view.findViewById(R.id.plusButton);
    plusButton.setOnClickListener(new View.OnClickListener() {
        private int counterPos;

        public void onClick(View v) {
            //code to change the value of the editText and the array, not working

            //cursor.getPosition() returns the position in the list
            counterPos = counter.get(cursor.getPosition());

            //increments the number at counterPos
            counterPos++;

            //set the new value to the array position
            counter.set(cursor.getPosition(), counterPos);

            //changes the editText in middle of row
            runningTotal.setText(Integer.toString(counterPos));

            //sends counterPos to the interface for the running total
            mMyInterface.updateEditText(test);
        }
    });

    //setting up the minus button
    final Button minusButton = (Button)view.findViewById(R.id.minusButton);
    minusButton.setOnClickListener(new View.OnClickListener() {
        private int counterPos = 0;

        public void onClick(View v) {
            //code to change the value of the editText and the array, not working

            //cursor.getPosition() returns the position in the list
            counterPos = counter.get(cursor.getPosition());

            //decrements the number at counterPos
            counterPos--;

            //sets the new value to the array position
            counter.set(cursor.getPosition(), counterPos);

            //changes the value of the editText in the middle of the row
            runningTotal.setText(Integer.toString(counterPos));

            //send value to the interface for the running total
            mMyInterface.updateEditText(test);
        }
    });
}
}

我的界面

//interface to change the editText(running totals) on the main activity
public interface MyAdapterInterface {

public void updateEditText(TextView value);
}

0 个答案:

没有答案