2016-11-12 153 views
-1

點擊一個按鈕後,我的代碼在表格中用TextView,EditView和delete按鈕創建一行。刪除按鈕被編程爲刪除表中的行(成功)和Notes.class中的列表中的日期(這是問題)。要確認這個動作,我有一個文本視圖來計算列表大小與日期,當我按刪除按鈕什麼都沒有改變。 很抱歉,如果這是一個愚蠢的問題,但我真的很新的到Android :)如何通過單擊按鈕刪除一行數據?

的按鈕處理

public void addButtonHandler(View view) { 
    TableLayout tl = (TableLayout) findViewById(R.id.subs_table); 

    TableRow tr = new TableRow(this); 

    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

    //get the date in string 
    String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); 

    //the TextView with the date 
    TextView tv = new TextView(this); 
    tv.setText(date); 
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP , 18); 

    //editText with the type 
    final EditText et = new EditText(this); 
    TableRow.LayoutParams params = new TableRow.LayoutParams(getPx(50), ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.setMargins(getPx(130),0,getPx(40),0); 
    et.setLayoutParams(params); 

    SharedPreferences prefs = getSharedPreferences(SubsPREFERENCES , MODE_PRIVATE); // get notes object 
    Gson gsonN = new Gson(); 
    String jsonN = prefs.getString("DATES" , ""); 
    final Notes notes = gsonN.fromJson(jsonN , Notes.class); 


    //button for deleting row 
    ImageButton imageButton = new ImageButton(this); 
    TableRow.LayoutParams imageParams = new TableRow.LayoutParams(50,70); 
    imageParams.setMargins(5 ,0 ,0 ,0); 
    imageButton.setLayoutParams(imageParams); 
    imageButton.setImageResource(R.drawable.ok); 
    imageButton.setClickable(true); 
    imageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      ViewGroup gradnparent = (ViewGroup) parent.getParent(); 
      gradnparent.removeView(parent); 

      notes.delete(tv.getText().toString()); // this does not seem to work 
      } 
     } 
    ); 

    // Adding views in row 
    tr.addView(tv); 
    tr.addView(et); 
    tr.addView(imageButton); 

    // Adding row in table 
    tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT 
      , TableLayout.LayoutParams.WRAP_CONTENT)); 



    //add info in note 
    notes.addNote(date , et.getText().toString()); 

    // saving the changes in notes 
    SharedPreferences sharedPreferences =getSharedPreferences(SubsPREFERENCES ,MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    String json1 = gsonN.toJson(notes); 
    editor.putString("DATES" , json1); 
    editor.apply(); 

    //display the number of objects in list 
    TextView textView = (TextView) findViewById(R.id.textView); 
    textView.setText(String.valueOf(notes.getNum_dates())); 
    } 

該票據類

package com.uni.notes; 



public class Notes { 

private ArrayList<String> list =new ArrayList<String>(); 
private ArrayList<String> type =new ArrayList<String>(); 

public Notes() {} 

public int getNum_dates() {return list.size();} 

public void addNote(String date ,String typeN) { 

    list.add(date); 
    type.add(typeN); 
} 
public String getDate (int pos){ 
    return list.get(pos); 
} 

public String getType (int pos) {return type.get(pos);} 

public void delete (String thing) { 
    for (String element : list){ 
     if (element.equals(thing)){ 
      list.remove(element); 
     } 
    } 
} 

}

+0

如果您調試此操作,刪除方法是否接收您期望的輸入? – Randy

回答

0

剛過

0123你點擊刪除按鈕後

notes.delete(et.getText().toString()); // this does not seem to work 
//display the number of objects in list 
TextView textView = (TextView) findViewById(R.id.textView); 
textView.setText(String.valueOf(notes.getNum_dates())); 

這將更新計數Textview分辯:

notes.delete(et.getText().toString()); // this does not seem to work 

移動你的TextView計數單元的更新。

+0

是否解決了您的問題? – HelloSadness

+0

說實話,這不是我的問題,但我想出了你的意見 – Xantho