gpt4 book ai didi

android - 如何将以下代码分解为android中的各个类

转载 作者:行者123 更新时间:2023-11-30 03:12:06 24 4
gpt4 key购买 nike

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

8年前关闭。




Improve this question




我有以下代码有效并为我提供了我正在寻找的解决方案,但是我想做的是将以下部分分解为我自己的类,以便我可以在应用程序启动时初始化数组:

            String[] questions = getResources().getStringArray(R.array.questions);
List<String> qlist = new ArrayList<String>();
for(String s: questions){
qlist.add(s);
}
questioncount = qlist.size();
Collections.shuffle(qlist);
int cr = (r.nextInt(questioncount - 0) + 0);
ques.setText(qlist.get(cr).toString());
Log.i("VertygoEclypse", qlist.get(cr).toString());

话虽这么说,代码目前在我的主要 Activity 的主要 OnCreate 类中,我想要将它分解为它自己的类。这将使我能够将上述代码分成 2 个部分,一个用于初始化部分,另一个用于随机选择部分,这将使我能够随机选择问题并将其设置为我的应用程序中的 textview 字段。我希望有这样的代码:
    String[] questions = getResources().getStringArray(R.array.questions);
List<String> qlist = new ArrayList<String>();
for(String s: questions){
qlist.add(s);
}
questioncount = qlist.size();
Collections.shuffle(qlist);

尚未开发的代码将被称为 public getquestion() 将包含:
    int cr = (r.nextInt(questioncount - 0) + 0);
ques.setText(qlist.get(cr).toString());

但它不是一个计数器,而是一个随机选择,其中一部分是从随机数组中消除所选项目,以便下次不会选择它。

我的目标是有一个测验应用程序,它将从我的字符串文件中提取一个字符串数组,并在随机选择时设置 TextView 以给出这些问题的分数。
以下是我到目前为止开发的 xml:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="30dip"
android:text="TextView" />

<RadioGroup
android:id="@+id/rbg_answergroup"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginBottom="30dip"
android:layout_gravity="center">

<RadioButton
android:id="@+id/rb_dnm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Does Not Matter" />

<RadioButton
android:id="@+id/rb_att"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All the Time" />

<RadioButton
android:id="@+id/rb_st"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sometimes" />

<RadioButton
android:id="@+id/rb_naa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not at All" />
</RadioGroup>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/previousquestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="33"/>

<Button
android:id="@+id/answerbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="33"/>

<Button
android:id="@+id/nextquestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="33" />

</TableRow>
</TableLayout>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Total"
android:paddingRight="25dip" />

<TextView
android:id="@+id/runningtotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />

</TableRow>

以下是 string.xml 文件:
    <?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">quiztest</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="questions">
<item>Are you Drunk? </item>
<item>Do you currently love everyone?</item>
<item>Do you currently love your job?</item>
<item>Do you prefer Beers over Rum?</item>
<item>Do you currently wish to tell off your boss?</item>
<item>Feel like taking a picture and sending it to someone?</item>
<item>Spin around and when you stop are things spinning?</item>
<item>Can you walk in a straight line?</item>
<item>Does 1 + 1 = 11?</item>
<item>Can you explain why "42" is the Secret to Life?</item>
<item>How do you feel about checking out George Street for a Good Time?</item>
<item>When you see a striped pants does your heart skip a beat?</item>
<item>Can you say for sure that bad thoughts have not passed through your head about a co-worker?</item>
<item>When is a good time to Drink?</item>
<item>Can you only have 1 beer.</item>
<item>Does the words 1-z mean just one beer?</item>
<item>Can you say for sure that you have never instigated a Drinking lime?</item>
<item>Given the opportunity to go home with a complete stranger will you?</item>
<item>How much do you think that you can be a designated driver</item>
<item>Given the chance to consumate with Sus scrofa domesticus will you?</item>
<item>If real "HONGREE" will you eat dogfood?</item>
</string-array>

非常感谢任何关于上述内容的指导我已经尝试过,但是当我将上面的代码移动到一个单独的类并尝试只返回一个字符串值时,总是遇到一个空指针问题。

以下是我目前拥有的代码:
    package com.vertygoeclypse.quiztest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView ques;
TextView rt;
Button nxtqstn;
Button prvsqstn;
Button sbtanswr;
RadioGroup arg;
int answertotal = 0;
int rdochkid = -1;
Random r = new Random();
int questioncount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nxtqstn = (Button) findViewById(R.id.nextquestion);
prvsqstn = (Button)findViewById(R.id.previousquestion);
sbtanswr = (Button)findViewById(R.id.answerbutton);
arg = (RadioGroup)findViewById(R.id.rbg_answergroup);
ques = (TextView) findViewById(R.id.textView1);
rt = (TextView)findViewById(R.id.runningtotal);
arg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
rdochkid = checkedId;
}
});
prvsqstn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});
nxtqstn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
String[] questions = getResources().getStringArray(R.array.questions);
List<String> qlist = new ArrayList<String>();
for(String s: questions){
qlist.add(s);
}
questioncount = qlist.size();
Collections.shuffle(qlist);
int cr = (r.nextInt(questioncount - 0) + 0);
ques.setText(qlist.get(cr).toString());
Log.i("VertygoEclypse", qlist.get(cr).toString());
}
public void AnswerCheck(){
if(rdochkid == -1){
rdochkid = arg.getId();
}
if(rdochkid == R.id.rb_dnm){
answertotal = answertotal+10;
}
if(rdochkid == R.id.rb_att){
answertotal = answertotal+5;
}
if(rdochkid == R.id.rb_st){
answertotal = answertotal+3;
}
if(rdochkid == R.id.rb_naa){
answertotal = answertotal+1;
}
}
public void resetquiz(){
answertotal = 0;
rdochkid = -1;
}

}

我相信我的问题是当我移动这段代码时:
    String[] questions = getResources().getStringArray(R.array.questions);
List<String> qlist = new ArrayList<String>();
for(String s: questions){
qlist.add(s);
}
questioncount = qlist.size();
Collections.shuffle(qlist);

我无法返回 qlist,以便我可以提取随机器将选择的字符串。

再一次,任何帮助将不胜感激。

问候

钦乔伊

编辑 - 25/12/2013 - 晚上 8:30
在上述之后,我一直试图让突破继续下去,我已经达到了这一点。
我已经分解了适配器代码,这就是它的样子:
    package com.vertygoeclypse.quiztest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.util.Log;

public class QuizAdapter {
//---------------------------------------------------------------------------------------------------------------/
String[] questions = null;
List<String> qlist = null;
private int questioncount;
Random r = new Random();
int cr;
Context context;
//---------------------------------------------------------------------------------------------------------------/

QuizAdapter(Context context){
Log.i("QuizAdapter", "We Are Here - 1");
this.context = context;
}
//---------------------------------------------------------------------------------------------------------------/
void initializequestions(){
Log.i("QuizAdapter", "We Are Here - 2");
questions = context.getResources().getStringArray(R.array.questions);
Log.i("QuizAdapter", "We Are Here - 3");
qlist = new ArrayList<String>();
Log.i("QuizAdapter", "We Are Here - 4");
for(String s: questions){
Log.i("QuizAdapter", "We Are Here - 5");
qlist.add(s);
}
questioncount = qlist.size();
Collections.shuffle(qlist);
cr = (r.nextInt(questioncount - 0) + 0);
// ques.setText(qlist.get(cr).toString());
for(int counter = 0; counter < 20; counter++){
Log.i("VertygoEclypse", qlist.get(counter).toString());
}
}
//---------------------------------------------------------------------------------------------------------------/
public List<String> getquestions(List<String> qlist){
return qlist;

}
}

在主要 Activity 中,我将代码如下:
    Context context;
QuizAdapter qa = new QuizAdapter(context);

然后:
            Log.i("VertygoEclypse", "Just before Null Pointer");
qa.initializequestions();
Log.i("VertygoEclypse", "Just after Null Pointer");

另请参阅下面的 logcat 输出,我看到系统进入该方法并在此时以空指针陷入困境:
                Log.i("QuizAdapter", "We Are Here - 2");
questions = context.getResources().getStringArray(R.array.questions);
Log.i("QuizAdapter", "We Are Here - 3");

请查看 logcat 输出:
    12-26 00:24:46.247: I/QuizAdapter(1628): We Are Here - 1
12-26 00:24:46.977: I/VertygoEclypse(1628): Just before Null Pointer
12-26 00:24:46.977: I/QuizAdapter(1628): We Are Here - 2
12-26 00:24:46.977: D/AndroidRuntime(1628): Shutting down VM
12-26 00:24:46.977: W/dalvikvm(1628): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-26 00:24:46.987: E/AndroidRuntime(1628): FATAL EXCEPTION: main
12-26 00:24:46.987: E/AndroidRuntime(1628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vertygoeclypse.quiztest/com.vertygoeclypse.quiztest.MainActivity}: java.lang.NullPointerException
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.os.Looper.loop(Looper.java:137)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-26 00:24:46.987: E/AndroidRuntime(1628): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 00:24:46.987: E/AndroidRuntime(1628): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 00:24:46.987: E/AndroidRuntime(1628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 00:24:46.987: E/AndroidRuntime(1628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 00:24:46.987: E/AndroidRuntime(1628): at dalvik.system.NativeStart.main(Native Method)
12-26 00:24:46.987: E/AndroidRuntime(1628): Caused by: java.lang.NullPointerException
12-26 00:24:46.987: E/AndroidRuntime(1628): at com.vertygoeclypse.quiztest.QuizAdapter.initializequestions(QuizAdapter.java:28)
12-26 00:24:46.987: E/AndroidRuntime(1628): at com.vertygoeclypse.quiztest.MainActivity.onCreate(MainActivity.java:68)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.Activity.performCreate(Activity.java:5104)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 00:24:46.987: E/AndroidRuntime(1628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

我想我越来越近了,但只需要一点指导,任何帮助将不胜感激。

问候

钦乔伊

最佳答案

下面是一个简单的例子。请原谅我在没有安装 SDK 的情况下输入记事本++ 时出现的错误。

像下面的例子应该可以工作。注意有一些问题,例如:

  • 问题可以重复使用吗?

  • 下面的示例允许某人不重复使用问题,在这种情况下,在某些时候所有问题都已被使用并且必须调用 resetList()。您可以获取以下类的固定版本并将其输入到您的 android 代码中。

    此外,您可以优化以保留原始问题列表和副本,以便您无需在 restList() 方法中重新读取文件。
    public class Questions
    {

    private List<String> qlist;

    public Questions()
    {
    resetList()
    }

    public void resetList()
    {
    String[] questions = getResources().getStringArray(R.array.questions);
    qlist = new ArrayList<String>();
    for(String s: questions){
    qlist.add(s);
    }
    Collections.shuffle(qlist);
    }

    public int getQuestionCount()
    {
    return qlist.size();
    }

    public String getRandomQuestion(boolean deleteIt) //deleteIt means we do not show this again
    {
    if(qlist.size()==0) //No more questions
    return "No more questions";

    if(deleteIt)
    return qlist.removeElement(qlist.size()-1) //However method is called remove/pop/etc...

    return qlist.get(Math.Random()*(qlist.size()-0.1)).toString(); //or some other random generation
    }
    }

    调用 getRandomQuestion(true)删除提供的问题。使用 false相反,意味着可以多次提供相同的问题。

    酌情修复错误 - 希望这会有所帮助!

    关于android - 如何将以下代码分解为android中的各个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20776207/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com