列表视图中的数据在焦点更改时从顶行移动到底行

时间:2012-12-13 09:06:04

标签: android listview

我正在使用包含文本视图和edittext的listview。我通过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);
                }
            }
        });

1 个答案:

答案 0 :(得分: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);

                }