Android:应用程序在显示自定义列表视图时崩溃

时间:2017-07-11 16:17:55

标签: android sqlite listview android-arrayadapter

我正在尝试在自定义列表视图中显示我的数据库,但每次运行该活动时,我的应用程序都会停止运行。请查看我的代码并帮助我。 这是我的活动,我试图从下拉列表中选择的表中提取数据,然后将其显示在自定义列表视图中。

package com.example.aas.advancedattendancesystem;

import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import model_class.Attendance_database;
import model_class.Attendance_database_handler;

import static com.example.aas.advancedattendancesystem.R.id.dropdown;
import static com.example.aas.advancedattendancesystem.R.id.listshow;

public class Show_databases extends AppCompatActivity {

private Attendance_database_handler adh;
private ArrayList<Attendance_database> dblist = new ArrayList<>();
private CustomArrayAdapter caa;
private ListView lvshow;
private Spinner dropdown;
String[] items = new String[]{"CSE7A"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_icon);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
    getSupportActionBar().setTitle(Html.fromHtml("<font color='#3BB9FF'>Attendance </font>"));
    setContentView(R.layout.activity_show_databases);

    lvshow=(ListView) findViewById(R.id.listshow);
    dropdown=(Spinner) findViewById(R.id.dropdown);

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
    dropdown.setAdapter(adapter);


    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selected = parent.getItemAtPosition(position).toString();
            refreshData(selected);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}


    private void refreshData(String selected) {
       try {
           dblist.clear();
           adh = new Attendance_database_handler(getApplicationContext());
           ArrayList<Attendance_database> listFromDB;
           listFromDB= adh.getAttendance_list(selected);

           for (int i = 0; i < listFromDB.size(); i++) {

               Integer roll = listFromDB.get(i).getRoll();
               String name = listFromDB.get(i).getStudent_name();

               Attendance_database mylist = new Attendance_database();
               mylist.setRoll(roll);
               mylist.setStudent_name(name);

               dblist.add(mylist);
           }
           adh.close();

           caa = new CustomArrayAdapter(Show_databases.this, R.layout.custom_listview, dblist);
           lvshow.setAdapter(caa);
           caa.notifyDataSetChanged();
       }catch (Exception e){
           Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
       }
    }
}

这是我的数据库处理程序

    package model_class;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    import android.widget.Toast;

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

    import static model_class.Constants.ROLL;
    import static model_class.Constants.STUDENT_NAME;
    import static model_class.Constants.TABLE_NAME;


    public class Attendance_database_handler extends SQLiteOpenHelper{

//private final ArrayList<Attendance_database> attendance_list=new ArrayList<>();

public Attendance_database_handler(Context context) {
    super(context,Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
    Log.v("OnUpgrade","Dropping the table");
    onCreate(db);

}

public void addRecord(Attendance_database adb){
    SQLiteDatabase db=this.getWritableDatabase();
    String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+
            adb.getClass_name()+"("+
            ROLL+" INTEGER PRIMARY KEY,"+
            STUDENT_NAME+" TEXT);";
    db.execSQL(CREATE_TABLE);
    ContentValues values=new ContentValues();


    values.put(ROLL,adb.getRoll());
    values.put(STUDENT_NAME,adb.getStudent_name());

    db.insert(adb.getClass_name(),null,values);
    db.close();
}

public ArrayList<Attendance_database> getAttendance_list(String selected) {

    //attendance_list.clear();

    String selectQuery = "SELECT * FROM " + selected;

    SQLiteDatabase db = this.getReadableDatabase();
    // Cursor cursor = db.rawQuery(selectQuery, null);


    Cursor cursor = db.rawQuery(selectQuery,null);
    ArrayList<Attendance_database> attendance_list = new ArrayList<>();
    //loop through cursor

    if (cursor.moveToFirst()) {

        do {
            Attendance_database myList = new Attendance_database();

            myList.setRoll(cursor.getInt(cursor.getColumnIndex(ROLL)));
            myList.setStudent_name(cursor.getString(cursor.getColumnIndex(STUDENT_NAME)));

            attendance_list.add(myList);

        } while (cursor.moveToNext());
    }


    cursor.close();
    db.close();

    return attendance_list;

}

}

这是一个具有我想要的所有参数的类。

package model_class;

public class Constants {

public static final String DATABASE_NAME="db_name";
public static final int DATABASE_VERSION=8;
public static final String TABLE_NAME="tb_name";
public static final String STUDENT_NAME="st_name";
public static final String ROLL="st_roll";
}

这是我的自定义数组适配器

package com.example.aas.advancedattendancesystem;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import model_class.Attendance_database;

public class CustomArrayAdapter extends ArrayAdapter<Attendance_database> {

private ArrayList<Attendance_database> alist = new ArrayList<>();
Activity activity;
int layoutResource;
Attendance_database adb;


public CustomArrayAdapter(Activity act, int resource, ArrayList<Attendance_database> data) {
    super(act, resource, data);
    activity = act;
    layoutResource = resource;
    alist = data;
    notifyDataSetChanged();
}
@Override
public int getCount() {
    return alist.size();
}

@Override
public Attendance_database getItem(int position) {
    return  alist.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    ViewHolder holder;

    if (view == null || (view.getTag())==null) {
        LayoutInflater inflater;
        inflater = LayoutInflater.from(activity);
        view = inflater.inflate(layoutResource, null);
        holder=new ViewHolder();
        holder.roll= (TextView)view.findViewById(R.id.tvroll);
        holder.name= (TextView) view.findViewById(R.id.tvname);
        view.setTag(holder);
    }else{
        holder=(ViewHolder) view.getTag();
    }

    holder.mylist=getItem(position);
    holder.roll.setText(holder.mylist.getRoll());
    holder.name.setText(holder.mylist.getStudent_name());
    return view;
}


class ViewHolder{
Attendance_database mylist;
TextView roll;
TextView name;
}}

请帮忙。我无法弄清楚出了什么问题。提前谢谢你:)

logcat的:

07-12 17:09:49.168 8600-8600/com.example.aas.advancedattendancesystem 
E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.aas.advancedattendancesystem, PID: 8600
                                                                                        android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                                                            at android.content.res.Resources.getText(Resources.java:331)
                                                                                            at android.widget.TextView.setText(TextView.java:4554)
                                                                                        at com.example.aas.advancedattendancesystem.CustomArrayAdapter.getView(CustomArrayAdapter.java:74)
                                                                                        at android.widget.AbsListView.obtainView(AbsListView.java:2362)
                                                                                        at android.widget.ListView.makeAndAddView(ListView.java:1970)
                                                                                        at android.widget.ListView.fillDown(ListView.java:704)
                                                                                        at android.widget.ListView.fillFromTop(ListView.java:765)
                                                                                        at android.widget.ListView.layoutChildren(ListView.java:1716)
                                                                                        at android.widget.AbsListView.onLayout(AbsListView.java:2161)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                        at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                                                                                        at android.view.View.layout(View.java:17523)
                                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                                        at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我认为错误发生在customArrayAdapter中,但我无法弄明白确切的位置

0 个答案:

没有答案