显示从数据库到textview android的多个数据

时间:2017-07-17 05:46:52

标签: android

当我点击按钮时,我的问题是无法显示我从我的数据库调用到我的textview的所有数据,但我当时只能显示1个数据,而我读到了这样的另一个问题,但我仍然没有得到它。

这是我从数据库中调用数据的代码

 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;
  }
}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

试试这个。

data {2,1}

>>struct with fields

         additional_model_information: 'H Series,S3150-S0-AW-04-02-C-F421,'
 ceiling_fan_size_diameters_in_inches: '60'
      airflow_efficiency_cfm_watt_low: '727'
     airflow_efficiency_cfm_watt_high: '392'

现在在你的GrammarActivity里面for循环这样做。

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;
}