Android没有显示SQL查询结果

时间:2018-02-04 01:37:16

标签: java android sql

我是Android开发的新手,我目前正致力于计算一个GPA的应用程序。我一直试图为每个学期展示GPA,但到目前为止,我的查询无效。

列出每学期GPA的代码:DISPLAYALLSEMESTERACTIVITY.java

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

import java.util.ArrayList;

import static com.example.markymark.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 ;

@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() {



    super.onResume();
    ShowSQLiteDBdata() ;
}

private void ShowSQLiteDBdata() {

    sqLiteDatabase = sqLiteHelper.getWritableDatabase();

    cursor = sqLiteDatabase.rawQuery("SELECT 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.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;
}

}

有人可以解释一下我做错了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您确定您的rawQuery会返回结果吗?

试试这段代码:

while  (!cursor.isAfterLast()) {
       Log.d(LOG_TAG,"_id = +cursor.getString(catList.getColumnIndex("_id")));
       cursor.moveToNext();
}

检查结果