繁体   English   中英

Android SQL 查询仅显示一个结果

[英]Android SQL Query only showing one result

我对 Android 应用程序开发相当陌生。 我一直在开发一个计算一个人大学 GPA 的应用程序。 目前,我目前正在努力展示学生每个学期获得的 GPA。 我只能打印出第一学期的结果。

显示所有学期活动.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

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

DISPLAYALLSEMESTERACTIVITY.java ,我猜你做了两次cursor.moveToNext() ,所以它前进了 2 & 每次跳过一个记录。

对于ShowSQLiteDBdata()cursor相关的代码,您可以尝试将其替换为以下代码吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM