用于填充ListView的ArrayList方法

时间:2016-10-18 12:34:44

标签: android listview arraylist

我很难理解我该怎么做,以便从数据库中获取信息并将其放在ListView上。我已经有一个来自我的数据库类的方法,它查找特定的信息。它应该返回该行的所有列。

数据库方法:

public List<alvara_db> getAlvaras(){
    SQLiteDatabase db = this.getReadableDatabase();

    List<alvara_db> projects = new ArrayList<>(); //SELECT * FROM TABLE_ALVARA WHERE projetoid = ?
    String[] project = new String[]{String.valueOf(id_projeto)};

    Cursor cur = db.rawQuery("SELECT * FROM alvara WHERE projetoid = ?", project);
    if (cur.moveToFirst()){
        do {
            alvara_db alvaras = new alvara_db();
            alvaras.setPlaca(cur.getInt(cur.getColumnIndex("placa")));
            alvaras.setProj_exec(cur.getInt(cur.getColumnIndex("proj_exec")));
            alvaras.setResp_tec(cur.getString(cur.getColumnIndex("resp_tec")));
            alvaras.setParecer(cur.getString(cur.getColumnIndex("parecer")));
            projects.add(alvaras);
        }while(cur.moveToNext());

        return projects;
    }else{
        return null;
    }
}

此处调用此方法(突出显示代码):

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detalhes_projeto);
    listView_projectDetails = (ListView)findViewById(R.id.detalhes);

    TextView textView_data = (TextView) findViewById(R.id.textView31);
    TextView textView_Proc = (TextView) findViewById(R.id.textView33);
    TextView textView_Requer = (TextView) findViewById(R.id.textView35);

    projeto_db db = new projeto_db(this);
    Bundle bun2 = getIntent().getExtras();
    if (bun2 != null) {
        int position = bun2.getInt("position");

        id_projeto = db.getUserProjects().get(position).getProj_id();
        tipo_obra = db.getUserProjects().get(position).getTipo();
        textView_data.setText(db.getUserProjects().get(position).getData());
        textView_Proc.setText(db.getUserProjects().get(position).getProcesso());
        textView_Requer.setText(db.getUserProjects().get(position).getRequeren());

        ListAdapter adapterlist = null;
        if(tipo_obra == 1){ *****HERE*****
            adapterlist = new ArrayAdapter<>(
                    this,
                    android.R.layout.simple_list_item_1,
                    db.getAlvaras()
            );

        }else{
            adapterlist = new ArrayAdapter<>(
                    this,
                    android.R.layout.simple_list_item_1,
                    db.getHabiteses()
            );

        }
        listView_projectDetails.setAdapter(adapterlist); *****I SET THE INFO FROM THE ARRAY LIST HERE*****
    }

    Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(my_toolbar);

    getSupportActionBar().setTitle(R.string.detalhes_projeto);
}

在屏幕上,它会返回一个位置,其中包含&#34;我的包裹的名称,带有getters / setter的类别@ something&#34;虽然我希望它返回与projetoid = project相关的行的列中的所有信息。

提前致谢!

编辑:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.relatoriodeobras, PID: 3160
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.relatoriodeobras/com.example.relatoriodeobras.detalhes_projeto}: java.lang.IllegalArgumentException: column '_id' does not exist
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
                  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
                  at android.widget.CursorAdapter.init(CursorAdapter.java:172)
                  at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
                  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
                  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
                  at com.example.relatoriodeobras.detalhes_projeto.onCreate(detalhes_projeto.java:64)
                  at android.app.Activity.performCreate(Activity.java:5990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

0 个答案:

没有答案