[英]Difficulty displaying list of random leap years in ListView upon a Button Click in Android Studio
On my MainActivity.java, I have written code for creating a list of Leap Year till 2015 (inclusive) from 1800 (inclusive). 在我的MainActivity.java中,我编写了用于创建到1800年(含)到2015年(含)的of年列表的代码。 However, I am stuck at the part on where a user clicks 'Generate' button and List of Leap Year is displayed in ListView .
但是,我停留在用户单击“生成”按钮并在ListView中显示Le年列表的部分。
I believe the real problem is with my populate() method because that's only part I am not so sure of. 我相信真正的问题出在我的populate()方法上,因为那只是我不确定的一部分。 Because I haven't dealt with ArrayAdapter or ArrayList much so far.
因为到目前为止我还没有处理ArrayAdapter或ArrayList。
NOTE: I don't get any compiling or run time error, I am missing some logical part. 注意:我没有任何编译或运行时错误,我缺少一些逻辑部分。 The app launches succesfully, but Generate Button doesn't do anything visibly.
该应用程序成功启动,但是“生成按钮”没有明显执行任何操作。
Refer to my code Below: MainActivity.java 请参阅下面的代码: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 which contains Button 'Generate' and 'ListView' --> This is displayed in activity_main.xml 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>
In order to display a list of random leap years, you need to give your listview a list of years rather than a single year. 为了显示随机leap年的列表,您需要给listview一个年份列表,而不是单个年份。 In the onClick method of your Button you randomly generate one number between 1800 and 2015 rather than generating a list of random numbers.
在Button的onClick方法中,您会随机生成一个介于1800和2015之间的数字,而不是生成一个随机数列表。
What you have to do is: 您要做的是:
Integer randomNum;
Integer randomNum;
, use List<Integer> randomNums;
List<Integer> randomNums;
int count = 20;
int count = 20;
// use a variable count to keep track of the number of leap years to generate In the onClick method of the button, use 在按钮的onClick方法中,使用
Random rand = new Random(); randomNums = new ArrayList<Integer>(); while(randomNums.size != count){ randomNums.add(rand.nextInt(2016 - 1800) + 1800); } populate()
This while loop can go on and on until you find the required count of random numbers. 这种while循环可以不断进行,直到找到所需的随机数计数为止。
Finally in the populate method use the array randomNums 最后,在填充方法中,使用数组randomNums
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, randomNums);
All the best :) 祝一切顺利 :)
I haven't run your code yet, but I can see one problem in your onClick handler. 我还没有运行您的代码,但是我可以在您的onClick处理程序中看到一个问题。 In this condition:
在这种情况下:
if (isLeapYear(randomNum)) {
populate();
}
you are really only calling populate if the random year that was generated is a leap year. 如果生成的随机年份是a年,则您实际上只是在呼叫填充。 From your code it looks like you are simply generating one random number and then creating a list with 0 or 1 entries (0 if it's not a leap year and 1 if it is).
从您的代码看来,您只是在生成一个随机数,然后创建包含0或1个条目的列表(如果不是a年则为0,如果是a年则为1)。 So the chances of you having any items in that list is only 1 in 4.
因此,您在该列表中包含任何项目的机会只有4分之一。
It would help if you post your desired outcome. 如果您发布期望的结果,将会有所帮助。 For example: are you trying to list ALL years starting in 1800 and ending in 2015 that are leap years in order?
例如:您是否要按顺序列出从1800年开始到2015年结束的所有leap年? In that case, why do you need to generate a random number?
在这种情况下,为什么需要生成一个随机数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.