2017-07-17 78 views
-3

我的問題是無法顯示我所有的數據我從我的數據庫調用到我的textview當我點擊按鈕,但我只能顯示1個數據在當時,我讀了另一個問題像這樣,但我仍然沒有得到它。從數據庫顯示多個數據到textview android

這是我的代碼從數據庫中調用數據

public WordObject getPOSbyWords(String wordd){ 
    WordObject wordObject2 = null; 
    String query2 = "SELECT pos FROM wordbank WHERE words in ("+wordd+")"; 
    Cursor cursor = this.getDbConnection1().rawQuery(query2,null); 
    if (cursor.moveToFirst()){ 
     do { 
      //= 
      String pos1=cursor.getString(cursor.getColumnIndexOrThrow("pos")); 
      //String pos2 = cursor.getString(cursor.getColumnIndexOrThrow("words")); 
      wordObject2 = new WordObject(pos1,null); 
     }while (cursor.moveToNext()); 

    } 
    cursor.close(); 
    return wordObject2; 
} 

這是我的主要活動代碼

public class GrammarActivity extends AppCompatActivity { 
TextView postv, wordtv; 
Button btngo; 
MultiAutoCompleteTextView multiple; 
Listview listview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_grammar); 
    wordtv = (TextView) findViewById(R.id.grammar); 
    multiple = (MultiAutoCompleteTextView) findViewById(R.id.MultipleAuto); 
    btngo = (Button) findViewById(R.id.check); 
    postv = (TextView) findViewById(R.id.Partofspeech); 
    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 


    //Welcome user 
    builder.setMessage("Welcome to the Grammar Checker") 
      .setIcon(R.mipmap.ic_launcher) 
      .setPositiveButton("Enter", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

       } 
      }); 
    builder.create(); 
    builder.show(); 


    //Space Tokenizer splitting the words 

    final String[] words = getResources().getStringArray(R.array.autocomplete); 
    multiple.setTokenizer(new SpaceTokenizer()); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words); 
    multiple.setAdapter(adapter); 

    btngo.setOnClickListener(new View.OnClickListener() { 

     @Override 

     public void onClick(View v) { 
      String space = ""; 
      String foo = " "; 
      String foo1 = ","; 
      String sentences = null; 
      String red = multiple.getText().toString(); 

      if (red.isEmpty()) { 
       Toast.makeText(getApplicationContext(), " Input text please ", Toast.LENGTH_SHORT).show(); 
       multiple.setBackgroundColor(Color.RED); 
      }else 
       Toast.makeText(getApplicationContext(), "THanks ", Toast.LENGTH_SHORT).show(); 

      //Splitting the sentence into words 
      sentences = multiple.getText().toString().toLowerCase(); 
      String[] splitwords = sentences.trim().split("\\s+"); 

      for (String biyak : splitwords) { 
       foo = (foo + "'" + biyak + "'" + foo1); 
       String fot = foo.replaceAll(",$", " "); 
       wordtv.setText(fot); 

       Db1Backend db1Backend = new Db1Backend(GrammarActivity.this); 
       WordObject DisplayPOS = db1Backend.getPOSbyWords(fot); 


       postv.setText(DisplayPOS.getWord()); 

      } 

世界級

public class WordObject { 
private String word; 
private String pos; 

public WordObject(String word, String definition) { 
    this.word = word; 
    this.pos = definition; 
} 

public String getWord() { 
    return word; 
} 

public void setWord(String word) { 
    this.word = word; 
}  

public String getPOS() { 
    return pos; 
} 

public void setPOS(String definition) { 
    this.pos = definition; 
    } 
} 
+0

您每次創建此對象的新對象wordObject2 = new WordObject(pos1,null);這就是爲什麼你只能看到一條記錄。最好將所有記錄追加到一個字符串中並調用該方法一次。你可以請發佈WordObject類。 –

+0

看到我的答案先生:) –

+0

呦。兄弟!它的工作非常感謝 –

回答

0

試試這個。

public String getPOSbyWords(String wordd) 
{ 
    String myPos = ""; 
    String query2 = "SELECT pos FROM wordbank WHERE words in ("+wordd+")"; 
    Cursor cursor = this.getDbConnection1().rawQuery(query2,null); 

    try 
    { 
    if (cursor != null && cursor.getCount() > 0) 
    { 
     while (cursor.moveToNext()) 
     { 
     String pos1=cursor.getString(cursor.getColumnIndexOrThrow("pos")); 
     myPos = pos1; 
     } 
    } 
    } 
    catch (Exception e) 
    { 
    // Handle Exception here 
    } 
    finally 
    { 
     // release cursor 
     if (cursor != null && !cursor.isClosed()) 
      cursor.close(); 
    } 
    return myPos; 
} 

現在在您的GrammarActivity裏面for循環做到這一點。

// Taking String Builder to append all the String in one. 
StringBuilder sb = new StringBuilder(); 
for (String biyak : splitwords) 
{ 
    foo = (foo + "'" + biyak + "'" + foo1); 
    String fot = foo.replaceAll(",$", " "); 
    wordtv.setText(fot); 

    Db1Backend db1Backend = new Db1Backend(GrammarActivity.this); 
    // Append all your position into StringBuilder 
    sb.append(db1Backend.getPOSbyWords(fot)); 
} 
postv.setText(sb.toString()); 
0

這種替換你的方法。

public ArrayList<WordObject> getPOSbyWords(String wordd){ 
    WordObject wordObject2 = null; 
    String query2 = "SELECT pos FROM wordbank WHERE words in ("+wordd+")"; 
    Cursor cursor = this.getDbConnection1().rawQuery(query2,null); 
    ArrayList<WordObject> words = new ArrayList<>(); 
    if (cursor.moveToFirst()){ 
     do { 
      //= 
      String pos1=cursor.getString(cursor.getColumnIndexOrThrow("pos")); 
      //String pos2 = cursor.getString(cursor.getColumnIndexOrThrow("words")); 
      wordObject2 = new WordObject(pos1,null); 
      words.add(wordObject2); 
     }while (cursor.moveToNext()); 

    } 
    cursor.close(); 
    return words; 
} 
+0

如何將該代碼實現到我的主要活動?我也應該改變它? 'Db1Backend db1Backend = new Db1Backend(GrammarActivity.this); WordObject DisplayPOS = db1Backend.getPOSbyWords(fot); postv.setText(DisplayPOS.getWord()); ' –

+0

我嘗試改變它,但我以錯誤 'Db1Backend db1Backend = new Db1Backend(GrammarActivity.this)結束; ArrayList DisplayPOS = db1Backend.getPOSbyWords(fot); postv.setText(DisplayPOS);' –