我的问题是无法显示我所有的数据我从我的数据库调用到我的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;
}
}
您每次创建此对象的新对象wordObject2 = new WordObject(pos1,null);这就是为什么你只能看到一条记录。最好将所有记录追加到一个字符串中并调用该方法一次。你可以请发布WordObject类。 –
看到我的答案先生:) –
呦。兄弟!它的工作非常感谢 –