自定义适配器不打印值

时间:2014-11-11 12:36:06

标签: android xml sqlite custom-adapter

我的自定义适配器遇到了一些问题,适配器是打印出来的| TextView | Button | EditText | Button |,它在我添加到按钮监听器之前工作,但是当我在监听器中添加了textView时出现空白。

我也想知道是否可以将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.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(1))));

}

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--;
            edit1.setText(Integer.toString(counter));
        }
    });

    return convertView;
}
}

这是我的XML代码

<?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>

1 个答案:

答案 0 :(得分:0)

布局xml存在一些问题,指定了 TextView 位置

<?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:text="item"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="30sp"
        android:paddingBottom="5dp"/>

    <Button
        android:id="@+id/plusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plus"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/item1"
        android:layout_toEndOf="@+id/item1"
        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_toRightOf="@+id/plusButton"
        android:layout_toEndOf="@+id/plusButton"
        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="minus"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

最好使用 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="item"
        android:textSize="30sp" />

    <Button
        android:id="@+id/plusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:text="plus" />

    <EditText
        android:id="@+id/runningTotal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/minusButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:text="minus" />

</LinearLayout>

<强>更新

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 {

    Context context;

    public int counter = 0;

    public CustomCursorAdapter(Context context, Cursor c) {
        super(context, c);
        this.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(1))));

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

        Button plusButton = (Button) view.findViewById(R.id.plusButton);
        plusButton.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               counter++;
               edit1.setText(Integer.toString(counter));
           }
        });

        Button minusButton = (Button) view.findViewById(R.id.minusButton);
        minusButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                counter--;
                edit1.setText(Integer.toString(counter));
            }
        });
    }

}