如何在sqlitedatabase中显示从数据库到listview的特定数据

时间:2015-02-06 00:48:28

标签: android listview sqlite

我希望显示来自我的数据库的特定数据,其中客户公司下的所有项目都将显示..如何从查询结果中显示数据..如果找到的公司下面的多个项目是listview,可以列出所有数据?我是新的Android ..提前预订

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

    lv = (ListView) findViewById(R.id.listView);

    btnrefresh = (Button) findViewById(R.id.btncrefresh);
    btnrefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Cursor cursor = dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + User.TABLE, null);
            if(cursor.moveToFirst())
            {
                String comnameproj = cursor.getString(5);
                Cursor qcursor = dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + Project.TABLE, null);

                if(qcursor.getString(8).equals(comnameproj)) {

                    ProjectCrud pcrud = new ProjectCrud(ClientModule.this);

                    ArrayList<HashMap<String , String >> projectList = pcrud.getProjectList();
                    if (projectList.size()!=0){
                        //entrylist = (ListView) findViewById(R.id.list);
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                                project_Id = (TextView) view.findViewById(R.id.project_Id);
                                String projectId = project_Id.getText().toString();
                                Intent objintent = new Intent(getApplicationContext(),ProjectDetail.class);
                                objintent.putExtra("project_Id", Integer.parseInt( projectId ));
                                startActivity(objintent);
                            }
                        });

                        ListAdapter adapter = new SimpleAdapter(ClientModule.this,projectList, R.layout.project_entry, new String []{"id", "title"}, new int[]{R.id.project_Id, R.id.project_title});
                        lv.setAdapter(adapter);
                    }else {
                        Toast.makeText(ClientModule.this, "No Project!", Toast.LENGTH_LONG).show();
                    }




                }
            }

        }
    });

这是我的表

public class DBHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 4;



private static final String DATABASE_NAME = "mobileorg.db";

public DBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){

    String CREATE_TABLE_PROJECT = "CREATE TABLE " + Project.TABLE + "("
            + Project.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Project.KEY_title + " TEXT,"
            + Project.KEY_type + " TEXT,"
         //   + Project.KEY_priority + " INTEGER,"
            + Project.KEY_timeframe + " TEXT,"
            + Project.KEY_start + " TEXT,"
            + Project.KEY_end + " TEXT,"
            + Project.KEY_cost + " INTEGER,"
            + Project.KEY_status + " TEXT,"
            + Project.KEY_companyproj + " TEXT" + ");";

    db.execSQL(CREATE_TABLE_PROJECT);

    String CREATE_TABLE_USER = "CREATE TABLE " + User.TABLE + "("
            + User.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + User.KEY_priv +" TEXT,"
            + User.KEY_username + " TEXT,"
            + User.KEY_usertype + " TEXT,"
            + User.KEY_sex + " TEXT,"
            + User.KEY_usercompany + " TEXT,"
            + User.KEY_usermail + " TEXT,"
            + User.KEY_usercontact + " INTEGER,"
            + User.KEY_useradd + " TEXT,"
            + User.KEY_loguser + " TEXT,"
            + User.KEY_logpass + " TEXT,"
            + User.KEY_logconfirm + " TEXT" + ");";

    db.execSQL(CREATE_TABLE_USER);

    String CREATE_TABLE_COMPANY = "CREATE TABLE " + Company.TABLE + "("
            + Company.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Company.KEY_cname + " TEXT,"
            + Company.KEY_cstanding + " TEXT,"
            + Company.KEY_crepres + " TEXT,"
            + Company.KEY_ccontact + " TEXT,"
            + Company.KEY_cemail + " TEXT" + ");";

    db.execSQL(CREATE_TABLE_COMPANY);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

    db.execSQL("DROP TABLE IF EXIST" + Project.TABLE);
    db.execSQL("DROP TABLE IF EXIST" + User.TABLE);
    db.execSQL("DROP TABLE IF EXIST" + Company.TABLE);


    onCreate(db);
}

public static boolean validateUser(String loguser, String logpass){

    return false;

}


}

这是我更新的查询版本..但没有任何事情发生..没有任何内容显示在我的列表视图中..

public class ClientModule extends ActionBarActivity {

private DBHelper dbHelper;

Button btnrefresh;
ListView lv ;
TextView project_Id;
String comnameproj;


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

    dbHelper = new DBHelper(ClientModule.this);

    lv = (ListView) findViewById(R.id.listView);

    btnrefresh = (Button) findViewById(R.id.btncrefresh);
    btnrefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Cursor cursor = dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + User.TABLE, null);
            if(cursor.moveToFirst()) {
                do {
                    comnameproj = cursor.getString(5);
                } while (cursor.moveToNext());

            }

                Cursor qcursor = dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + Project.TABLE, null);
                if (qcursor.moveToFirst()) {
                do {

                    if (qcursor.getString(8).equals(comnameproj)) {

                        ProjectCrud pcrud = new ProjectCrud(ClientModule.this);

                        ArrayList<HashMap<String, String>> projectList = pcrud.getProjectList();
                        if (projectList.size() != 0) {
                            //entrylist = (ListView) findViewById(R.id.list);
                            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    project_Id = (TextView) view.findViewById(R.id.project_Id);
                                    String projectId = project_Id.getText().toString();
                                    Intent objintent = new Intent(getApplicationContext(), ProjectDetail.class);
                                    objintent.putExtra("project_Id", Integer.parseInt(projectId));
                                    startActivity(objintent);
                                }
                            });

                            ListAdapter adapter = new SimpleAdapter(ClientModule.this, projectList, R.layout.refresh_project, new String[]{"id", "title"}, new int[]{R.id.project_Id, R.id.project_title});
                            lv.setAdapter(adapter);
                        } else {
                            Toast.makeText(ClientModule.this, "No Project!", Toast.LENGTH_LONG).show();
                        }

                    }


                }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_client_module, 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.exit_client) {

        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

1 个答案:

答案 0 :(得分:1)

您需要修改SQLite SELECT查询。由于您要选择特定用户拥有的项目列表,您应该编写如下所示的查询(我假设您在名为String的{​​{1}}中指定了用户的项目名称):

userCompany

如果有帮助,请告诉我。