我正在尝试创建一个应用程序,我可以阅读通过Array存储的诗歌。 它包含2个按钮" Next" &安培; "返回"
一切似乎都运行良好但是当我到达阵列的末尾然后我按下它会停止工作。反之亦然。
package com.example.haziqsheikhlocal.ghanwapoems;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GhanwaPoems extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ghanwa_poems);
final TextView myPoem1 = (TextView) findViewById(R.id.myPoem);
final Button nextButton = (Button) findViewById(R.id.buttonNext);
final Button backButton = (Button) findViewById(R.id.backButton);
View.OnClickListener backListen = new View.OnClickListener() {
public int kK =0;
@Override
public void onClick(View v) {
String[] mPoemBooks = getStrings();
if (kK <= mPoemBooks.length - 1 || kK >= 1 ){
switch (v.getId()) // v is the button that was clicked
{
case(R.id.buttonNext):
kK++;
break;
case (R.id.backButton):
kK--;
break;
default: // this will run the same code for any button clicked that doesn't have id of button1 defined in xml
break;
}
int k = kK;
String myPoem = "";
myPoem = mPoemBooks[k];
myPoem1.setText(myPoem);
}
else {
Toast.makeText(getApplicationContext(), " Sorry No More To Show" , Toast.LENGTH_LONG).show();
}
}
};
backButton.setOnClickListener(backListen);
nextButton.setOnClickListener(backListen);
}
private String[] getStrings() {
return new String[]{"","a","b","c","d","e","f"};
}
}
我需要的是一个逻辑或条件,以便当我到达我的数组的末尾时,它应该显示&#34;抱歉没有更多的诗。&#34;反之亦然。
对于我第一次做某事时弄乱的代码感到抱歉:P
答案 0 :(得分:1)
你if语句总是如此。这应该适合你:
if(kK <= mPoemBooks.length - 1&amp;&amp; kK&gt; = 0){
答案 1 :(得分:0)
看起来你的索引超出了界限错误。这意味着您正在尝试访问不存在的数组(mPoemBooks)中的对象。在backButton的情况下,你最有可能得到-1作为索引,而使用buttonNext你得到的东西比数组大。
要解决这个问题,你应该确保索引在边界
int k = kK;
String myPoem = "";
if (k < 0) {
k = 0;
}
if (k >= mPoemBooks.length()) {
k = mPoemBooks.length() -1; //Subtract 1 since arrays are 0 indexed and length is 1 indexed.
}
myPoem = mPoemBooks[k];
答案 2 :(得分:0)
这里有很多需要注意的事情:
1:onCreate()方法只应用于为活动创建时需要发生的事情设置活动。在这里设置监听器很好,但是在那里定义你的匿名类是不好的代码实践
2:使用两个侦听器或使用布局中的onClick属性更简单
3:当前的诗是一个属于该活动的项目,因此它应该是该类的成员
4:我认为你会想要更好地持续存储你展示的物品,但这有几个步骤
这里是前3个的快速实现:
public class Poems extends Activity {
private int currentPoem;
private TextView mPoemView;
private String[] mPoems = new String[] {...}
@Override
protected void onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
setContentView(R.layout.activity_ghanwa_poems);
mPoemView = (TextView) findViewById(R.id.myPoem);
Button nextButton = (Button) findViewById(R.id.nextButton);
Button backButton = (Button) findViewById(R.id.backButton);
nextButton.setOnClickListener(mNextListener);
backButton.setOnClickListener(mBackListener);
}
private View.OnClickListerner mNextListener
= new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentPoem < mPoems.length - 1) {
mPoemView.setText(mPoems[++currentPoem])
}
}
};
private View.OnClickListerner mBackListener
= new View.OnClickListener() {
@Override
public void onClick(View v) {
if (currentPoem > 0) {
mPoemView.setText(mPoems[--currentPoem])
}
}
};
}
祝你好运,欢迎来编码。