在android中,如何在列表视图中显示数据库中的选定数据?我正在使用sqlite数据库

时间:2015-05-15 20:39:52

标签: android database sqlite listview select

我正在开发一个android应用程序。我希望显示从数据库中选择的数据并将其显示在listview中。首先,我使用静态数据显示实习生(用户)数据。这是静态的。然后在相同的功能后我使用sqlite数据库并注册实习生(用户),现在我想在listview中显示注册的trainne的名字。我刚刚完成了下面的代码。任何人都可以帮我如何在列表视图中显示受训者姓名。

AddTraineeActivity.java 该文件的作用是创建实习数据库的基本功能,并插入实习生的值:

package com.example.gymapp;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class AddTraineeActivity extends Activity implements OnClickListener
{
EditText fn;
EditText ln;
EditText un;
EditText pwd;
EditText pno;
EditText age;
Button btnAdd;
SQLiteDatabase db = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_managetrainee);
    fn = (EditText) findViewById(R.id.etfirstname);
    ln = (EditText) findViewById(R.id.etlastname);
    age = (EditText) findViewById(R.id.edage);
    pno = (EditText) findViewById(R.id.etphoneno);
    un = (EditText) findViewById(R.id.ettraineeun);
    pwd = (EditText) findViewById(R.id.etpwdtrainee);
    btnAdd = (Button) findViewById(R.id.btnsavedata);
    db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
    db.execSQL("create table if not exists trainee(firstname text, lastname text,age varchar,phoneNumber varchar,userTrainee varchar,passwordTrainee varchar)");
    btnAdd.setOnClickListener(this);


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
} 

public void show(String str)
{
Toast.makeText(this, str, Toast.LENGTH_LONG).show();    
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v== btnAdd)
    {
        String firstname = fn.getText().toString();
        String lastname = ln.getText().toString();
        String Age = age.getText().toString();
        String phoneNumber = pno.getText().toString();
        String usernameTrainee = un.getText().toString();
        String passwordTrainee = pwd.getText().toString();
        if(firstname==null||firstname==""||firstname.length()<3)
        {
            show("Please Enter Correct Name.");
        }
        else if(lastname==null||lastname==""||lastname.length()<2)
        {
            show("Please Enter Correct Name.");
        }
        else if(Age==null||Age==""||Age.length()>3)
        {
            show("Please Enter Correct Age.");
        }
        else if(phoneNumber==null||phoneNumber==""||phoneNumber.length()<10)
        {
            show("Please Enter Correct mobile number.");
        }
        else if(usernameTrainee==null||usernameTrainee==""||usernameTrainee.length()<4)
        {
            show("Please Enter valid User name.");
        }
        else if(passwordTrainee==null||passwordTrainee==""||passwordTrainee.length()<6)
        {
            show("Please Enter Strong Password.");
        }
        else
        {
            db.execSQL("insert into trainee values('"+firstname+"','"+lastname+"','"+Age+"','"+phoneNumber+"','"+usernameTrainee+"','"+passwordTrainee+"')");
            //i=new Intent(this,Welcome.class);
            //startActivityForResult(i, 500);
            //overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); 
            db.close();
            finish();
        }
    }
}

}`

UserListActivity.java 此文件包含在listview中显示受训者姓名的代码。但此文件显示静态用户,例如trainee1,trainee2,trainee3 .... trainee13。

package com.example.gymapp;

import com.tss.constant.Constant;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.content.Context;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import com.example.gymapp.AddTraineeActivity;
import com.example.gymapp.dao.DBfitguidehelper;

public class UserListActivity extends Activity {

private ListView listViewUser;
private String loggedInType ;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_list);
    //SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    //queryBuilder.setTables(DBfitguidehelper.)
    listViewUser = (ListView)findViewById(R.id.listViewUser);
    String[] values = new String[]{"trainee", "trainee1", "trainee2", "trainee3", "trainee4", "trainee5", "trainee6", "trainee7", "trainee8", "trainee9", "trainee10", "trainee11", "trainee12", "trainee13"};
    ArrayAdapter<String> userAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listViewUser.setAdapter(userAdapter);
    listViewUser.setOnItemClickListener(new ListViewListner());
    if(savedInstanceState!=null){
        Bundle extras = getIntent().getExtras();
        loggedInType = extras.getString("loggedInType");
        System.out.println("loggedInType - " + loggedInType);
    }
} 

private class ListViewListner implements OnItemClickListener{
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "selected user is " + listViewUser.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
        Constant.Selected_Trainee = ""+listViewUser.getItemAtPosition(position);
        Intent intent = new Intent(getApplicationContext(),TrainerActivity.class);
        intent.putExtra("loggedInType", loggedInType);
        Toast.makeText(getApplicationContext(), "loggedInType"+loggedInType, Toast.LENGTH_SHORT).show();

        startActivity(intent);
    }
}

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

}

现在我想显示存储在数据库中的学员姓名。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果你想看看Cursor加载器如何为你工作,你可以查看我上传到github的以下项目:GPS Distance Tracking