簡體   English   中英

Android - 無法從 EditText 獲取文本並在 Adapter 的 ListView 中的 TextViews 中設置

[英]Android - Cant Get Text from EditText and set in TextViews in ListView in Adapter

我可以將文本設置一次,但設置后應用程序崩潰並顯示越界錯誤:

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)。 適配器根據列表視圖適配器的數據填充單元格數量及其內容。

在您嘗試添加第二個 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM