Android - 无法从 EditText 获取文本,也无法在 Adapter 的 ListView 中的 TextV



我可以让文本设置一次,但在设置后应用程序崩溃并显示越界错误:

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;
        }
}

自定义列表适配器:

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;
    }
}

看起来问题是您错误地使用了列表视图的适配器。您只需要将列表视图适配器的一个实例附加到列表视图,并且只需要更新列表视图适配器中包含的数据(例如列表注释和 int[] imageCross)。适配器根据列表视图适配器的数据填充单元格的数量及其内容。

在您尝试添加第二个列表单元格的情况下,适配器的 getView 会尝试填充单元格并从位置 = 0 和位置 = 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);
    }
}

最新更新