2017-07-19 49 views
0

我有一个包含复选框和EditText字段的列表项的自定义布局。点击添加按钮后,尽管新项目正在添加,但清单已被清除。所有以前的数据都消失了。我是新至Android所以请让我知道我错了......添加新行时,所有ListView项目都会被清除

这里是我的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; 
} 
} 

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

回答

0

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

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

现在你按钮,如下添加新项单击

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

我试过这个,但它仍然不工作.. – heytherebrowncow

0

当你调用这个方法,要添加一个新的项目,你传递给列表适配器。

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

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

加入这一行适配器构造

this.itemsArrayList = items; 

就是这样里面。

+0

感谢您的帮助,但它仍然无法正常工作。我无法理解这个问题... – heytherebrowncow

+0

所以,我仔细检查了一下你的代码,并且注意到你在列表中添加了视图而不是数据,这不应该完成,你应该创建一组新的数据,如复选框状态和编辑文本的文本而不是视图本身,只有这样才能保持状态。 –

+0

好的,我照你说的做了...但是当我按下添加按钮或当我按下手机的返回软键时,所有的行仍然被清除... – heytherebrowncow

相关问题