3

我是 android 新手,在使用这种方法时遇到了一些问题,想知道是否有人可以提供帮助。

问题:我有一个带有 EditText 框的数组适配器,用户可以在 ListView 中修改它。但是,我只想要用户修改而不是我的片段中的程序。我该怎么做?(继续阅读完整上下文)这里的问题是,当用户在 listView 中修改此 EditText 时,适配器将调用片段,片段将操纵数字。然后它将修改列表视图中的其他 EditText。

我的问题是,当用户修改或单击列表视图中的 EditText 时,使用 afterTextChanged() 的值将传递给片段,但是由于我做了一些修改并在列表中的不同位置修改了相同的 EditText以编程方式查看,列表视图中的其他 EditText 视图也会调用 afterTextChanged 事件。我只想改变用户。

为了解决这个问题,我尝试使用这个 onTouchListener()。只是想知道这是否正确。

Class myArrayAdapter extends ArrayAdapter<Obj>{

    //private defines and static viewHolder class

    public myArrayAdapter (Context c, ArrayList<Obj> obj, MyFragment myFrag
    {
        //this....
    }

    @Override
    public View get View (int position, View convertView, ViewGroup parent){
    obj posObj = getItem(position);
    if (convertView == NULL){

        //inflate (ObjlistView)
        viewHolder.userInputValue = (EditText) covertView.findViewById(R.id.view_userInput);

       //user onTouch
                 final int pos= position;
        viewHolder.userInputValue.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent event){
                EditText UserInput= (EditText)view;
                //this is a custom Text watcher for passing editText Value to Fragment. SO I want this to be only called when user modifies it. Not when the Fragment modifies it and calling notifyDataSetChanged
                UserInput.addTextChangedListener(new CustomTextWatcher(pos, myFrag));
                return false;
            }
        });

        convertView.setTag(viewHolder)
     }else {//...}

     viewHolder.userInputValue.setText(posObj.getValue());

    } 

}

ListVie 的 XML

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"     // another annoying issue when i user clicks the editText it will show the number pad then will go to the keyboard and every time there is a delete or a new entry this keyboard change will happen. But that is not for this question. 
    android:id="@+id/view_userInput"

在片段中,这就是我再次修改编辑文本的方式

Obj myTempUserobj ;
for (int i = 0; i< userObj.size(); i++){
    myTempUserobj = userObj.get(i);
    //modify the field in the object
    userObj.set(i,myTempUserobj) 
    myAdapter.notifyDataSetChanged();
}

感谢您的帮助!

4

0 回答 0