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

时间:2021-07-02 17:02:09

I am developing one android application.i want to display a data which is selected from database and display it in listview.first of all i had used static data for display Trainee(user) data. which is static. then after For same functionality i have use sqlite Database and register the Trainee(user) and now i want to display registered trainne's name in listview. i have just done below code. can anyone help me how to display trainee names in listview.

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

AddTraineeActivity.java This file works basic function of create trainee database and insert values of trainee:

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 This file contain the code which display the trainee names in listview. but this file display static users for example,trainee1,trainee2,trainee3....trainee13.

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;
}

}

now i want to display name of trainees which is stored in database. can anyone help me??

现在我想显示存储在数据库中的学员姓名。谁能帮我??

1 个解决方案

#1


0  

If you want to see how a Cursor loader can work for you, you can review the following project that I have uploaded to github: GPS Distance Tracking

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

#1


0  

If you want to see how a Cursor loader can work for you, you can review the following project that I have uploaded to github: GPS Distance Tracking

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