我想用自己的成本添加產品列表,但是當我添加第二個產品時,第二個值會覆蓋第一個的值。 我能做些什麼?通過鍵盤向列表視圖插入數據並查看數據
Spesa.java
public class Spesa extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spesa_layout);
final Button add_btn = (Button)findViewById(R.id.btn_add);
final ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
final HashMap<String,String> personMap=new HashMap<String, String>();
final EditText et_main = (EditText)findViewById(R.id.et_main);
final EditText costo= (EditText)findViewById(R.id.costo);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = et_main.getText().toString();
String str2 = costo.getText().toString();
personMap.put("name", str);
personMap.put("code", str2);
data.add(personMap);
String[] from = {"name", "code"};
int[] to = {R.id.textView1, R.id.textView3};
SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), data,R.layout.row,from,to);
//Setting Adapter to ListView
((ListView)findViewById(R.id.list)).setAdapter(adapter);
}});}}
我怎樣才能通過單擊刪除的產品?
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(Spesa.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
data.remove(positionToRemove);
adapter.notifyDataSetChanged();// i don't know how to do this
}
});
adb.show();
}
});
好的謝謝!如果我想通過點擊刪除產品,我該怎麼辦? – Rubin
ofcourse,你可以刪除ArrayList的位置 –
是的,但我有一個問題,如何適配器是在onClick的內部我怎麼做與適配器notifyDataSetChanged()? – Rubin