English 中文(简体)
B. 甲状腺qui所需的帮助
原标题:Help needed for android quiz app
  • 时间:2010-06-17 10:02:24
  •  标签:
  • android

千年期问题是,只有最后一种选择案文在下顿点的点击上发生变化,其余部分保持不变。 左翼的想法有些问题。 一个人可以这样说。 谢谢。

此处为b类:

package com.myapps.quiz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper{

    //The Android s default system path of your application database.
    private static String DB_PATH = "/data/data/com.myapps.quiz/databases/";
    private static String DB_NAME = "quiz";
    private static String Table_name="Quiz";

    private SQLiteDatabase myDataBase; 
    private SQLiteDatabase myData; 
    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   



/**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();
        if(dbExist){
            //do nothing - database already exist
        }else{  
            CopyFiles();
        }
    }

    private void CopyFiles()
    {
        try
        { 
           InputStream is = myContext.getAssets().open(DB_NAME); 
           File outfile = new File(DB_PATH,DB_NAME);
           outfile.getParentFile().mkdirs();
           outfile.createNewFile();

          if (is == null)
          throw new RuntimeException("stream is null");
          else
          {
             FileOutputStream out = new FileOutputStream(outfile);      
          // BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outfile));
              byte buf[] = new byte[128];
                do {
              int numread = is.read(buf);
                    if (numread <= 0)
                        break;
              out.write(buf, 0, numread);
               } while (true);

                is.close();
                out.close();
          }
           //AssetFileDescriptor af = am.openFd("world_treasure_hunter_deluxe.apk");
        }
        catch (IOException e)
        {
              throw new RuntimeException(e); 
        }

    }    

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn t
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

        }

        if(checkDB != null){
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

            if(myDataBase != null)
                myDataBase.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }



      /// Get Book content////////
      public Cursor getQuiz_Content(int bookId)     
      {
        String myPath = DB_PATH + DB_NAME;
        myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);       

        Cursor cur;
        cur=myData.rawQuery("select quiz_text from Quiz where quiz_id= "+bookId+" ",null);
        cur.moveToFirst();

        myData.close();
        return cur;
      };
      //////////////////////////



      /// Get Book content////////
      public Cursor getQuiz_List()     
      {
        String myPath = DB_PATH + DB_NAME;
        myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);       
        int i;

        Cursor cur;
        cur=myData.rawQuery("select quiz_id,quiz_text,correct_answer from quiz",null);
        cur.moveToFirst();
        i = cur.getCount();
        myData.close();
        return cur;
      };
      //////////////////////////


      public Cursor getAns(int quizid)
      {
          String myPath = DB_PATH + DB_NAME;
          myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

          Cursor cur;
          cur = myData.rawQuery("select answers from answer where quiz_id= "+quizid+" ", null);
          cur.moveToFirst();
          myData.close();
          return cur;
      }

      public Cursor getAnsList()
      {
          String myPath = DB_PATH + DB_NAME;
          myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

          Cursor cur;
          cur = myData.rawQuery("select answers from answer", null);
          cur.moveToFirst();
          myData.close();

          return cur;  
      }



      //---updates a title---
     /* public boolean UpdateFavourite_Individual(long rowid,String fav) 
      {
        String myPath = DB_PATH + DB_NAME;
        myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

          ContentValues args = new ContentValues();
          args.put("bookmark", fav);
          return myData.update("lyrics", args, 
                           "rowid=" + rowid, null) > 0;                     
      }*/
      //////////////////


}

页: 1

    package com.myapps.quiz;

import java.io.IOException;



import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Quiz extends Activity{
    /** Called when the activity is first created. */
    final DataBaseHelper db = new DataBaseHelper(this);

    Cursor c3;
    Cursor c4;
    int counter=1;  
    String label;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String options[] = new String[19];
     // get reference to radio group in layout
        RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
        // layout params to use when adding each radio button
        LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                RadioGroup.LayoutParams.WRAP_CONTENT,
                RadioGroup.LayoutParams.WRAP_CONTENT);


        c4 = db.getAnsList();
        c4.moveToFirst();
        for (int k=0;k<19;k++)
        {       
            options[k]=c4.getString(0);
            c4.moveToNext();
            Log.e("value:", options[k]);
        }

        for (int i = 0; i < 4; i++){
            final RadioButton newRadioButton = new RadioButton(this);
            c3 = db.getAns(3);

        for (int j=0;j<i;j++)    
            c3.moveToNext();
           label = c3.getString(0);

        newRadioButton.setText(label);
        newRadioButton.setId(6);
        radiogroup.addView(newRadioButton, layoutParams);
        //getOptions();



        try {
            db.createDataBase();
            db.openDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final Cursor c1;

        c1= db.getQuiz_Content(counter);






        //rdoAns1.setText("Lisbon");

        final TextView tvQuizText = (TextView)findViewById(R.id.TextView01);
        tvQuizText.setText(c1.getString(0));

        Button btnNext = (Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {


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

                    Cursor c2 = null ;
                    Cursor c3 =null;    

                    if (counter < 5)
                    {
                    counter +=1;    
                    c2 = db.getQuiz_Content(counter);
                    c3 = db.getAns(counter);
                    //Log.e("Row Position after increment:",Integer.toString(c2.getPosition()));
                    tvQuizText.setText(c2.getString(0));
                    }



                    for (int j=0;j<3;j++)    
                    {   
                        c3.moveToNext();
                       label = c3.getString(0);

                    newRadioButton.setText(label);

                    }

                    //Log.e("value:",Integer.toString(counter));
                    //Log.e("value",c3.getString(0));
            }
        });
        //c3 = db.getAns(4);
     // rdoAns1.setText(c3.getString(0));
      //rdoAns2.setText(c3.getString(1));
      //rdoAns3.setText(c3.getString(2));
      //rdoAns4.setText(c3.getString(3));
    }
    /** add radio buttons to the group */
   // private void getOptions()
    {

        // get reference to radio group in layout
        //RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
        // layout params to use when adding each radio button
        //LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
          //      RadioGroup.LayoutParams.WRAP_CONTENT,
            //    RadioGroup.LayoutParams.WRAP_CONTENT);

        // add 20 radio buttons to the group

        //for (int i = 0; i < 4; i++){
          //  RadioButton newRadioButton = new RadioButton(this);
           // c3 = db.getAns(4);
        //for (int j=0;j<i;j++)    
          //  c3.moveToNext();
           // String label = c3.getString(0);

        //newRadioButton.setText(label);
        //newRadioButton.setId(i);
        //radiogroup.addView(newRadioButton, layoutParams);
        }

    }

    }

我的XML规定:

<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">


                <RadioGroup
                android:id="@+id/rdbGp1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_y="30dip"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                >
                </RadioGroup>


<Button android:text="Save" android:id="@+id/btnSave" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="50dip" android:layout_y="250dip"></Button>
<Button android:text="Next" android:id="@+id/btnNext" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="175dip" android:layout_y="250dip"></Button>

<TextView android:layout_x="17dip" android:layout_y="15dip" android:id="@+id/TextView01" android:layout_width="wrap_content" android:text="@+id/TextView01" android:layout_height="wrap_content"></TextView>






</AbsoluteLayout>
最佳回答

如果我放弃这一权利,而且该守则与所有这些可悲的方括号几乎混淆不清,那么你就会建立布特顿电台,以防 lo? 你创建了新的RadioButton,并增加了布局。 之后,你设立了新的RadioButton,这是你创建的最后一个。 当你试图在新RadioButton上设定案文时,你唯一做的是在同一无线电台上设定文字。

因此,你们必须利用各自的广播小组,单独收听Button电台。

我希望这有所帮助。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签