添加新项目时,listview中的edittext设置选择

时间:2015-01-14 09:39:32

标签: android listview android-edittext

listview的布局是LinearLayout,包含两个项目:CheckBox和EditText。

我想要实现的是在向列表视图添加新项目时,在从开始到结束选择的新添加项目中设置editText。然后显示软键盘。

我在适配器中尝试的代码的getView()函数如下(ToDoItemEntity是listView项的属性类):

public class ToDoItemAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<ToDoItemEntity> toDoListData;
private ToDoItemModel toDoItemModel;
private int fragmentId;
private static final String TAG = ToDoItemAdapter.class.getName();

public ToDoItemAdapter(Context context, ArrayList<ToDoItemEntity> toDoListData,
                       ToDoItemModel toDoItemModel, int fragmentId) {
    this.mInflater = LayoutInflater.from(context);
    this.toDoListData = toDoListData;
    this.toDoItemModel = toDoItemModel;
    this.fragmentId = fragmentId;
}

@Override
public int getCount() {
    return this.toDoListData.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

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

        holder.editText = (EditText) convertView.findViewById(R.id.text);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
        holder.mWatcher = new MutableWatcher();
        holder.myOnClickListener = new MyOnClickListener(holder.editText);

        holder.editText.addTextChangedListener(holder.mWatcher);
        holder.checkBox.setOnClickListener(holder.myOnClickListener);

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

    ToDoItemEntity entity = this.toDoListData.get(position);

    holder.mWatcher.setActive(false);
    holder.editText.setText(this.toDoListData.get(position).text);
    holder.editText.setEnabled(!entity.isChecked);
    holder.editText.getPaint().setFlags(entity.isChecked ? Paint.STRIKE_THRU_TEXT_FLAG : 0);
    holder.checkBox.setChecked(entity.isChecked);

    if (this.toDoListData.get(position).focusable) {
        Log.d(TAG, "position: " + position + "focusable!");
        holder.editText.setFocusableInTouchMode(true);
        holder.editText.requestFocus();
        holder.editText.setSelection(0, holder.editText.getText().length());

        Timer timer = new Timer();
        timer.schedule(new TimerTask()
                       {
                           public void run()
                           {
                               InputMethodManager inputManager =
                                       (InputMethodManager) holder.editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(holder.editText, 0);
                           }
                       },
                500);
    }

    holder.mWatcher.setPosition(position);
    holder.mWatcher.setActive(true);

    holder.myOnClickListener.setPosition(position);

    return convertView;
}

public class ViewHolder {
    public EditText editText;
    public CheckBox checkBox;
    public MutableWatcher mWatcher;
    public MyOnClickListener myOnClickListener;
}

顺便说一句,如果你下载了jtodo应用程序,当你点击应用程序中的添加项目按钮时,你会明白我说的更清楚。

0 个答案:

没有答案