2017-07-17 87 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);' –