سؤال

In my project, im going to make a multiple choice type question. I can select the questions and answer from mysql, however, i cant do the "matching" of the answer between input answer and the data from mysql. I tried some solutions but they doesnt work as well. I need a big help on it. Below are my codes.

the java class that make multiple choice

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.normalmode);

new DataAsyncTask().execute();

}

public void onClick(View v){

    inputtext = ((Button) v).getText().toString();

    tv3 = (TextView)findViewById(R.id.textView3);
    tv3.setText(inputtext);
    if(inputtext == tv2.getText().toString()){
        playerscore +=5;
    } else {
        playerscore +=1;
    }
    score.setText("Scores : " + playerscore);

    new DataAsyncTask().execute();
}
class DataAsyncTask extends AsyncTask<String, String, String>{
    @Override
    protected String doInBackground(String... param) {
        String result = DBConnector.executeQuery("SELECT * FROM questions ORDER BY RAND( ) LIMIT 1");
        return result;
    }
    @Override
    protected void onPostExecute(String result) {
        question = (TextView)findViewById(R.id.textView_question);
        fchoice = (Button)findViewById(R.id.Btn_choice1);
        schoice = (Button)findViewById(R.id.Btn_choice2);
        tchoice = (Button)findViewById(R.id.Btn_choice3);
        tv2 = (TextView)findViewById(R.id.textView2);
        score = (TextView)findViewById(R.id.textView_score);
        try {               
            JSONArray jsonArray = new JSONArray(result);
            JSONObject jsonData = jsonArray.getJSONObject(0);
            question.setText(jsonData.getString("q_desc"));
            fchoice.setText(jsonData.getString("fchoice"));
            schoice.setText(jsonData.getString("schoice"));
            tchoice.setText(jsonData.getString("tchoice"));             
            ans = jsonData.getString("q_ans");
            tv2.setText(ans);               
        } catch(Exception e) {
            Log.e("log_tag", e.toString());
        }           
    }
}
} 

three buttons are sharing same onClick in xml file by using

 android:onClick="onClick"

the current problem i faced is that it always return "false" no matter i pressed which button. also, is there any way to store/pass "previous" async task data? I have tried using internal storage but it doesnt work as well

EDIT: my xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.95" >



    <TextView
        android:id="@+id/textView_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/player_score" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.95" >

    <TextView
        android:id="@+id/textView_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/string_question"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/Btn_choice3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/third_choice" 
        android:onClick="onClick" />

    <Button
        android:id="@+id/Btn_choice2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Btn_submit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/second_choice"
        android:onClick="onClick" />

    <Button
        android:id="@+id/Btn_choice1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/Btn_choice2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/first_choice"
        android:onClick="onClick" />

</RelativeLayout>

<RelativeLayout
    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:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" />

</RelativeLayout>

</LinearLayout

textview2 and textview3 are used to test my output and display them as text

هل كانت مفيدة؟

المحلول

try this if(inputtext.equals(tv2.getText().toString())) instead of if(inputtext == tv2.getText().toString())

becasue the == operator checks to see if the two strings are exactly the same object.

The .equals() method will check if the two strings have the same value.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top