ListView不保留以前的数据

时间:2014-03-06 14:34:50

标签: android 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 在这个输出中,我只想要更新下一个项目,同时保持前一个项目在列表中相同。 提前致谢。

1 个答案:

答案 0 :(得分: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中删除相同的代码