gpt4 book ai didi

java - Android - 无法从 EditText 获取文本并在适配器的 ListView 的 TextViews 中设置

转载 作者:行者123 更新时间:2023-11-30 00:19:16 25 4
gpt4 key购买 nike

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

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

但是我不确定为什么当我设置了一个计数器并且它获取文本然后递增计数器时它会越界。

我想做的是,当用户在编辑文本上写下注释并单击一个按钮时,这会在 TextView 中设置文本,然后用户可以添加另一个注释,这将在前一个 TextView 下,依此类推。但是尝试了各种方法,但没有用:

在我的标签中我有这个:

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

最佳答案

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

在您的情况下,当您尝试添加第二个 listcell 时,适配器的 getView 会尝试填充单元格并从 positon = 0 和 position = 1 获取数据。但是由于只有 1 个元素被传递到适配器中,它会导致输出位置 1 的边界误差。

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

标签:

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

关于java - Android - 无法从 EditText 获取文本并在适配器的 ListView 的 TextViews 中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com