【问题标题】:All ListView items get cleared when a new row is added添加新行时,所有 ListView 项目都会被清除
【发布时间】:2017-07-19 06:45:15
【问题描述】:

我有一个包含复选框和 EditText 字段的列表项的自定义布局。点击添加按钮后,虽然添加了新项目,但列表被清除。之前的所有数据都消失了。我是android新手,所以请让我知道我哪里出错了......

这是我的 MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //  ArrayList for items
    final ArrayList<Item> itemArrayList = new ArrayList<>() ;

    // CheckBox and EditText
    CheckBox checkBox = (CheckBox) findViewById(R.id.default_checkbox) ;
    EditText editText = (EditText) findViewById(R.id.default_edit_text) ;

    //  Add Button
    Button addButon = (Button) findViewById(R.id.add_button);

    // Item Adapter
    final ItemAdapter itemAdapter = new ItemAdapter(this, itemArrayList) ;

    // ListView Object
    final ListView listView = (ListView) findViewById(R.id.list) ;

    // Set Adapter
    listView.setAdapter(itemAdapter);

    // Setting the onClick Listener
    addButon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            CheckBox cBox = new CheckBox(MainActivity.this);
            //EditText eText = new EditText(MainActivity.this) ;

            itemArrayList.add(new Item(cBox, "")) ;
            itemAdapter.notifyDataSetChanged() ;
        }
    });
}
}

这是我的适配器类:

public class ItemAdapter extends ArrayAdapter<Item> {

// Arraylist
private ArrayList<Item> itemsArrayList = new ArrayList<>() ;

//Constructor
/*@param context    This is the class which is sent as the context
* @param items  This is the ArrayList
* @param resource   Extra elements*/
public ItemAdapter(@NonNull Context context, ArrayList<Item> items) {
    super(context,0, items);
}

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

    View listView = convertView ;

    if (listView == null) {
        listView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

    // Item at index position
    Item currentItem = getItem(position) ;

    // Initializing elements
    CheckBox checkBox = (CheckBox) listView.findViewById(R.id.default_checkbox);
    EditText editText = (EditText) listView.findViewById(R.id.default_edit_text);

    // Setting the state of CheckBox
    if (currentItem.getCheckBox().isChecked()) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);
    }

    // Setting the state of EditText
    editText.setText(currentItem.getCheckBoxText());

    return listView;
}
}

物品类:

public class Item {

//  CheckBox
private CheckBox checkBox ;

//  Text
private String checkBoxText ;

//  Constructor

//* @param rootView   This  is the layout which contains the checkBox and editText element
//* @param cBox   This is the checkBox
//* @param text   This is the text alongside the checkBox
public Item(CheckBox cBox, String text){
    // Initializing the Item
    checkBox = cBox ;
    checkBoxText = text ;
}

// Method to get checkBox
public CheckBox getCheckBox(){
    return checkBox ;
}

// Method to get checkBoxText
public String getCheckBoxText(){
    return checkBoxText ;
}

// Method to set CheckBox
/*
* @param cBox   This is the checkBox which is set*/
public void setCheckBox(CheckBox cBox){
    checkBox = cBox ;
}

// Method to set checkBoxText
public void setCheckBoxText(String text){
    checkBoxText = (text) ;
}
}

【问题讨论】:

    标签: android android-layout listview android-adapter


    【解决方案1】:

    试试这个。 在你的适配器中添加这个方法

    public void addNewItem(Item item)
    {
        itemsArrayList.add(item);
    }
    

    现在在您的按钮单击中添加如下新项目

    itemAdapter.add(new Item(cBox, eText.getText().toString())) ;
    itemAdapter.notifyDataSetChanged() ;
    

    【讨论】:

      【解决方案2】:

      当您调用此方法时,您正在向传递给适配器的列表中添加一个新项目。

      itemArrayList.add(new Item(cBox, eText.getText().toString())) ;
              itemAdapter.notifyDataSetChanged() ;
      

      现在您不是在适配器内初始化列表引用,而是将其传递给父类。

      在适配器构造函数中添加这一行

      this.itemsArrayList = items;
      

      就是这样。

      【讨论】:

      • 感谢您的帮助,但它仍然无法正常工作。我无法理解问题...
      • 所以,我更详细地检查了您的代码,并注意到您在列表中添加视图而不是数据,这不应该这样做,您应该创建一组新数据,例如作为edittext的复选框状态和文本而不是视图本身,只有这样才能保持状态。
      • 好的,我按照你说的做了……但是当我按下添加按钮或点击手机的返回软键时,所有行仍然被清除……
      • 请在此处更新您的代码,否则我将无能为力。
      • 抱歉花了这么多时间...新项目现在只接受空字符串而不是整个视图。仍然没有区别:/
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2014-12-23
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多