EditText Value与ListView中第一行的值相同

时间:2014-11-07 11:45:20

标签: java android

EditText - holder.ednum.setText为ListView中的第一个Item和lastitem(Fourth)设置相同的值 例如,如果我按下ListView中第一行的加号图标(请参阅图像),则会更改第一个项目以及列表视图中的最后一个项目的值。它必须与holder.ednum做一些事情,但我不确定如何解决这个问题。

请参阅截图,以便更好地了解我的问题:

  package com.freshmenu.mylistview;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.pavan.mylistview.R;

public class CustomAdapter extends BaseAdapter {

    public int max_range = 9;

    public int min_range = 0;

    int initialvalues = 0;

    String filepath = "/storage/sdcard0/FreshGrub/";

    String filepath1 = "/item.jpg";

    int initialvalues2 = 0;

    int pos;

    Context context;

    RowItem row_pos;

    List<RowItem> rowItems;

    RowItem selectedRowItem;

    CustomAdapter( Context context , List<RowItem> rowItems ) {

        this.context = context;
        this.rowItems = rowItems;
    }

    @Override
    public int getCount() {

        return rowItems.size();
    }

    @Override
    public Object getItem( int position ) {

        return rowItems.get( position );
    }

    @Override
    public long getItemId( int position ) {

        return rowItems.indexOf( getItem( position ) );
    }

    /* private view holder class */
    public class ViewHolder {

        ImageView profile_pic;

        TextView member_name;

        TextView status;

        TextView contactType;

        ImageButton plus;

        ImageButton minus;

        EditText ednum;

    }

    @Override
    public View getView( int position , View convertView , ViewGroup parent ) {

        final ViewHolder holder;

        LayoutInflater mInflater = ( LayoutInflater ) context.getSystemService( Activity.LAYOUT_INFLATER_SERVICE );

        if ( convertView == null ) {
            convertView = mInflater.inflate( R.layout.list_item , null );
            holder = new ViewHolder();

            holder.member_name = ( TextView ) convertView.findViewById( R.id.member_name );
            holder.profile_pic = ( ImageView ) convertView.findViewById( R.id.profile_pic );
            holder.status = ( TextView ) convertView.findViewById( R.id.state );

            //holder.profile_pic.setImageResource( row_pos.getProfile_pic_id() );

            holder.ednum = ( EditText ) convertView.findViewById( R.id.ednum );
            holder.plus = ( ImageButton ) convertView.findViewById( R.id.plus );
            holder.minus = ( ImageButton ) convertView.findViewById( R.id.minus );

            convertView.setTag( holder );
        } else {
            holder = ( ViewHolder ) convertView.getTag();
        }

        row_pos = rowItems.get( position );
        Bitmap bitmap = decodeSampledBitmapFromPath( filepath + row_pos.getStatus() + filepath1 , 400 , 225 );
        holder.profile_pic.setImageBitmap( bitmap );

        //holder.profile_pic.setImageResource( row_pos.getProfile_pic_id() );
        holder.member_name.setText( row_pos.getMember_name() );

        holder.plus.setFocusable( false );
        holder.minus.setFocusable( false );

        holder.plus.setTag( position );
        holder.minus.setTag( position );
        holder.ednum.setTag( position );

        holder.plus.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick( View v ) {

                String tag = v.getTag().toString();
                if ( tag != null ) {

                    pos = Integer.parseInt( tag );
                    selectedRowItem = rowItems.get( pos );
                }

                //initialvalues = selectedRowItem.getInitialValues();
                String initialvalues = selectedRowItem.getInitialValues();
                int finalInitialvalues = Integer.parseInt( initialvalues );

                if ( finalInitialvalues >= min_range && finalInitialvalues <= max_range )
                    finalInitialvalues++;
                else
                    finalInitialvalues = 0;
                selectedRowItem.setInitialValues( finalInitialvalues + "" );
                if ( finalInitialvalues > max_range )
                    finalInitialvalues = min_range;
                //holder.ednum.setText( row_pos.getMember_name() );
                holder.ednum.setText( "    " + finalInitialvalues );

            }

        } );

        holder.minus.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick( View v ) {

                String tag = v.getTag().toString();
                if ( tag != null ) {

                    pos = Integer.parseInt( tag );
                    selectedRowItem = rowItems.get( pos );
                }
                //initialvalues = selectedRowItem.getInitialValues();
                String initialvalues = selectedRowItem.getInitialValues();
                int finalInitialvalues = Integer.parseInt( initialvalues );
                if ( finalInitialvalues >= min_range && finalInitialvalues <= max_range )
                    finalInitialvalues--;
                else {
                    finalInitialvalues = 9;

                }
                selectedRowItem.setInitialValues( finalInitialvalues + "" );
                if ( finalInitialvalues < min_range )
                    finalInitialvalues = max_range;

                holder.ednum.setText( "    " + finalInitialvalues );
            }
        } );

        return convertView;
    }

    private ImageView findViewById( int profilePic ) {

        // TODO Auto-generated method stub
        return null;
    }

    protected void finish() {

        // TODO Auto-generated method stub

    }

    public static Bitmap decodeSampledBitmapFromPath( String path , int reqWidth , int reqHeight ) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile( path , options );

        options.inSampleSize = calculateInSampleSize( options , reqWidth , reqHeight );

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile( path , options );
        return bmp;
    }

    public static int calculateInSampleSize( BitmapFactory.Options options , int reqWidth , int reqHeight ) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if ( height > reqHeight || width > reqWidth ) {
            if ( width > height ) {
                inSampleSize = Math.round( ( float ) height / ( float ) reqHeight );
            } else {
                inSampleSize = Math.round( ( float ) width / ( float ) reqWidth );
            }
        }
        return inSampleSize;
    }

}

1 个答案:

答案 0 :(得分:1)

问题似乎在你的getView()方法中。

if ( convertView == null ) {
        convertView = mInflater.inflate( R.layout.list_item , null );
        holder = new ViewHolder();

     // here you should only initialise(findViewById) all your views 
     // remember don't set any values to your TextView/ImageViews here

      convertView.setTag( holder );

    } else {
        holder = ( ViewHolder ) convertView.getTag();
   }

 // here you should set value of your TextViews and ImageViews and your clickListeners