Android Listview滚动重复数字

时间:2014-06-01 18:06:32

标签: android android-listview

嗨我在listview中有一个edittext,我希望用户在其上放一些数字,然后将其保存到数组中...... 但我有滚动问题,我看到其他人发布,我可以找到解决方案。

问题:"我滚动数字重复并改变位置。

我的适配器:

public class MyCustomBaseAdapter extends BaseAdapter {
 private static ArrayList<Alimentos> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Alimentos> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return searchArrayList.size();
    }

    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {      
            convertView = mInflater.inflate(R.layout.amidos, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.Alimentos);
            holder.txtQuant = (TextView) convertView.findViewById(R.id.quant1);  
            holder.Quant = (EditText) convertView.findViewById(R.id.text_quant); 
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(searchArrayList.get(position).getName());
        holder.txtQuant.setText(searchArrayList.get(position).getQuantidade());

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtQuant;
        EditText Quant;
    }
}

我的listrow:

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

    <TextView
        android:id="@+id/Alimentos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="24dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/quant1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Alimentos"
        android:layout_below="@+id/Alimentos"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/text_quant"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/quant1"
        android:layout_below="@+id/quant1"
        android:ems="10"
        android:hint="Quantidade:"
        android:inputType="number"
        android:text="0" >

        <requestFocus />
    </EditText>

</RelativeLayout>

我的listview xml:

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

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp" >
    </RelativeLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="1 porção ou equivalente (tem ± 12 g HC)"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="@dimen/activity_horizontal_margin" />

    <TextView
        android:id="@+id/niveL2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="82dp"
        android:text="Total de Hidratos de Carbono:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/insu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/niveL2"
        android:layout_alignTop="@+id/niveL2"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="38dp"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/insu"
        android:layout_marginLeft="33dp"
        android:layout_toRightOf="@+id/insu"
        android:text="HC"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/niveL2"
        android:layout_alignLeft="@+id/relativeLayout1"
        android:layout_marginBottom="28dp" >
    </RelativeLayout>

    <Button
        android:id="@+id/hcalcu"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_marginLeft="38dp"
        android:layout_toRightOf="@+id/textView4"
        android:text="Total de Hidratos Consumidos" />

    <ListView
        android:id="@+id/listViewA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/relativeLayout2"
        android:layout_alignTop="@+id/relativeLayout1"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

这是我提前感谢的第一篇帖子......

2 个答案:

答案 0 :(得分:1)

谢谢我能找到另一种方法textWatcher我会在这里为其他人发布我的解决方案:

public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {     
            convertView = mInflater.inflate(R.layout.amidos, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.Alimentos);
            holder.txtQuant = (TextView) convertView.findViewById(R.id.quant1);  
            holder.Quant = (EditText) convertView.findViewById(R.id.text_quant);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtName.setText(searchArrayList.get(position).getName());
        holder.txtQuant.setText(searchArrayList.get(position).getQuantidade());

        if (holder.textWatcher != null) {
            holder.Quant.removeTextChangedListener(holder.textWatcher);
        }

        // Create the TextWatcher corresponding to this row
        holder.textWatcher = new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("")){
                    searchArrayList.get(position).setQua("0");
                }
                else{
                    searchArrayList.get(position).setQua(s.toString());
                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        };

        holder.Quant.addTextChangedListener(holder.textWatcher);

        Alimentos ali = searchArrayList.get(position);
        holder.Quant.setText(ali.getQua());


        return convertView;
    }

        static class ViewHolder {
            public TextWatcher textWatcher;
            TextView txtName;
            TextView txtQuant;
            EditText Quant;
        }

答案 1 :(得分:0)

您的问题是您没有保存存储在EditText对象中的值。

滚动android时,重新使用旧视图以显示新视图,EditText将重新使用您插入的值。

我建议您在失去焦点后立即将值存储在EditText中,如下所示。

 holder.Quant.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus)
           //store the value here
    }
});

之后,您应该像加载EditText个对象一样加载TextView值。

holder.Quant.setText(searchArrayList.get(position).getUserQuant());

这样您就可以在每一行获得正确的值。

希望有所帮助