2

我正在使用具有文本视图和编辑文本的列表视图。我正在通过ViewHolder. 问题是,当我在第一列和第二列中写入数据时,屏幕上可见的部分以及滚动时,第一行的数据会自动复制到最后一行。

这是代码:

private class MobileArrayAdapter extends ArrayAdapter
{
    ViewHolder holder;
    private Context context;
    private ArrayList values;

    @SuppressWarnings("unchecked")
    public MobileArrayAdapter(Context context, ArrayList values)
    {
        super(context, R.layout.taskstrt, values);
        this.context = context;
        this.values = values;
        System.out.println("in mobile constructor");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.taskstrt, parent, false);
            holder.editText = (EditText) convertView.findViewById(R.id.editText1);
            holder.textView = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(values.get(position).toString());
        holder.textView.setId(position);
        holder.editText.setId(position);
        holder.editText.setFocusable(false);
        // holder.editText.setFocusableInTouchMode(true);

        holder.editText.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                final int positio = v.getId();
                edittexthold = (EditText) v;
                textholder = (TextView) v;
                edittexthold.setFocusable(true);

                if (edittexthold.getText().toString().length() > 0)

                {
                    HashMap<Integer, String> Map = new HashMap<Integer, String>();
                    Map.put(positio, edittexthold.getText().toString());

                    data.set(positio, Map);
                    System.out.println(data);
                }

                System.out.println(positio + " this is position that is clicked");
                if (positio == 4)
                {
                    System.out.println("going to assign ID");
                    qq = positio;
                    showDialog(NAME_LIST);
                }

                if (positio == 5)
                {
                    qq = positio;
                    System.out.println("going to start time ID");
                    showDialog(STARTTIME);
                }

                if (positio == 3)
                {
                    qq = positio;
                    System.out.println("going to date picker ID");
                    showDialog(DATE_DIALOG_ID);
                }
            }
        }) ;
        holder.editText.setOnFocusChangeListener(new OnFocusChangeListener()
        {

            public void onFocusChange(View v, boolean hasFocus)
            {
                //if(!hasFocus)
                {
                    final int positio = v.getId();
                    String conv = null;
                    final EditText edittexthold = (EditText) v;
                    final TextView textholder = (TextView) v;
                    edittexthold.setFocusable(true);

                    if (edittexthold.getText().toString().length() > 0)

                    {
                        HashMap<Integer, String> Map = new HashMap<Integer, String>();
                        Map.put(positio, edittexthold.getText().toString());

                        data.set(positio, Map);
                        System.out.println(data);
                    }

                    edittexthold.setFocusable(false);
                }
            }
        });
4

1 回答 1

1

请尝试此代码,它可能对您有所帮助

private class MobileArrayAdapter extends ArrayAdapter
{

     ViewHolder holder;
            private Context context;
            private ArrayList values;

            @SuppressWarnings("unchecked")
            public MobileArrayAdapter(Context context, ArrayList values)
            {

                super(context, R.layout.taskstrt, values);
                this.context = context;
                this.values = values;
                System.out.println("in mobile constructor");



            }

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


                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         convertView = inflater.inflate(R.layout.taskstrt, parent, false);

               if (convertView == null || convertView.getTag() == null) {

                     holder = new ViewHolder();



                    holder.editText = (EditText) convertView.findViewById(R.id.editText1);
                    holder.textView = (TextView) convertView.findViewById(R.id.textView1);

         holder.textView.setText(values.get(position).toString());

                    holder.textView.setId(position);
                    holder.editText.setId(position);
                    holder.editText.setFocusable(false);
                   // holder.editText.setFocusableInTouchMode(true);

                    holder.editText.setOnClickListener(new OnClickListener() {




                        public void onClick(View v) {



                            final int positio = v.getId();
                             edittexthold = (EditText) v;
                             textholder = (TextView) v;
                             edittexthold.setFocusable(true);

                             if(edittexthold.getText().toString().length()>0)

                             {  HashMap<Integer, String> Map=new HashMap<Integer, String>();
                                    Map.put(positio, edittexthold.getText().toString());

                                    data.set(positio, Map);
                                    System.out.println(data);
                                }

                            System.out.println(positio+" this is position that is clicked");
                            if(positio==4)
                            {
                                System.out.println("going to assign ID");
                                qq=positio;
                                showDialog(NAME_LIST);


                            }

                            if(positio==5)
                            {
                                qq=positio;
                                System.out.println("going to start time ID");
                            showDialog(STARTTIME);
                            }

                            if(positio==3)
                                {
                                qq=positio;
                                System.out.println("going to date picker ID");
                                showDialog(DATE_DIALOG_ID);
                                }







                        }
                    }) ;
                    holder.editText.setOnFocusChangeListener(new OnFocusChangeListener() {

                    public void onFocusChange(View v, boolean hasFocus) 
                    {

                    //if(!hasFocus)
                        {


                        final int positio = v.getId();
                        String conv=null;
                        final EditText edittexthold = (EditText) v;
                        final TextView textholder = (TextView) v;
                        edittexthold.setFocusable(true);

                         if(edittexthold.getText().toString().length()>0)

                         {  HashMap<Integer, String> Map=new HashMap<Integer, String>();
                                Map.put(positio, edittexthold.getText().toString());

                                data.set(positio, Map);
                                System.out.println(data);
                            }

                         edittexthold.setFocusable(false);
                    }   }
                });


                    convertView.setTag(holder);

                } 
于 2012-12-13T09:18:12.070 回答