listview只能得到我的数组中的一个东西

时间:2013-07-07 19:28:43

标签: android listview

我只得到test30作为输出我的失败?

MainActivity:

public class MainActivity extends Activity {
   ListView listview;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       listview = (ListView) findViewById(R.id.listView);

       Product[] items = {
               new Product("test1",07,07,2013),
               new Product("test2",07,07,2013),
               new Product("test3",07,07,2013),
       };

       ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
               android.R.layout.simple_list_item_1, items);

    listview.setAdapter(adapter);
       listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position,
                                   long id) {
               String item = ((TextView)view).getText().toString();
               Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
           }
       });
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }     
}

Product.java

public class Product {
    static String name;
    static int day;
    static int month;
    static int year;
    static String res; 

    public Product(){
        super();
    }

    public Product(String name, int day, int month, int year) {
        super();
        this.name = name;
        this.day = day;
        this.month = month;
        this.year = year;

        Calendar thatDay = Calendar.getInstance();
        thatDay.set(Calendar.DAY_OF_MONTH,this.day);
        thatDay.set(Calendar.MONTH,this.month-1); // 0-11 so 1 less
        thatDay.set(Calendar.YEAR, this.year);

        Calendar today = Calendar.getInstance();

        long diff =thatDay.getTimeInMillis()- today.getTimeInMillis(); //result in millis
        long days = diff / (24 * 60 * 60 * 1000);
            res=String.valueOf(days);
        }

    @Override
    public String toString() {
        return this.name+this.res ;
    }
}

2 个答案:

答案 0 :(得分:0)

将源数组移出onCreate()。你得到“test30”,因为你的res为零。

此外,您不需要像{1}}那样使用this.。我不知道你可能想要避免范围冲突,但是android上的常见模式只是为类成员添加m前缀,所以不要使用:

static String name;

你有

static String mName;

答案 1 :(得分:0)

你的错误是,(我从上面发布的代码中读到的)所有字段都是静态的(意味着它们每个类一次,而不是每个对象一次)。这使得你在onCreate中调用的构造函数调用毫无用处,因为每个产品都有一个唯一的名称,每次都会覆盖Product.name。

       Product[] items = {
           new Product("test1",07,07,2013), // Product.name = "test1"
           new Product("test2",07,07,2013), // Product.name = "test2"
           new Product("test3",07,07,2013), // Product.name = "test3"
       }

让您的字段非静态(可能是私有的),您的问题应该消失。

edit1:有关静态/非静态字段的进一步阅读,请转至http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

edit2:名称背后的零应符合预期,因为今天和今天之间的差异为零,你应该用不同的日子进行测试; - )