2016-05-03 10 views
1

私はparse.comバックエンドを持つクロスワードパズルを開発しようとしています。私はコードを変換するのが難しいです。 以下のコードは私が作成したデータベーステーブルです。parse.comクエリからsqliteクエリ

public class puzzle extends SQLiteOpenHelper { 

public static final String db_name = "crossword.db"; 
public static final String Table1_name = "puz"; 
public static final String Table2_name = "item"; 
public static final String Table3_name = "clue"; 

public puzzle(final Context context){ 
    super(context, db_name, null, 1); 

} 



@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + Table1_name + "hash INTEGER, title TEXT, author TEXT,copyright TEXT,height INTEGER ,width INTEGER"); 
    db.execSQL("CREATE TABLE IF NOT EXISTS"+ Table2_name + "hash INTEGER, x INTEGER ,y INTEGER, cell position INTEGER , black BOOLEAN , letter TEXT"); 
    db.execSQL("CREATE TABLE IF NOT EXISTS" + Table3_name + "hash INTEGER, x INTEGER ,y INTEGER, cellposition INTEGER, cluenumber INTEGER, position INTEGER,clue TEXT"); 


} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS" + Table1_name); 
    db.execSQL("DROP TABLE IF EXISTS" + Table2_name); 
    db.execSQL("DROP TABLE IF EXISTS" + Table3_name); 
    onCreate(db); 


} 

public boolean insertitem(int hash, int x, int y, int cellposition, boolean black, String letter) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues itemvalues = new ContentValues(); 
    itemvalues.put("hash", hash); 
    itemvalues.put("X", x); 
    itemvalues.put("Y", y); 
    itemvalues.put("CellPosition", cellposition); 
    itemvalues.put("Black", black); 
    itemvalues.put("Letter", letter); 
    long itemresult = db.insert(Table2_name, null, itemvalues); 
    if (itemresult == -1) 
     return false; 
    else 
     return true; 

} 
public boolean isinsertpuz(int hash,String title,String author,String copyright,int height,int width) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues puzvalues = new ContentValues(); 
    puzvalues.put("hash",hash); 
    puzvalues.put("Title",title); 
    puzvalues.put("author",author); 
    puzvalues.put("copyright",copyright); 
    puzvalues.put("Height",height); 
    puzvalues.put("Width",width); 
    long puzresult = db.insert(Table1_name, null, puzvalues); 
    if (puzresult == -1) 
     return false; 
    else 
     return true; 


} 
public boolean isinsertclue(int hash,int x,int y,int cellposition,int cluenumber,String clue) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues cluevalues = new ContentValues(); 
    cluevalues.put("hash",hash); 
    cluevalues.put("X",x); 
    cluevalues.put("Y",y); 
    cluevalues.put("Cellposition",cellposition); 
    cluevalues.put("Cluenumber",cluenumber); 
    cluevalues.put("Clue",clue); 
    long clueresult=db.insert(Table3_name,null,cluevalues); 
    if(clueresult==-1) 
     return false; 
    else 
     return true; 
} 
    public Cursor getpuzdata() 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor puzquery=db.rawQuery("select * from" +Table1_name,null); 
     return puzquery; 
    }} 

私はsqliteのに次のコードをしたい:私が試したが、親切に私を助けて、いくつかのerrorrを示しています!

public CustomAdapter(Context context, int screenWidth, final int width,int screenHeight, int height, int hash, TextView clueTextView, TextView titleTextView){ 
    this.context = context; 
    this.width = width; 
    this.height = height; 
    this.screenHeight = screenHeight; 
    this.screenWidth = screenWidth; 
    activity = (Activity)context; 
    this.hash = hash; 
    this.clueTextView = clueTextView; 
    this.titleTextView = titleTextView; 

    clueNumber = 0; 

    //cell count 
    count = width*height; 

    //to store each cell view 
    view = new View[count]; 

    //contains solution 
    letterChar = new char[count]; 

    clueAcross = new HashMap<>(); 
    clueDown = new HashMap<>(); 

    Log.d("parseSize", String.valueOf(letterChar.length)); 

    //fetch data saved locally 
    query = ParseQuery.getQuery("Item").fromLocalDatastore(); 
    query.whereEqualTo("hash", hash); 
    query.orderByAscending("cellPosition"); 
      query.findInBackground(new FindCallback<ParseObject>() { 
       @Override 
       public void done(List<ParseObject> objects, ParseException e) { 
        if(e == null) 
         for (ParseObject parseObject : objects) { 
          if(parseObject.getString("letter").equals("")) 
           letterChar[parseObject.getInt("cellPosition")] = ' '; 
          else 
           letterChar[parseObject.getInt("cellPosition")] = parseObject.getString("letter").charAt(0); 
         } 
        ParseQuery.getQuery("Clue").fromLocalDatastore().whereEqualTo("hash", CustomAdapter.this.hash).orderByDescending("clueNumber").getFirstInBackground(new GetCallback<ParseObject>() { 
         @Override 
         public void done(ParseObject object, ParseException e) { 
          if(e == null) 
           clueSize = object.getInt("clueNumber"); 
          Log.d("clueNumber", String.valueOf(clueSize)); 
         } 
        }); 
        ParseQuery.getQuery("Clue").fromLocalDatastore().whereEqualTo("hash", CustomAdapter.this.hash).orderByAscending("cellPosition").findInBackground(new FindCallback<ParseObject>() { 
         @Override 
         public void done(List<ParseObject> objects, ParseException e) { 
          if (e == null) 
           for (ParseObject object : objects) { 
            //int pos = (object.getInt("y")*width)+object.getInt("x"); 
            if(object.getString("position").equals("Hor")) 
             clueAcross.put(object.getInt("cellPosition"), object.getString("clue")); 
            else 
             clueDown.put(object.getInt("cellPosition"), object.getString("clue")); 
            Log.d("clue", object.getInt("cellPosition")+" "+object.getString("clue")+" "+object.getString("position")); 
           } 

          //will populate the griview 
          Game.gridView.setAdapter(Game.customAdapter); 

          //to adjust layout on keyboard up 
          activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

          //dismiss progress dialog on UI thread 
          activity.runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            Game.progress.dismiss(); 
           } 
          }); 
         } 
        }); 
       } 
      }); 

    //to fix clueNumber change on minimize and restore bug 
    if(clueNumber > clueSize) 
     clueNumber = 0; 
     } 

@Override 
public int getCount() { 
    return count; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    view[position] = convertView; 
    if(view[position] == null){ 
     LayoutInflater layoutInflater = LayoutInflater.from(context); 
     view[position] = layoutInflater.inflate(R.layout.grid_cell, null); 
     view[position].setLayoutParams(new RelativeLayout.LayoutParams(screenWidth/width, screenHeight/height)); 
    } 

    //to shade black on cells containing no letters 
    view[position].setBackgroundResource(R.drawable.shape); 

    TextView cellNumberTextView = (TextView)view[position].findViewById(R.id.cellNumber); 
    final EditText letter = (EditText)view[position].findViewById(R.id.letter); 
    /*letter.setCursorVisible(false); 
    letter.setClickable(false); 
    letter.setLongClickable(false); 
    letter.setFocusable(false); 
    letter.setSelected(false); 
    letter.setKeyListener(null); 
    letter.setBackgroundResource(android.R.color.transparent);*/ 

    //letter.setText(Character.toString(letterChar[position])); 
    // cellNumberTextView.setTextSize(cellNumberTextView.getTextSize()/1.5f); 

    //set clue number on cells 
    if(clueAcross.get(position) != null){ 
     cellNumberTextView.setText(String.valueOf(clueNumber)); 
     clueNumber++; 
     //to fix clueNumber change on minimize and restore bug 
     if(clueNumber > clueSize) 
      clueNumber = 0; 
     Log.d("clueNumber", position+" "+clueNumber+ " "+clueSize); 
    } 
    else if(clueDown.get(position) != null){ 
     cellNumberTextView.setText(String.valueOf(clueNumber)); 
     clueNumber++; 

     //to fix clueNumber change on minimize and restore bug 
     if(clueNumber > clueSize) 
      clueNumber = 0; 
     Log.d("clueNumber", position+" "+clueNumber+ " "+clueSize); 
    } 


    //to disable EditText operations on black cells 
    if(letterChar[position] == ' ') { 
     view[position].setBackgroundColor(Color.BLACK); 
     letter.setClickable(false); 
     letter.setLongClickable(false); 
     letter.setFocusable(false); 
     letter.setSelected(false); 
     letter.setKeyListener(null); 
    }else 
    view[position].setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      //update clues on view click 
      clueTextView.setText(getClueDown(position)); 
      titleTextView.setText(getClueAcross(position)); 

      //to open keyboard and focus on the letter to edit 
      letter.requestFocus(); 
      letter.setCursorVisible(true); 
      ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(letter, InputMethodManager.SHOW_IMPLICIT); 
     } 
    }); 


    //update clues on letter click 
    letter.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus){ 
       letter.setText(""); 
       clueTextView.setText(getClueDown(position)); 
       titleTextView.setText(getClueAcross(position)); 
      } 
     } 
    }); 


    //check value and change color of cell accordingly 
    letter.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //letter.setText(s.toString().toUpperCase()); 
      if(s.toString().isEmpty()) 
       return; 
      Log.d("letter", "input "+s.toString()+"position "+String.valueOf(position)+Character.toString(letterChar[position])); 
      if (s.toString().equals(Character.toString(letterChar[position]))) { 
       view[position].setBackgroundResource(R.drawable.correct); 
       focusNext(position); 
      } 
      else 
       view[position].setBackgroundResource(R.drawable.wrong); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
       /* if(s.toString().isEmpty()) 
        return;*/ 
      //view[0].setBackgroundResource(R.drawable.correct); 
     } 
    }); 

    return view[position]; 
} 

//to focus next cell 
public void focusNext(int position) { 
    for (int i = position; i < count; i++) { 
     if (letterChar[i+1] == ' ') 
      continue; 
     else { 
      EditText editText = (EditText) ((ViewGroup) view[i + 1]).getChildAt(0); 
      editText.requestFocus(); 
      ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
      return; 
     } 
    } 
} 

//get ACROSS clue based on position 
public String getClueAcross(int position){ 
    for(int i=position; i>=0; i--) 
     if(clueAcross.get(i) != null) 
      return "(across): "+clueAcross.get(i); 
    return ""; 
} 

//get DOWN clue based on position 
public String getClueDown(int position){ 
    for(int i=position; i>=0; i-=width) 
     if(clueDown.get(i) != null) 
      return "(down): "+clueDown.get(i); 

    return ""; 
} 

@Override 
public Object getItem(int position) { 
    return view[position]; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

}

次のコードは、私が }

public class CustomAdapter extends BaseAdapter { 


int count, hash; 
int width, height, screenWidth, screenHeight; 
Activity activity; 
Context context; 
private static final String SELECTitem_SQL = "SELECT * FROM item"; 
private static final String SELECTpuz_SQL = "SELECT * FROM puz"; 
private static final String SELECTclue_SQL = "SELECT * FROM clue"; 


private SQLiteDatabase db,db1 ; 

puzzle ca=new puzzle(context); 

char[] letterChar; 
HashMap<Integer, String> clueAcross, clueDown; 
TextView clueTextView, titleTextView; 
int clueNumber, clueSize; 
View view[]; 
public CustomAdapter(Context context, int screenWidth, final int width,int screenHeight, int height, int hash, TextView clueTextView, TextView titleTextView) { 
    this.context = context; 
    this.width = width; 
    this.height = height; 
    this.screenHeight = screenHeight; 
    this.screenWidth = screenWidth; 
    activity = (Activity) context; 
    this.hash = hash; 
    this.clueTextView = clueTextView; 
    this.titleTextView = titleTextView; 

    clueNumber = 0; 
    letterChar = new char[100]; 
    //cell count 
    count = width * height; 

    //to store each cell view 
    view = new View[count]; 

    //contains solution 
    letterChar = new char[count]; 

    clueAcross = new HashMap<>(); 
    clueDown = new HashMap<>(); 

    Log.d("parseSize", String.valueOf(letterChar.length)); 
    Cursor c1=db.rawQuery(SELECTitem_SQL,null); 
    Cursor c2=db.rawQuery(SELECTclue_SQL,null); 
    c1.moveToFirst(); 
    c2.moveToFirst(); 
    db= openOrCreateDatabase("item", null); 
    db1=openOrCreateDatabase("clue",null); 
    String hashid=c1.getString(0); 
    String cluehash=c2.getString(0); 
    String cluenum=c2.getString(4); 
    String hashpos=c1.getString(3); 
    String hashletter=c1.getString(5); 
    String hashblack=c1.getString(4); 
    int id=hashid.length(); 
    for(int i=0;i<id;i++) 
    { 
     if(hashletter== "") 
     { 
      letterChar[Integer.parseInt(hashpos)]=' '; 

     } 
     else 
     { 
      letterChar[Integer.parseInt(hashpos)]=hashletter.charAt(0); 
     } 
    } 
} 
@Override 
public int getCount() { 
    return 0; 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    return null; 
} 

をSQLiteのためにparse.comからコードを変更しようとした方法ですこれは私の解析からsqliteのに変換するためのコードが、ときであります私はまだそれを維持するプロジェクトを構築する。

+0

私はparse.comがシャットダウンされると思います。検索すると、 –

+0

はい、それは私がsqliteに変更している理由です –

+0

どのようなエラーが表示されますか? – USKMobility

答えて

1

私はこれがあなたを助けますが、あなたのコード内でSQL構文エラーがあるかどうかわからないんだけど:

db.execSQL("CREATE TABLE IF NOT EXISTS " + Table1_name + " (hash INTEGER, title TEXT, author TEXT,copyright TEXT,height INTEGER ,width INTEGER)"); 
    db.execSQL("CREATE TABLE IF NOT EXISTS "+ Table2_name + " (hash INTEGER, x INTEGER ,y INTEGER, cell position INTEGER , black BOOLEAN , letter TEXT)"); 
    db.execSQL("CREATE TABLE IF NOT EXISTS " + Table3_name + " (hash INTEGER, x INTEGER ,y INTEGER, cellposition INTEGER, cluenumber INTEGER, position INTEGER,clue TEXT)"); 

あなたはそれぞれの文の終わりに"hash""(""EXISTS"")"後にスペースを逃した

+0

はい、私はまだ私はカスタムアダプタクラスの構文解析クエリのsqliteの代替を必要とするチャンドした –

+0

あなたがデータベースでアダプタを使用する場合は、それがより良いCursorAdapterを使用する場合 – Pooya