2017-07-19 48 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

相關問題