Android SQL Query仅显示一个结果

时间:2018-02-04 04:24:45

标签: java android sql

我是Android应用程序开发的新手。我一直在研究一个计算一个人的大学GPA的应用程序。目前,我正在努力向学生展示每个学期的GPA。我只能打印出第一学期的成绩。

DISPLAYALLSEMESTERACTIVITY.java

import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

import static com.example.govinpillai.ca2.R.id.listView1;

public class DISPLAYALLSEMESTERACTIVITY extends AppCompatActivity {
DataManager sqLiteHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
GPAListAdapter listAdapter ;
ListView LISTVIEW;
String SEMHOLDER;

ArrayList<String> ID_Array;
ArrayList<String> GPA_Array;
ArrayList<String> SEMESTER_Array;
Intent intent;

ArrayList<String> ListViewClickItemArray = new ArrayList<String>();
String TempHolder ;
private String LOG_TAG;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displayallsemesteractivity);

    LISTVIEW = (ListView) findViewById(listView1);
    intent = getIntent();
    SEMHOLDER = intent.getStringExtra("SEM");

    ID_Array = new ArrayList<String>();

    GPA_Array = new ArrayList<String>();

    SEMESTER_Array = new ArrayList<String>();

    sqLiteHelper = new DataManager(this);

    LISTVIEW.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // TODO Auto-generated method stub


    /*                Intent intent = new 
 Intent(getApplicationContext(),DISPLAYONEGRADEACTIVITY.class);

            intent.putExtra("ListViewClickedItemValue", 
    ListViewClickItemArray.get(position).toString());

            startActivity(intent);*/


        }
    });

}

@Override
protected void onResume() {


    ShowSQLiteDBdata() ;
    super.onResume();

}

private void ShowSQLiteDBdata() {

    sqLiteDatabase = sqLiteHelper.getWritableDatabase();

    cursor = sqLiteDatabase.rawQuery("SELECT id, sum(gcproduct)/sum(credit) AS Semestergpa , Semester  FROM " + DataManager.TABLE_NAME +" group by Semester", null);

    ID_Array.clear();
    SEMESTER_Array.clear();

    GPA_Array.clear();

    if (cursor.moveToFirst()) {
        do {

           ID_Array.add(cursor.getString(cursor.getColumnIndex(DataManager.Table_Column_ID)));

            //Inserting Column ID into Array to Use at ListView Click Listener Method.
/*               ListViewClickItemArray.add(cursor.getString(cursor.getColumnIndex(DataManager.Table_Column_ID)));*/
            GPA_Array.add(cursor.getString(cursor.getColumnIndex("Semestergpa")));

            SEMESTER_Array.add(cursor.getString(cursor.getColumnIndex(DataManager.Table_Column_5_SEMESTER)));
            while(!cursor.isAfterLast()) {
                Log.e(LOG_TAG, DatabaseUtils.dumpCurrentRowToString(cursor));
                cursor.moveToNext();
            }




        } while (cursor.moveToNext());
    }

    listAdapter = new GPAListAdapter(DISPLAYALLSEMESTERACTIVITY.this,

            ID_Array,
            GPA_Array,
            SEMESTER_Array
    );

    LISTVIEW.setAdapter(listAdapter);

    cursor.close();
}
}

GPAListAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class GPAListAdapter extends BaseAdapter {

Context context;
ArrayList<String> ID;
ArrayList<String> GPA;

ArrayList<String> SEMESTER;



public GPAListAdapter(
        Context context2,
        ArrayList<String> id,
        ArrayList<String> gPA,
        ArrayList<String> sEMESTER
)
{

    this.context = context2;
    this.ID = id;

    this.SEMESTER = sEMESTER;
    this.GPA = gPA;
}

   public int getCount() {
    // TODO Auto-generated method stub
    return ID.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View child, ViewGroup parent) {

    Holder holder;

    LayoutInflater layoutInflater;

    if (child == null) {
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        child = layoutInflater.inflate(R.layout.gpaitems, null);

        holder = new Holder();

        holder.IDTextView = (TextView) child.findViewById(R.id.textViewID);
        holder.GPATextView = (TextView) child.findViewById(R.id.textViewGPA);
        holder.SEMESTERTextView = (TextView) child.findViewById(R.id.textViewSEMESTER);

        child.setTag(holder);

    } else {

        holder = (Holder) child.getTag();
    }
    holder.IDTextView.setText(ID.get(position));
    holder.GPATextView.setText(GPA.get(position));
    holder.SEMESTERTextView.setText(SEMESTER.get(position));

    return child;
}

public class Holder {

    TextView IDTextView;
    TextView GPATextView;
    TextView SEMESTERTextView;
}

}

我的logcat输出

[ 02-03 23:15:46.304  6376: 6376 E/         ]
                                                                      0 {
                                                                         id=2
                                                                         Semestergpa=3.75
                                                                         SEMESTER=1
                                                                      }


                                                                      [ 02-03 23:15:46.304  6376: 6376 E/         ]
                                                                      1 {
                                                                         id=4
                                                                         Semestergpa=3
                                                                         SEMESTER=2
                                                                      }

Android虚拟设备中的所需输出

id=2
Semester=1
Semester GPA=3.75

id=4
Semester=2
Semester GPA=3

Android虚拟设备中的实际输出

id=2
Semester=1
Semester GPA=3.75

有人可以告诉我,我做错了什么以及我能做些什么来解决它?

1 个答案:

答案 0 :(得分:1)

DISPLAYALLSEMESTERACTIVITY.java中,我的猜测是你cursor.moveToNext()两次,所以它前进了2&amp;每次跳过一条记录。

对于cursor中的ShowSQLiteDBdata()相关代码,您可以尝试使用以下代码替换它吗?

if (cursor.moveToFirst()) {
    while (cursor.isAfterLast() == false) {
        // do everything here first
        ID_Array.add(....)
        .....
        .....


        // only move to next cursor position at end of while loop
        cursor.moveToNext();
    }
}

希望这有帮助,祝你好运!