Android - 无法从EditText获取文本并在适配器

时间:2017-10-06 21:40:13

标签: java android android-layout listview android-fragments

我可以让文本设置一次,但是在设置应用程序崩溃之后它会显示一个超出范围的错误:

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

但是当我设置一个计数器并且它获得文本然后递增计数器时,我不确定为什么它超出界限。

我想要做的是当用户在编辑文本上写一个注释并单击一个按钮时,它会在文本视图中设置文本,然后用户可以添加另一个注释,这将在前一个文本视图下面,依此类推。但尝试了各种各样的事情并且它没有起作用:

在我的标签中我有这个:

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.tab3, container, false);

        final EditText notes = (EditText) v.findViewById(R.id.editText);
        listView=(ListView)v.findViewById(R.id.list);

        int[] cross = new int[]{R.drawable.cross};
        notesofrules = new ArrayList<String>();

        adapter = new CustomListAdapter(this.getActivity(), notesofrules, cross);

        listView=(ListView) v.findViewById(R.id.list);

        Button button = (Button)v.findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                    notesofrules.add(counter, notes.getText().toString());
                    listView.setAdapter(adapter);
                    counter++;
                    notes.setText("");
            }
        });
            return v;
        }
}

CustomListAdapter:

public class CustomListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    ArrayList<String> notes = new ArrayList<String>();

    int[]imageCross;

    public CustomListAdapter(Activity context, ArrayList<String> notes, int[] imageCross) {
        super(context, R.layout.item,notes);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.notes = notes;
        this.imageCross = imageCross;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.item, null,false);

        TextView ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
        ImageView image = (ImageView) rowView.findViewById(R.id.icon);

        image.setImageResource(imageCross[position]);
        ruleNotesSet.setText(notes.get(position));
        return rowView;
    }
}

1 个答案:

答案 0 :(得分:1)

看起来问题是您正在使用listview的适配器。 您只需要一个listview适配器的实例附加到listview,它只是listview适配器中包含的数据(例如List notes和int [] imageCross),需要更新。适配器根据列表视图适配器的数据填充单元格数量及其内容。

在你尝试添加第二个listcell的情况下,适配器的getView会尝试填充单元格并从positon = 0和position = 1抓取数据。但是因为只有1个元素被传递到适配器,所以它会导致输出位置1的绑定错误。

更新数据 - 添加一种方法来访问和修改适配器中的数据。添加/删除所需数据后,请务必调用adapter.notifyDataSetChanged()以刷新列表视图中的单元格。

选项卡:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View v = inflater.inflate(R.layout.tab3, container, false);

    final EditText notes = (EditText) v.findViewById(R.id.editText);
    listView=(ListView)v.findViewById(R.id.list);

    int cross = R.drawable.cross;
    notesofrules = new ArrayList<String>(); //initial data list

    adapter = new CustomListAdapter(this.getActivity(), notesofrules, cross);

    listView=(ListView) v.findViewById(R.id.list);
    listView.setAdapter(adapter); //set the adapter once, only manipulate the data within

    Button button = (Button)v.findViewById(R.id.button3);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            String newNote = notes.getText().toString();
            adapter.addNote(newNote); //add new note to the adapter list
            adapter.notifyDataSetChanged(); //very important to notify adapter and refresh the listview
            notes.setText("");
        }
    });
        return v;
    }
}

适配器:

public class CustomListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    ArrayList<String> notes = new ArrayList<>();

    int imageCross; //make this a list if you have multiple images and add similar to notes list

    public CustomListAdapter(Activity context, ArrayList<String> notes, int imageCross) {
        super(context, R.layout.item,notes);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.notes = notes;
        this.imageCross = imageCross;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.item, null,false);

        TextView ruleNotesSet = rowView.findViewById(R.id.textView1);
        ImageView image = rowView.findViewById(R.id.icon);

        image.setImageResource(imageCross);
        ruleNotesSet.setText(notes.get(position));
        return rowView;
    }

    public void addNote(String data) {
        notes.add(data);
    }
}