简体   繁体   English

如何将内容保存在列表视图的edittext中

[英]How do I save contents in an edittext that is in a listview

In a tabbed activity, under tab 1, i have a button that adds edittexts to the screen and one each, I'd want the user to enter some text. 在选项卡式活动中,在选项卡1下,我有一个按钮,可将编辑文本添加到屏幕上,每个按钮一个,我希望用户输入一些文本。 However, when the list gets long enough, when the user opens the keyboard, it covers the list. 但是,当列表足够长时,当用户打开键盘时,它将覆盖列表。 After that when the keyboard closes all the matter typed onto the edittext gets disappeared. 之后,当键盘关闭时,在edittext上键入的所有内容都会消失。 So i want to know how to take in the data typed in an edittext to an arraylist. 所以我想知道如何将在edittext中键入的数据接收到arraylist中。 I know we need to get gettext.tostring() but where do i type it so that it automatically gets added to the arraylist? 我知道我们需要获取gettext.tostring(),但是我应该在哪里键入它以便自动将其添加到arraylist?

have a look at this to know the issue : https://drive.google.com/open?id=15YccHFrKYdvoxnc-ftZT9WSfcEGJG5Os 看看这个就知道问题了: https : //drive.google.com/open?id=15YccHFrKYdvoxnc-ftZT9WSfcEGJG5Os

Heres what I've done: 这是我所做的:

public class tab1task extends Fragment {

ArrayList<String> tasks= new ArrayList<String>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    final View view1= inflater.inflate(R.layout.tasktab1,container,false);



    Button add_task = (Button) view1.findViewById(R.id.addtask);
    add_task.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            tasks.add("");
            ListView listView = (ListView) view1.findViewById(R.id.alltasks);

            customAdapter customAdapter = new customAdapter();

            listView.setAdapter(customAdapter);
        }
    });
    ListView listView = (ListView) view1.findViewById(R.id.alltasks);

    customAdapter customAdapter = new customAdapter();
    listView.setAdapter(customAdapter);
    return view1;

}





class customAdapter extends BaseAdapter {

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

        View view = getActivity().getLayoutInflater().inflate(R.layout.addtaskcustomlayout,null);
        EditText task = (EditText) view.findViewById(R.id.taskadd);
        task.setId(position);
        tasks.set(position,task.getText().toString());
        task.setText(tasks.get(position));



        return view;
    }



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

@Override
public Object getItem(int position) {
    return tasks.get(position);
}

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

}

You are not saving your value after you press the button ADD . 按下按钮ADD后,您没有保存您的值。

Try to use the code below : 尝试使用以下代码:

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

        View view = getActivity().getLayoutInflater().inflate(R.layout.addtaskcustomlayout,null);
        EditText task = (EditText) view.findViewById(R.id.taskadd);
        task.setId(position);
        task.setText(tasks.get(position));
        TextWatcher tw = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {

                 tasks.set(position,s.toString());
            }
        };

        task.addTextChangedListener(tw);

        return view;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM