在Android中的LIstview中编辑文本

时间:2014-07-06 11:53:05

标签: android listview android-listview

在我的项目中,需要在编辑listview项目和编辑文本时编辑文本

当我使用 android:focusable =" false" android:focusableInTouchMode =" false&#时,我在listview中使用两个textview和一个编辑文本34; 在所有listview项目中;显示列表视图选择但不编辑文本。

当我删除编辑文本中的 android:focusable =" false" android:focusableInTouchMode =" false" 然后编辑文本已编辑但未选择列表视图项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="horizontal"
    android:padding="18dip" >

    <TextView
        android:id="@+id/txtItemcode"
        android:layout_width="110dip"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="9dip"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:singleLine="true"
        android:text="323232"
        android:textColor="#0C090A"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/txtItem"
        android:layout_width="300dip"
        android:layout_height="fill_parent"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="9dip"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="5456455565456"
        android:textColor="#0C090A"
        android:textSize="20sp" />

    <EditText
        android:layout_marginTop="4.5dip"
        android:id="@+id/editcreateQuantity"
        android:layout_width="100dp"
        android:layout_height="50dip"
        android:background="@android:drawable/editbox_background"
        android:ems="10"
        android:inputType="number"
        android:singleLine="true"
        android:focusable="false"
android:focusableInTouchMode="false" />

</LinearLayout>

我的列表视图

<ListView
            android:id="@+id/createlist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
            android:clickable="true"
            android:divider="#eeeeee"
            android:dividerHeight="1dip"
            android:scrollingCache="true"
            android:smoothScrollbar="true"
            android:descendantFocusability="beforeDescendants" >
        </ListView>

这里是getView Adapter java代码

// Create List Adapter



  class CreateAdapter extends ArrayAdapter<String>  {
    String[] strItecode=null;
    String[] strItem;
    String[] strQuantity;
    Context context;

    int temp;
    CreateAdapter(Context context, String[] strItemcode, String[] strItem,
            String[] strQauntity) {
        super(context, R.layout.create_list_item, R.id.txtItemcode, strItemcode);
        this.context = context;
        this.strItecode = strItemcode;
        this.strItem = strItem;
        this.strQuantity = strQauntity;
       // text= new String[strItem.length];
    }
    private int editingPosition = 0;
    private TextWatcher watcher = new TextWatcher() {
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  text[editingPosition] = s.toString();
              }
              public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
              public void afterTextChanged(Editable s) { }
          };


    public View getView( final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        temp=position;
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {

            holder = new ViewHolder();
            convertView = mInflater
                    .inflate(R.layout.create_list_item, parent, false);

            holder.txtItecode = (TextView) convertView
                    .findViewById(R.id.txtItemcode);
            holder.txtItem = (TextView) convertView
                    .findViewById(R.id.txtItem);
            holder.editQuantity = (EditText) convertView
                    .findViewById(R.id.editcreateQuantity);
            holder.editQuantity.setTag(position);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.editQuantity.removeTextChangedListener(watcher);
       if(text[temp].contentEquals("0"))
           holder.editQuantity.setText("");
       else
        holder.editQuantity.setText(text[temp]);

        holder.editQuantity.setOnFocusChangeListener(new OnFocusChangeListener() {       
            public void onFocusChange(View v, boolean hasFocus) {
              if(hasFocus) editingPosition = position;
            }
        });

        holder.editQuantity.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.requestFocus();
                v.requestFocusFromTouch();
            }
        });

        // The rest is good, just make sure to remove the call to setting the EditText's text at the end
        holder.txtItecode.setText(strItecode[position]);
        holder.txtItem.setText(strItem[position]);
      //  holder.editQuantity.setText(text[temp]);
        return convertView;


    }

    class ViewHolder {
        TextView txtItecode;
        TextView txtItem;
        EditText editQuantity;
      }
    }

请帮助我点击列表视图时如何编辑编辑文本并单击编辑文本

提前致谢

0 个答案:

没有答案