在一个ArrayAdapter无法解析构造

在一个ArrayAdapter无法解析构造

本文介绍了在一个ArrayAdapter无法解析构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经附加值的主题表内,但使用的ListView无法检索值了。

我的code:

DatabaseHelper.java

 公共类DatabaseHelper扩展SQLiteOpenHelper {
    SQLiteDatabase分贝;
    公共静态最后弦乐DATABASE_NAME =myexam.db;
    公共静态最终诠释DATABASE_VERSION = 1;    公共DatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        Log.d(数据库操作,数据库创建成功!);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        字符串CREATE_USER =CREATE TABLE+
                user.TABLE_USER +(+
                user.USER_ID +INTEGER PRIMARY KEY NOT NULL,+
                user.USER_EMAIL +VARCHAR NOT NULL,+
                user.USER_PASS +VARCHAR NOT NULL,+
                user.USER_CPASS +VARCHAR NOT NULL);;        字符串CREATE_SUBJECT =CREATE TABLE+
                subject.TABLE_SUBJECT +(+
                subject.USER_ID +INTEGER,+
                subject.SUBUSER_ID +INTEGER PRIMARY KEY NOT NULL,+
                subject.SUBJECT_NAME +TEXT NOT NULL,+
                subject.SUBJECT_RESULTS +TEXT NOT NULL,+
                subject.SUBJECT_HOURS +TEXT NOT NULL);;        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_SUBJECT);
        Log.d(数据库操作,表创建!);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        db.execSQL(DROP TABLE IF EXISTS+ user.TABLE_USER);
        db.execSQL(DROP TABLE IF EXISTS+ subject.TABLE_SUBJECT);
        的onCreate(DB);
    }    //插入用户
    公共布尔insertUser(UID字符串,字符串电子邮件,字符串传递,字符串cpass){
        SQLiteDatabase分贝= this.getWritableDatabase();        ContentValues​​ CV =新ContentValues​​();        cv.put(user.USER_ID,UID);
        cv.put(user.USER_EMAIL,电子邮件);
        cv.put(user.USER_PASS,通过);
        cv.put(user.USER_CPASS,cpass);
        长USER_ID = db.insert(user.TABLE_USER,空,CV);        如果(USER_ID == -1){
            返回false;
        }其他
            返回true;
    }    //获取用户
    公共光标的getUser(){
        SQLiteDatabase分贝= this.getReadableDatabase();
        串selectQuery =SELECT+ user.USER_ID +,+ user.USER_PASS +从+ user.TABLE_USER;        光标C = db.rawQuery(selectQuery,NULL);        返回℃;
    }    //插入主题
    公共布尔insertSubjects(UID字符串,字符串SNAME,字符串sresults,字符串shours){
        SQLiteDatabase分贝= this.getWritableDatabase();        ContentValues​​ CV =新ContentValues​​();        cv.put(subject.SUBUSER_ID,UID);
        cv.put(subject.SUBJECT_NAME,SNAME);
        cv.put(subject.SUBJECT_RESULTS,sresults);
        cv.put(subject.SUBJECT_HOURS,shours);
        长sub_id = db.insert(subject.TABLE_SUBJECT,空,CV);        如果(sub_id == -1){
            返回false;
        }其他
            返回true;
    }    公共光标getSubjects(){
        SQLiteDatabase分贝= this.getReadableDatabase();
        串selectQuery =SELECT+ subject.SUBUSER_ID +
                            ,+ subject.SUBJECT_NAME +
                            ,+ subject.SUBJECT_RESULTS +
                            ,+ subject.SUBJECT_HOURS +
                            FROM+ subject.TABLE_SUBJECT +
                            WHE​​RE+ subject.SUBUSER_ID +
                            =+ subject.USER_ID;        光标C = db.rawQuery(selectQuery,NULL);
        返回℃;
    }
}

subject.java

 公共类主题{
    公共静态最后弦乐SUBUSER_ID =etUID;
    公共静态最后弦乐USER_ID = user.USER_ID;
    公共静态最后弦乐SUBJECT_NAME =etSubjects;
    公共静态最后弦乐SUBJECT_RESULTS =etResults;
    公共静态最后弦乐SUBJECT_HOURS =etHours;
    公共静态最后弦乐TABLE_SUBJECT =科目;
}

user.java

 公共类用户{
    公共静态最后弦乐USER_ID =ETID;
    公共静态最后弦乐USER_EMAIL =etEmail;
    公共静态最后弦乐USER_PASS =etPassword;
    公共静态最后弦乐USER_CPASS =etCPassword;
    公共静态最后弦乐TABLE_USER =用户;
}

addSubject.java

 公共类addSubjects扩展AppCompatActivity {    的EditText etUID,etSubjects,etResults,etHours;
    DatabaseHelper databaseHelper;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_add_subjects);        etUID =(EditText上)findViewById(R.id.etUID);
        etSubjects =(EditText上)findViewById(R.id.etSubjects);
        etResults =(EditText上)findViewById(R.id.etResults);
        etHours =(EditText上)findViewById(R.id.etHours);        databaseHelper =新DatabaseHelper(本);        动作条动作条= getSupportActionBar();
        actionBar.setHomeButtonEnabled(真);
        actionBar.setDisplayHomeAsUpEnabled(真);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_add_subjects,菜单);
        返回true;
    }   @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();       如果(ID == android.R.id.home){
           意图I =新意图(这一点,subjects.class);
           startActivity(ⅰ);
           完();
       }        // noinspection SimplifiableIfStatement
       如果(ID == R.id.action_home){
           意图I =新意图(这一点,Menus.class);
           startActivity(ⅰ);
           完();
       }
       返回super.onOptionsItemSelected(项目);
    }    公共无效SaveInfo(查看视图){
        。字符串userid = etUID.getText()的toString();
        字符串对象= etSubjects.getText()的toString()。
        字符串结果= etResults.getText()的toString()。
        串个小时= etHours.getText()的toString()。        如果(userid.equals()|| subjects.equals()|| results.equals()|| hours.equals()){
            Toast.makeText(addSubjects.this,请在所有的关键信息,Toast.LENGTH_SHORT).show();
        }
        否则如果((results.equals(A))及!及(results.equals(B))及!及(results.equals(C))及!及(! results.equals(D))及及(!results.equals(F))){
            Toast.makeText(getBaseContext(),请键入有效的结果!Toast.LENGTH_SHORT).show();
        }
        否则如果((hours.equals(1))及!及(hours.equals(二))及!及(hours.equals(三公))及!及(! hours.equals(四))){
            Toast.makeText(getBaseContext(),请键入有效的小时!,Toast.LENGTH_SHORT).show();
        }
        其他{
            布尔isInserted = databaseHelper.insertSubjects(用户ID,受试者,结果,小时);
            如果(isInserted = TRUE){
                Toast.makeText(addSubjects.this,补充说:成功!,Toast.LENGTH_SHORT).show();
                意图I =新意图(这一点,subjects.class);
                startActivity(ⅰ);
                完();
            }其他{
                Toast.makeText(addSubjects.this,不成功补充!!!,Toast.LENGTH_SHORT).show();
            }
        }
    }
}

subject.java 的ListView

 包com.smartexamorganizer.subjects;进口android.content.Intent;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.ListView;进口com.smartexamorganizer.ListData.ListDataAdapter;
进口com.smartexamorganizer.ListData.SubjectProvider;
进口com.smartexamorganizer.R;
进口com.smartexamorganizer.activity.Menus;
进口com.smartexamorganizer.database.DatabaseHelper;公共类科目扩展AppCompatActivity {    ListView的lvSubjects;    DatabaseHelper databaseHelper;
    光标光标;
    ListDataAdapter listDataAdapter;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_subjects);        lvSubjects =(ListView控件)findViewById(R.id.lvSubjects);        listDataAdapter =新ListDataAdapter(getApplicationContext(),R.layout.display_subjects_row);
        lvSubjects.setAdapter(listDataAdapter);        databaseHelper =新DatabaseHelper(本);
        databaseHelper.getReadableDatabase();
        光标= databaseHelper.getSubjects();        如果(cursor.moveToFirst()){
            做{
                串sub_name,sub_results,sub_hours;
                sub_name = cursor.getString(0);
                sub_results = cursor.getString(1);
                sub_hours = cursor.getString(2);                SubjectProvider subjectProvider =新SubjectProvider名(sub_name,sub_results,sub_hours);
                listDataAdapter.add(subjectProvider); //传递数据的每一行            }而(cursor.moveToNext());
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_subjects,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_add){
            意图I =新意图(这一点,addSubjects.class);
            startActivity(ⅰ);
            完();
        }        如果(ID == R.id.action_home){
            意图I =新意图(这一点,Menus.class);
            startActivity(ⅰ);
            完();
        }        返回super.onOptionsItemSelected(项目);
    }
}

ListDataAdapter.java

 包com.smartexamorganizer.ListData;进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;进口com.smartexamorganizer.R;进口的java.util.ArrayList;
进口的java.util.List;/ **
 *创建宏碁在2015年12月18日。
 * /
公共类ListDataAdapter扩展ArrayAdapter {
    列表列表=新的ArrayList();
    公共ListDataAdapter(上下文的背景下,资源INT){
        超(背景下,资源);
    }    静态类LayoutHandler {
        TextView的SUB_NAME,SUB_RESULTS,SUB_HOURS;
    }
    @覆盖
    公共无效添加(Object对象){
        super.add(对象);
        list.add(对象);
    }    @覆盖
    公众诠释的getCount(){
        返回则为list.size();
    }    @覆盖
    公共对象的getItem(INT位置){
        返回list.get(位置);
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看排= convertView;
        LayoutHandler layoutHandler;
        如果(行== NULL){
            LayoutInflater layoutInflater =(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
            行= layoutInflater.inflate(R.layout.display_subjects_row,父母,假);
            layoutHandler =新LayoutHandler();
            layoutHandler.SUB_NAME =(TextView中)row.findViewById(R.id.tvSub_Name);
            layoutHandler.SUB_RESULTS =(TextView中)row.findViewById(R.id.tvSub_Results);
            layoutHandler.SUB_HOURS =(TextView中)row.findViewById(R.id.tvSub_Hours);
            row.setTag(layoutHandler);
        }
        其他{
            layoutHandler =(LayoutHandler)row.getTag();
        }
        SubjectProvider subjectProvider =(SubjectProvider)this.getItem(位置);
        layoutHandler.SUB_NAME.setText(subjectProvider.getSub_name());
        layoutHandler.SUB_RESULTS.setText(subjectProvider.getSub_results());
        layoutHandler.SUB_HOURS.setText(subjectProvider.getSub_hours());        返回行;
    }
}

SubjectProvider.java

 公共类SubjectProvider {
    私人字符串sub_name;
    私人字符串sub_results;
    私人字符串sub_hours;    公共SubjectProvider(字符串sub_name,字符串sub_results,字符串sub_hours){
        this.sub_name = sub_name;
        this.sub_results = sub_results;
        this.sub_hours = sub_hours;
    }    公共字符串getSub_hours(){
        返回sub_hours;
    }    公共无效setSub_hours(字符串sub_hours){
        this.sub_hours = sub_hours;
    }    公共字符串getSub_name(){
        返回sub_name;
    }    公共无效setSub_name(字符串sub_name)调用{
        this.sub_name = sub_name;
    }    公共字符串getSub_results(){
        返回sub_results;
    }    公共无效setSub_results(字符串sub_results){
        this.sub_results = sub_results;
    }
}

错误:

 错误:(40 45)错误:发现ArrayAdapter没有合适的构造(科目,INT,ArrayList的< SubjectProvider>)
构造ArrayAdapter.ArrayAdapter(背景下,INT,INT)不适用
(参数不匹配; ArrayList的< SubjectProvider>不能被转换成int)
构造ArrayAdapter.ArrayAdapter(背景下,INT的String [])不适用
(参数不匹配; ArrayList的< SubjectProvider每个可不能转换为字符串[])
构造ArrayAdapter.ArrayAdapter(背景下,INT,列表与LT;弦乐>)的不适用
(参数不匹配; ArrayList的< SubjectProvider每个可不能转换为List<串GT;)


解决方案

试试这个:

  // getSubjects()现在将返回SubjectProviders的List
//你可以看到下面的
公众的ArrayList< SubjectProvider> getSubjects(){
    //获取数据库
    SQLiteDatabase分贝= this.getReadableDatabase();
    //创建一个应该返回名称的结果和时间查询
    //对于具有subuser_id等于USER_ID各受试者
    串selectQuery =SELECT+ subject.SUBJECT_NAME +
                        ,+ subject.SUBJECT_RESULTS +
                        ,+ subject.SUBJECT_HOURS +
                        FROM+ subject.TABLE_SUBJECT +
                        WHE​​RE+ subject.SUBUSER_ID +
                        =+ subject.USER_ID;    //创建中,我们会迟到存储所有subjectproviders ArrayList中
    //此ArrayList将被退回,所以你可以在你的类科目使用
    ArrayList的< SubjectProvider> subjectList =新的ArrayList< SubjectProvider>()
    //执行选择查询
    光标C = db.rawQuery(selectQuery,NULL);
    //如果光标不为空,又名一些至少有一行被发现
    如果(c.moveToFirst()){
        //通过所有的循环结果
        做{
            的System.out.println(发现结果!);
            //从游标获取值
            串sub_name,sub_results,sub_hours;
            sub_name = cursor.getString(0);
            sub_results = cursor.getString(1);
            sub_hours = cursor.getString(2);            //创建一个新subjectprovider对象来存储它的价值观
            SubjectProvider subjectProvider =新SubjectProvider名(sub_name,sub_results,sub_hours);            的System.out.println(主题:+ sub_name +中的+ sub_hours得到+ sub_results +);
            //它添加到ArrayList
            subjectList.add(subjectProvider);
        //停止,如果没有更多的结果
        }而(c.moveToNext());
    }
    //列表回到你的主题类
    返回subjectList;
}

和科目:

  //创建一个新的ArrayList中,将包含值从
    // getsubjects方法     ArrayList的< SubjectProvider> my_array_list =(ArrayList的< SubjectProvider>)databaseHelper.getSubjects();
    //创建一个适配器,以便可以从ArrayList中添加值到你的列表视图
    ArrayAdapter<串GT; arrayAdapter =新ArrayAdapter<串GT;(
             subjects.this,
             android.R.layout.simple_list_item_1,
             my_array_list);     //设置previously创建适配器为您的列表视图适配器
     lvSubjects.setAdapter(arrayAdapter);

插入方式:

  //插入主题
公共布尔insertSubjects(整数ID,UID整数,字符串SNAME,字符串sresults,字符串shours){
    SQLiteDatabase分贝= this.getWritableDatabase();    ContentValues​​ CV =新ContentValues​​();    cv.put(user.USER_ID,身份证);
    cv.put(subject.SUBUSER_ID,UID);
    cv.put(subject.SUBJECT_NAME,SNAME);
    cv.put(subject.SUBJECT_RESULTS,sresults);
    cv.put(subject.SUBJECT_HOURS,shours);
    长sub_id = db.insert(subject.TABLE_SUBJECT,空,CV);    如果(sub_id == -1){
        返回false;
    }其他
        返回true;
    }
}

I had added values inside the subjects table but could not retrieve the values out using ListView.

My code:

DatabaseHelper.java

 public class DatabaseHelper extends SQLiteOpenHelper {
    SQLiteDatabase db;
    public static final String DATABASE_NAME = "myexam.db";
    public static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("Database operations", "Database successfully created!");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USER = "CREATE TABLE " +
                user.TABLE_USER + "(" +
                user.USER_ID + " INTEGER PRIMARY KEY NOT NULL," +
                user.USER_EMAIL + " VARCHAR NOT NULL," +
                user.USER_PASS + " VARCHAR NOT NULL," +
                user.USER_CPASS + " VARCHAR NOT NULL);";

        String CREATE_SUBJECT = "CREATE TABLE " +
                subject.TABLE_SUBJECT + "(" +
                subject.USER_ID + " INTEGER," +
                subject.SUBUSER_ID + " INTEGER PRIMARY KEY NOT NULL," +
                subject.SUBJECT_NAME + " TEXT NOT NULL," +
                subject.SUBJECT_RESULTS + " TEXT NOT NULL," +
                subject.SUBJECT_HOURS + " TEXT NOT NULL);";

        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_SUBJECT);
        Log.d("Database operations", "Table created!");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + user.TABLE_USER);
        db.execSQL("DROP TABLE IF EXISTS " + subject.TABLE_SUBJECT);
        onCreate(db);
    }

    //insert User
    public boolean insertUser(String uid, String email, String pass, String cpass) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues cv = new ContentValues();

        cv.put(user.USER_ID, uid);
        cv.put(user.USER_EMAIL, email);
        cv.put(user.USER_PASS, pass);
        cv.put(user.USER_CPASS, cpass);
        long user_id =  db.insert(user.TABLE_USER, null, cv);

        if (user_id == -1){
            return false;
        }else
            return true;
    }

    //get User
    public Cursor getUser(){
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT "+ user.USER_ID + ", " + user.USER_PASS + " FROM " + user.TABLE_USER;

        Cursor c = db.rawQuery(selectQuery, null);

        return c;
    }

    //insert Subjects
    public boolean insertSubjects(String uid, String sname, String sresults, String shours) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues cv = new ContentValues();

        cv.put(subject.SUBUSER_ID, uid);
        cv.put(subject.SUBJECT_NAME, sname);
        cv.put(subject.SUBJECT_RESULTS, sresults);
        cv.put(subject.SUBJECT_HOURS, shours);
        long sub_id =  db.insert(subject.TABLE_SUBJECT, null, cv);

        if (sub_id == -1){
            return false;
        }else
            return true;
    }

    public Cursor getSubjects(){
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT "+ subject.SUBUSER_ID +
                            ", " + subject.SUBJECT_NAME +
                            ", " + subject.SUBJECT_RESULTS +
                            ", " + subject.SUBJECT_HOURS +
                            " FROM " + subject.TABLE_SUBJECT +
                            " WHERE " + subject.SUBUSER_ID +
                            " = " + subject.USER_ID;

        Cursor c = db.rawQuery(selectQuery, null);
        return c;
    }
}

subject.java

public class subject {
    public static final String SUBUSER_ID = "etUID";
    public static final String USER_ID = user.USER_ID;
    public static final String SUBJECT_NAME = "etSubjects";
    public static final String SUBJECT_RESULTS = "etResults";
    public static final String SUBJECT_HOURS = "etHours";
    public static final String TABLE_SUBJECT = "subjects";
}

user.java

 public class user {
    public static final String USER_ID = "etID";
    public static final String USER_EMAIL = "etEmail";
    public static final String USER_PASS = "etPassword";
    public static final String USER_CPASS = "etCPassword";
    public static final String TABLE_USER = "user";
}

addSubject.java

public class addSubjects extends AppCompatActivity {

    EditText etUID, etSubjects, etResults, etHours;
    DatabaseHelper databaseHelper;

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

        etUID = (EditText)findViewById(R.id.etUID);
        etSubjects = (EditText)findViewById(R.id.etSubjects);
        etResults = (EditText)findViewById(R.id.etResults);
        etHours = (EditText)findViewById(R.id.etHours);

        databaseHelper = new DatabaseHelper(this);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_subjects, menu);
        return true;
    }

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

       if(id == android.R.id.home){
           Intent i = new Intent(this, subjects.class);
           startActivity(i);
           finish();
       }

        //noinspection SimplifiableIfStatement
       if (id == R.id.action_home) {
           Intent i = new Intent(this, Menus.class);
           startActivity(i);
           finish();
       }
       return super.onOptionsItemSelected(item);
    }

    public void SaveInfo(View view){
        String userid = etUID.getText().toString();
        String subjects = etSubjects.getText().toString();
        String results = etResults.getText().toString();
        String hours = etHours.getText().toString();

        if(userid.equals("") || subjects.equals("") || results.equals("") || hours.equals("")){
            Toast.makeText(addSubjects.this, "Please key in all the information", Toast.LENGTH_SHORT).show();
        }
        else if((!results.equals("A")) && (!results.equals("B")) && (!results.equals("C")) && (!results.equals("D")) && (!results.equals("F"))){
            Toast.makeText(getBaseContext(), "Please key in valid results!", Toast.LENGTH_SHORT).show();
        }
        else if((!hours.equals("One")) && (!hours.equals("Two")) && (!hours.equals("Three")) && (!hours.equals("Four"))){
            Toast.makeText(getBaseContext(), "Please key in valid hours!", Toast.LENGTH_SHORT).show();
        }
        else{
            boolean isInserted = databaseHelper.insertSubjects(userid, subjects, results, hours);
            if(isInserted = true){
                Toast.makeText(addSubjects.this, "Added Successfully!!!", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(this, subjects.class);
                startActivity(i);
                finish();
            }else{
                Toast.makeText(addSubjects.this, "Unsuccessfully added!!!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

subject.java (ListView)

package com.smartexamorganizer.subjects;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.smartexamorganizer.ListData.ListDataAdapter;
import com.smartexamorganizer.ListData.SubjectProvider;
import com.smartexamorganizer.R;
import com.smartexamorganizer.activity.Menus;
import com.smartexamorganizer.database.DatabaseHelper;

public class subjects extends AppCompatActivity {

    ListView lvSubjects;

    DatabaseHelper databaseHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;

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

        lvSubjects = (ListView) findViewById(R.id.lvSubjects);

        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.display_subjects_row);
        lvSubjects.setAdapter(listDataAdapter);

        databaseHelper = new DatabaseHelper(this);
        databaseHelper.getReadableDatabase();
        cursor = databaseHelper.getSubjects();

        if (cursor.moveToFirst()) {
            do {
                String sub_name, sub_results, sub_hours;
                sub_name = cursor.getString(0);
                sub_results = cursor.getString(1);
                sub_hours = cursor.getString(2);

                SubjectProvider subjectProvider = new SubjectProvider(sub_name, sub_results, sub_hours);
                listDataAdapter.add(subjectProvider); //pass each row of data

            } while (cursor.moveToNext());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_subjects, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_add) {
            Intent i = new Intent(this, addSubjects.class);
            startActivity(i);
            finish();
        }

        if (id == R.id.action_home) {
            Intent i = new Intent(this, Menus.class);
            startActivity(i);
            finish();
        }

        return super.onOptionsItemSelected(item);
    }
}

ListDataAdapter.java

package com.smartexamorganizer.ListData;

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

import com.smartexamorganizer.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Acer on 12/18/2015.
 */
public class ListDataAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler{
        TextView SUB_NAME, SUB_RESULTS, SUB_HOURS;
    }
    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;
        if(row == null){
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.display_subjects_row, parent, false);
            layoutHandler = new LayoutHandler();
            layoutHandler.SUB_NAME = (TextView)row.findViewById(R.id.tvSub_Name);
            layoutHandler.SUB_RESULTS = (TextView)row.findViewById(R.id.tvSub_Results);
            layoutHandler.SUB_HOURS = (TextView)row.findViewById(R.id.tvSub_Hours);
            row.setTag(layoutHandler);
        }
        else{
            layoutHandler = (LayoutHandler)row.getTag();
        }
        SubjectProvider subjectProvider = (SubjectProvider) this.getItem(position);
        layoutHandler.SUB_NAME.setText(subjectProvider.getSub_name());
        layoutHandler.SUB_RESULTS.setText(subjectProvider.getSub_results());
        layoutHandler.SUB_HOURS.setText(subjectProvider.getSub_hours());

        return row;
    }
}

SubjectProvider.java

public class SubjectProvider {
    private String sub_name;
    private String sub_results;
    private String sub_hours;

    public SubjectProvider(String sub_name, String sub_results, String sub_hours){
        this.sub_name = sub_name;
        this.sub_results = sub_results;
        this.sub_hours = sub_hours;
    }

    public String getSub_hours() {
        return sub_hours;
    }

    public void setSub_hours(String sub_hours) {
        this.sub_hours = sub_hours;
    }

    public String getSub_name() {
        return sub_name;
    }

    public void setSub_name(String sub_name) {
        this.sub_name = sub_name;
    }

    public String getSub_results() {
        return sub_results;
    }

    public void setSub_results(String sub_results) {
        this.sub_results = sub_results;
    }
}

Error:

Error:(40, 45) error: no suitable constructor found for ArrayAdapter(subjects,int,ArrayList<SubjectProvider>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; ArrayList<SubjectProvider> cannot be converted to int)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; ArrayList<SubjectProvider> cannot be converted to String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; ArrayList<SubjectProvider> cannot be converted to List<String>)
解决方案

Try this:

// getSubjects() will now return a List of SubjectProviders
// as you can see below
public ArrayList<SubjectProvider> getSubjects(){
    // obtain the database
    SQLiteDatabase db = this.getReadableDatabase();
    // create the query that should return name results and hours
    // for each subject that has the subuser_id equal to the user_id
    String selectQuery = "SELECT " + subject.SUBJECT_NAME +
                        ", " + subject.SUBJECT_RESULTS +
                        ", " + subject.SUBJECT_HOURS +
                        " FROM " + subject.TABLE_SUBJECT +
                        " WHERE " + subject.SUBUSER_ID +
                        " = " + subject.USER_ID;

    // create the arraylist in which we will late store all the subjectproviders
    // this arraylist will be returned so you can use it in your subjects class
    ArrayList<SubjectProvider> subjectList = new  ArrayList<SubjectProvider>()
    // execute the select-query
    Cursor c = db.rawQuery(selectQuery, null);
    // if the cursor is not empty aka some at least one row was found
    if (c.moveToFirst()) {
        // loop through all the results
        do {
            System.out.println("Results were found!");
            // get the values from the cursor
            String sub_name, sub_results, sub_hours;
            sub_name = cursor.getString(0);
            sub_results = cursor.getString(1);
            sub_hours = cursor.getString(2);

            // create a new subjectprovider Object to store the values in it
            SubjectProvider subjectProvider = new SubjectProvider(sub_name, sub_results, sub_hours);

            System.out.println("Subject: "+sub_name+" got" +sub_results+" in "+sub_hours);
            // add it to the arraylist
            subjectList.add(subjectProvider);
        // stop if there are no more results
        } while (c.moveToNext());
    }
    // return the list to your subjects class
    return subjectList;
}

and in subjects:

    // create a new arraylist, that will contain the values from your
    // getsubjects method

     ArrayList<SubjectProvider> my_array_list = (ArrayList<SubjectProvider>) databaseHelper.getSubjects();
    // create an adapter so you can add the values from the arraylist to your listview
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             subjects.this,
             android.R.layout.simple_list_item_1,
             my_array_list);

     // set the previously created adapter as an adapter for your listview
     lvSubjects.setAdapter(arrayAdapter);

Insert method:

//insert Subjects
public boolean insertSubjects(Integer id, Integer uid, String sname, String sresults, String shours) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues cv = new ContentValues();

    cv.put(user.USER_ID, id);
    cv.put(subject.SUBUSER_ID, uid);
    cv.put(subject.SUBJECT_NAME, sname);
    cv.put(subject.SUBJECT_RESULTS, sresults);
    cv.put(subject.SUBJECT_HOURS, shours);
    long sub_id =  db.insert(subject.TABLE_SUBJECT, null, cv);

    if (sub_id == -1){
        return false;
    }else
        return true;
    }
}

这篇关于在一个ArrayAdapter无法解析构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:45