添加新行时,将清除所有ListView项

时间:2017-07-19 06:45:15

标签: android android-layout listview android-adapter

我有一个包含复选框和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() ;
        }
    });
}
}

这是我的Adapter类:

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

Item类:

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

2 个答案:

答案 0 :(得分:0)

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

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

现在在按钮中单击

添加如下新项目
itemAdapter.add(new Item(cBox, eText.getText().toString())) ;
itemAdapter.notifyDataSetChanged() ;

答案 1 :(得分:0)

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

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

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

在适配器构造函数

中添加此行
this.itemsArrayList = items;

是的。