I have been working with my custom cursor adapter and have run into a problem, the layout of the row is | textView| Button | editText | Button |. When a button is clicked a number in the edit text will either be incremented or decremented. At the moment it all works with one row, but when I add a second row the counter carries to the new row. How do I make it so that when I add a new row it will have its own separate counter variable? I am also having trouble with the text view, instead of printing the value from the database it is just prints out nothing. How do I fix these problems?
This is the custom Cursor adapter class I am using
package com.example.rory.dbtest;
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;
public class CustomCursorAdapter extends CursorAdapter{
public int counter = 0;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
this.context = context;
}
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;
}
@Override
public void bindView(View view, Context context, 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(0))));
}
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = (RelativeLayout)inflater.inflate(R.layout.row, null);
}
final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button plusButton = (Button) convertView.findViewById(R.id.plusButton);
plusButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
counter++;
edit1.setText(Integer.toString(counter));
}
});
//final EditText edit1 = (EditText)convertView.findViewById(R.id.runningTotal);
Button minusButton = (Button) convertView.findViewById(R.id.minusButton);
minusButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
counter = counter--;
edit1.setText(Integer.toString(counter));
}
});
return convertView;
}
}
And this is the XML file I am using
<?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="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:paddingBottom="5dp"/>
<Button
android:id="@+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plusButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/runningTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/minusButton"
android:layout_toStartOf="@+id/minusButton"
android:layout_marginRight="30dp" />
<Button
android:id="@+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minusButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
0 comments:
Post a Comment