2014-03-06 36 views
0

当我加入列表中的一个新的项目来查看它覆盖以前的one.This是我的代码。的ListView不保留以前的数据

package com.example.game; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class GuessActivity extends ListActivity implements OnClickListener 
{ 

EditText GNum; 
Button nxt; 
int[] a = new int[4]; 
int[] d = new int[4]; 
int t = 0; 
int x; 
ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> out = new HashMap<String, String>(); 
String KEY_NUM = "1" , KEY_BULLS = "2" , KEY_COWS = "3"; 


protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.guess_activity); 

    GNum = (EditText) findViewById(R.id.etGuessNum); 
    nxt = (Button) findViewById(R.id.btNxt); 
    int y = getIntent().getExtras().getInt("num"); 
    for(int i=0;i<4;i++) 
    { 
     a[i] = y%10; 
     y = y/10; 
    } 
    nxt.setOnClickListener(this); 
    GNum.setOnClickListener(this); 
} 



@Override 
public void onClick(View v) 
{ 
    if(v.getId() == R.id.btNxt) 
    { 
     String gN = GNum.getText().toString(); 
     int l = gN.length(); 
     int b=0; 
     int c=0; 
     if(l!=4) 
     { 
      Toast.makeText(getApplicationContext(),"Number should be of 4 digits", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 
     else 
     { 
      t++; 
      x = Integer.parseInt(gN); 


      for(int i=0;i<4;i++) 
      { 
       d[i] = x%10; 
       x = x/10; 
      } 
      if(t>8) 
      { 
       Toast.makeText(getApplicationContext(), "You Lost the Game", 
         Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      else 
      { 

       if(d[0] == a[0]) 
        b++; 
       if(d[0] == a[1] || d[0] == a[2] || d[0] == a[3]) 
        c++; 
       if(d[1] == a[1]) 
        b++; 
       if(d[1] == a[0] || d[1] == a[2] || d[1] == a[3]) 
        c++; 
       if(d[2] == a[2]) 
        b++; 
       if(d[2] == a[1] || d[2] == a[0] || d[2] == a[3]) 
        c++; 
       if(d[3] == a[3]) 
        b++; 
       if(d[3] == a[1] || d[3] == a[2] || d[3] == a[0]) 
        c++; 

       if(b == 4) 
        Toast.makeText(getApplicationContext(), "You Win", 
         Toast.LENGTH_SHORT).show(); 
       out.put(KEY_NUM, gN); 
       out.put(KEY_BULLS ,String.valueOf(b)); 
       out.put(KEY_COWS , String.valueOf(c)); 
       output.add(t-1, out); 

      } 
      ListAdapter adapter = new SimpleAdapter(this,output,R.layout.list_item, 
        new String[]{KEY_NUM , KEY_BULLS , KEY_COWS},new int[]{ 
        R.id.tvGuessNum , R.id.tvBulls , R.id.tvCows}); 
      setListAdapter(adapter); 

     } 
    } 
    else if(v.getId()==R.id.etGuessNum) 
    { 
     GNum.setText(""); 
    } 
} 
} 

这是我的输出。 http://imgur.com/orT91b http://imgur.com/gz2j7Ki 在此输出我只是想,明年的项目要更新,同时保持对同先前在列表中。 在此先感谢。

回答

0

每个用户点击按钮时,您正在创建一个新的适配器,这就是为什么以前的项目将被覆盖。

您应该创建onCreate方法内ListAdapter:

protected void onCreate(Bundle savedInstanceState) 
{ 
    <your code> 

    ListAdapter adapter = new SimpleAdapter(this,output,R.layout.list_item, 
       new String[]{KEY_NUM , KEY_BULLS , KEY_COWS},new int[]{ 
       R.id.tvGuessNum , R.id.tvBulls , R.id.tvCows}); 
    setListAdapter(adapter); 
} 

,然后删除相同的代码从的onClick methid