单击按钮后,我的代码在表格中创建了一行,其中包含TextView,EditView和删除按钮。删除按钮被编程为删除表中的行(成功执行)和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 :(得分:0)
在
之后notes.delete(et.getText().toString()); // this does not seem to work
移动textview计数元素更新:
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
权限。