2015-06-16 35 views
0

我想用自己的成本添加產品列表,但是當我添加第二個產品時,第二個值會覆蓋第一個的值。 我能做些什麼?通過鍵盤向列表視圖插入數據並查看數據

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

回答

0

它很明顯會發生這種情況,因爲您每次都使用同一個HashMap實例,它將用相同的密鑰覆蓋以前的值。所以,如果你不想在寫,你必須創建點擊事件作爲內部的HashMap的另一個實例,

add_btn.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      personMap=new HashMap<String, String>(); 

      String str = et_main.getText().toString(); 
      String str2 = costo.getText().toString(); 
      personMap.put("name", str); 
      personMap.put("code", str2); 
      data.add(personMap); 
      .... 

此外,您必須刪除final聲明的HashMap,這樣就可以使用它再次在onClick()事件內

+0

好的謝謝!如果我想通過點擊刪除產品,我該怎麼辦? – Rubin

+0

ofcourse,你可以刪除ArrayList的位置 –

+0

是的,但我有一個問題,如何適配器是在onClick的內部我怎麼做與適配器notifyDataSetChanged()? – Rubin

0

您的personmap在onclick事件之外被聲明爲最終的,但是在那裏沒有任何用法。將聲明放入onclick事件中(而不是最終),並且每次將添加到您的數據對象時您都將創建一個新實例。目前你正在改變你以前的同一個實例。

0

我覺得問題在於HashMap。 當我們在HashMap中使用相同的鍵傳遞值時,它將覆蓋以前的值。

personMap=new HashMap<String, String>(); 

String str = et_main.getText().toString(); 
      String str2 = costo.getText().toString(); 
      personMap.put("name", str); 
      personMap.put("code", str2); 
      data.add(personMap); 
相關問題