【问题标题】:Difficulty displaying list of random leap years in ListView upon a Button Click in Android Studio在 Android Studio 中单击按钮时,难以在 ListView 中显示随机闰年列表
【发布时间】:2016-02-22 03:10:57
【问题描述】:

在我的 MainActivity.java 上,我编写了从 1800 年(含)到 2015 年(含)的闰年列表。但是,我被困在用户单击“生成”按钮并且闰年列表显示在ListView中的部分。
我相信真正的问题在于我的 populate() 方法,因为这只是我不太确定的部分。因为到目前为止我还没有处理过多的 ArrayAdapter 或 ArrayList。
注意:我没有收到任何编译或运行时错误,我遗漏了一些逻辑部分。应用程序成功启动,但“生成按钮”没有明显执行任何操作。

参考我的代码如下: MainActivity.java

package net.androidbootcamp.gregorianspeeddating;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    Button btnGen, btnMan;
    ListView listView;
    Integer randomNum;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    listView = (ListView) findViewById(R.id.yearsListView);

    //Button Generate that creates a ListView of Leap Year on MainActivity
    btnGen = (Button) findViewById(R.id.btnGen);
    btnGen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random r = new Random();
            randomNum = r.nextInt(2016 - 1800) + 1800;

            if (isLeapYear(randomNum)) {
                populate();
            }


        }
    });
    // Button Manipulate that takes user to another activity
    btnMan = (Button) findViewById(R.id.btnMan);
    btnMan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, Manipulate.class));
        }
    });


}

// Returns true if given Integer is a Leap Year.
public boolean isLeapYear(Integer year) {
    boolean b = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
    if (!b) {
        return false;
    }
    return true;
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

// Method To Populate ListView with random Leap Year
public void populate() {
    ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this,  android.R.layout.simple_list_item_1, randomNum);
    listView.setAdapter(adapter);
    }
}

content_main.xml,其中包含 'Generate' 和 'ListView' 按钮 --> 这在 activity_main.xml 中显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="net.androidbootcamp.gregorianspeeddating.MainActivity"
    tools:showIn="@layout/activity_main">

<ListView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/yearsListView"
    android:layout_weight="1"
    android:dividerHeight="13dp"
    android:divider="@android:color/transparent"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Generate"
    android:id="@+id/btnGen"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="28dp"
    android:layout_marginStart="28dp"
    android:layout_marginBottom="60dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Manipulate"
    android:id="@+id/btnMan"
    android:layout_marginRight="28dp"
    android:layout_marginEnd="33dp"
    android:layout_alignTop="@+id/btnGen"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

【问题讨论】:

    标签: java android listview android-studio


    【解决方案1】:

    为了显示随机闰年列表,您需要为列表视图提供年份列表,而不是单个年份。在 Button 的 onClick 方法中,您随机生成一个介于 1800 和 2015 之间的数字,而不是生成随机数字列表。

    你要做的是:

    1. 不要使用Integer randomNum;,而是使用List&lt;Integer&gt; randomNums;
    2. 使用变量int count = 20; // 使用变量计数来跟踪要生成的闰年数
    3. 在按钮的onClick方法中,使用

      Random rand = new Random(); randomNums = new ArrayList<Integer>(); while(randomNums.size != count){ randomNums.add(rand.nextInt(2016 - 1800) + 1800); } populate()

      这个 while 循环可以一直持续下去,直到找到所需的随机数计数。

    4. 最后在填充方法中使用数组 randomNums

      ArrayAdapter&lt;Integer&gt; adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, randomNums);

    一切顺利:)

    【讨论】:

    • 它大部分是正确的,只是它不验证给定的数字是否是闰年,因为 % 等运算符不支持 List randomNums。不过还是非常感谢您的帮助!!!你能建议我解决这个问题吗?
    • 好的。更改 while 循环:while(randomNums.size() != count){ if(isLeapYear(rand.nextInt(2016 - 1800) + 1800)){randomNums.add(rand.nextInt(2016 - 1800) + 1800 )}; }
    【解决方案2】:

    我还没有运行您的代码,但我可以在您的 onClick 处理程序中看到一个问题。在这种情况下:

    if (isLeapYear(randomNum)) {
        populate();
    }
    

    如果生成的随机年份是闰年,您实际上只是在调用填充。从您的代码看来,您只是在生成一个随机数,然后创建一个包含 0 或 1 个条目的列表(如果不是闰年,则为 0,如果是,则为 1)。因此,您在该列表中拥有任何项目的机会只有四分之一。

    如果您发布您想要的结果会有所帮助。例如:您是否要按顺序列出从 1800 年开始到 2015 年结束的所有闰年?那么,为什么要生成随机数呢?

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多