显示TextView的数据源码 [英] show data sqlite in textview

查看:168
本文介绍了显示TextView的数据源码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示TextView的数据sqlite的,但我不不,我该怎么办?

LoginDataBaseAdapter.java

 包com.example.audevardlast;
进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.util.Log;公共类LoginDataBaseAdapter
{
        静态最后弦乐DATABASE_NAME =login.db;
        静态最终诠释DATABASE_VERSION = 1;
        公共静态最终诠释NAME_COLUMN = 1;
        // TODO:在您的表中每一列创建的公共领域。
        // SQL语句创建一个新的数据库。        静态最后弦乐DATABASE_CREATE =CREATE TABLE+LOGIN+(
                +ID+INTEGER PRIMARY KEY AUTOINCREMENT,+USERNAME+TEXT
                +preNOM+TEXT
                +EMAIL+TEXT
                +得分+TEXT
                +code+TEXT);;        //变量来存放数据库实例
        公共SQLiteDatabase分贝;
        //使用该数据库的应用程序的语境。
        私人最终上下文的背景下;
        //数据库打开/升级助手
        私人DataBaseHelper dbHelper;
        公共LoginDataBaseAdapter(上下文_context)
        {
            上下文= _context;
            dbHelper =新DataBaseHelper(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }
        公共LoginDataBaseAdapter的open()抛出的SQLException
        {
            DB = dbHelper.getWritableDatabase();
            返回此;
        }
        公共无效的close()
        {
            db.close();
        }        公共SQLiteDatabase getDatabaseInstance()
        {
            返回分贝;
        }        公共无效insertEntry(用户名字符串,字符串preNOM,字符串邮箱,字符串CP,串点)
        {
           ContentValues​​ newValues​​ =新ContentValues​​();
            //指定值的每一行。
            newValues​​.put(username的用户名);
            newValues​​.put(preNOM,preNOM);
            newValues​​.put(EMAIL,邮件);
            newValues​​.put(code,CP);
            newValues​​.put(得分,点);
            //将该行插入你的表
            db.insert(登录,空,newValues​​);
            ///Toast.makeText(context,提醒保存成功,Toast.LENGTH_LONG).show();
        }
        公众诠释deleteEntry(用户名字符串)
        {
            //字符串ID =将String.valueOf(ID);
            字符串,其中=USERNAME =?;
            INT numberOFEntriesDeleted = db.delete(登录,在那里,新的String [] {用户名});
           // Toast.makeText(背景下,数字FO项中删除成功:+ numberOFEntriesDeleted,Toast.LENGTH_LONG).show();
            返回numberOFEntriesDeleted;
        }
        公共字符串getSinlgeEntry(用户名字符串)
        {
            光标光标= db.query(登录,NULL,USERNAME =?,新的String [] {用户名},NULL,NULL,NULL);
            如果(cursor.getCount()&。1)//用户名不存在
            {
                cursor.close();
                返回不存在;
            }
            cursor.moveToFirst();
            字符串密码= cursor.getString(cursor.getColumnIndex(USERNAME));
            cursor.close();
            返回密码;
        }
        公共无效updateEntry(用户名字符串,字符串preNOM,字符串邮箱,字符串CP,串点)
        {
            //定义更新的行的内容。
            ContentValues​​ updatedValues​​ =新ContentValues​​();
            //指定值的每一行。
            updatedValues​​.put(username的用户名);
            updatedValues​​.put(preNOM,preNOM);
            updatedValues​​.put(EMAIL,邮件);
            updatedValues​​.put(code,CP);
            updatedValues​​.put(得分,点);            字符串,其中=EMAIL =?;
            db.update(登录,updatedValues​​,在那里,新的String [] {用户名});
        }}

SignUPActivity.java

 包com.example.audevardlast;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;公共类SignUPActivity扩展活动
{
    的EditText editTextUserName,EDITTEXT preNOM,editTextEmail,EDITTEXT codePostal,editTextPoint;
    按钮btnCreateAccount;
    最后弦乐得分=得分;    LoginDataBaseAdapter loginDataBaseAdapter;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.signup);
         意向意图= getIntent();
          的EditText loginDisplay =(EditText上)findViewById(R.id.editText2);           如果(意向!= NULL)
           {
               loginDisplay.setText(intent.getStringExtra(SCORE));
           }
        //获取数据库适配器的实例
        loginDataBaseAdapter =新LoginDataBaseAdapter(本);
        loginDataBaseAdapter = loginDataBaseAdapter.open();        //获取意见的参考文献的
        editTextUserName =(EditText上)findViewById(R.id.editTextUserName);
        EDITTEXT preNOM =(EditText上)findViewById(R.id.editTextPassword);
        editTextEmail =(EditText上)findViewById(R.id.editTextMail);
        EDITTEXT codePostal =(EditText上)findViewById(R.id.editText1);
        editTextPoint =(EditText上)findViewById(R.id.editText2);        editTextPoint.setVisibility(View.INVISIBLE);        btnCreateAccount =(按钮)findViewById(R.id.buttonCreateAccount);
        btnCreateAccount.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根            字符串username = editTextUserName.getText()的toString()。
            。字符串preNOM = EDITTEXT prenom.getText()的toString();
            。字符串的邮件= editTextEmail.getText()的toString();            。字符串CP = EDITTEXT codePostal.getText()的toString();
            。串点= editTextPoint.getText()的toString();
            //检查是否有字段是vaccant
            如果(userName.equals()|| prenom.equals())
            {
                    Toast.makeText(getApplicationContext(),现场Vaccant,Toast.LENGTH_LONG).show();
                    返回;
            }            其他
            {
                //将数据保存到数据库
                loginDataBaseAdapter.insertEntry(用户名,preNOM,邮件,CP点);
                Toast.makeText(getApplicationContext(),账户成功塑造了,Toast.LENGTH_LONG).show();
            }
        }
    });
}
    @覆盖
    保护无效的onDestroy(){
        // TODO自动生成方法存根
        super.onDestroy();        loginDataBaseAdapter.close();
    }
}

我想我的源码的总数据出现在一个TextView在homeActivity ...

HomeActivity.java

 包com.example.audevardlast;
进口android.app.Activity;
进口android.app.Dialog;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;公共类HomeActivity扩展活动
{
    按钮btnSignIn,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;
    最后弦乐得分=得分;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.main);         意向意图= getIntent();
           TextView的loginDisplay =(的TextView)findViewById(R.id.textView1);           如果(意向!= NULL)
           {
               loginDisplay.setText(intent.getStringExtra(SCORE));
           }
         //创建SQLite数据库的实例
         loginDataBaseAdapter =新LoginDataBaseAdapter(本);
         loginDataBaseAdapter = loginDataBaseAdapter.open();         //获取REFFERENCE按钮
         btnSignIn =(按钮)findViewById(R.id.buttonSignIN);
         btnSignUp =(按钮)findViewById(R.id.buttonSignUP);
        //设置的OnClick监听器上的按钮注册
        btnSignUp.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            // TODO自动生成方法存根            ///为SignUpActivity创建意向,并开始活动
            意图int​​entSignUP =新意图(getApplicationContext(),SignUPActivity.class);
            TextView的score1 =(的TextView)findViewById(R.id.textView1);            intentSignUP.putExtra(SCORE,score1.getText()的toString());
            startActivity(intentSignUP);
            }
        });
    }
    // Methos到handleClick事件的登录按钮中
    公共无效签到(查看V)
       {
            最后对话的对话=新的对话框(HomeActivity.this);
            dialog.setContentView(R.layout.login);
            dialog.setTitle(登录);            //获取视图的参考文献的
            最终的EditText editTextUserName =(EditText上)dialog.findViewById(R.id.editTextUserNameToLogin);            按钮btnSignIn =(按钮)dialog.findViewById(R.id.buttonSignIn);            //设置ClickListener
            btnSignIn.setOnClickListener(新View.OnClickListener(){                公共无效的onClick(视图v){
                    //获取用户名和密码
                    字符串username = editTextUserName.getText()的toString()。
                    //为获取相应的用户名的密码形式数据库
                    字符串storedPassword = loginDataBaseAdapter.getSinlgeEntry(用户名);                    //检查存储的密码与密码匹配由用户输入
                    如果(userName.equals(storedPassword))
                    {
                         意向意图=新意图(HomeActivity.this,ProfilActivity.class);
                            TextView的score1 =(的TextView)findViewById(R.id.textView1);                             intent.putExtra(SCORE,score1.getText()的toString());                            startActivity(意向);
                    }
                    其他
                    {
                        Toast.makeText(HomeActivity.this,用户名或密码不匹配,Toast.LENGTH_LONG).show();
                    }
                }
            });            dialog.show();
    }    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        //关闭数据库
        loginDataBaseAdapter.close();
    }
}


解决方案

您可以声明适配器一样,

 进口的java.util.ArrayList;
进口android.annotation.Sup pressLint;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.TextView;@燮pressLint(InflateParams)
公共类UserList的延伸BaseAdapter
{
    私人语境mContext;
    私人的ArrayList<串GT; ID;
    私人的ArrayList<串GT;电子邮件ID;
    私人的ArrayList<串GT;名字;
    私人的ArrayList<串GT;姓;    公众的UserList(上下文C,ArrayList的<弦乐> ID,ArrayList的<串GT; FNAME,ArrayList的<串GT; LNAME,ArrayList的<串GT; EMAILID)
    {
        this.mContext = C;        this.id = ID;
        this.emailID = EMAILID;
        this.firstName = FNAME;
        this.lastName = LNAME;
    }
    公众诠释的getCount(){
        返回id.size();
    }    公共对象的getItem(INT位置){
        返回null;
    }    众长getItemId(INT位置){
        返回0;
    }@覆盖
公共查看getView(INT POS,查看孩子的ViewGroup ARG2){
    持有人mHolder;
    LayoutInflater layoutInflater;
    如果(孩子== NULL){
        layoutInflater =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        孩子= layoutInflater.inflate(R.layout.listcell,NULL);
        mHolder =新持有人();
        //mHolder.txt_id =(TextView中)child.findViewById(R.id.txt_id);
        mHolder.txt_userName =(TextView中)child.findViewById(R.id.txt_userName);
        mHolder.txt_fName =(TextView中)child.findViewById(R.id.txt_fName);
        mHolder.txt_lName =(TextView中)child.findViewById(R.id.txt_lName);
        child.setTag(mHolder);
    }其他{
        mHolder =(座)child.getTag();
    }
    //mHolder.txt_id.setText(id.get(pos));
    mHolder.txt_userName.setText(emailID.get(POS));
    mHolder.txt_fName.setText(firstName.get(POS));
    mHolder.txt_lName.setText(lastName.get(POS));
    返回子女;
}公共类持有人
{
    // TextView的txt_id;
    TextView的txt_userName;
    TextView的txt_fName;
    TextView的txt_lName;
}

}

,之后写你的活动code类的东西。

 包com.example.registrationapp;进口的java.util.ArrayList;进口android.app.Activity;
进口android.app.AlertDialog;
 进口android.content.DialogInterface;
 进口android.content.Intent;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.ListView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.AdapterView.OnItemLongClickListener;公共类首页扩展活动
{
私人DatabaseActivity mHelper;
私人SQLiteDatabase数据库;
私人AlertDialog.Builder构建;私人的ArrayList<串GT;用户id =新的ArrayList<串GT;();
私人的ArrayList<串GT;名字=新的ArrayList<串GT;();
私人的ArrayList<串GT;姓氏=新的ArrayList<串GT;();
私人的ArrayList<串GT; EMAILID =新的ArrayList<串GT;();私人的ListView用户列表;保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.display_activity);    用户列表=(ListView控件)findViewById(R.id.List);    mHelper =新DatabaseActivity(本);@覆盖
保护无效onResume()
{
    displayData();
    super.onResume();
}私人无效displayData()
{
    数据库= mHelper.getWritableDatabase();
    光标mCursor = dataBase.rawQuery(SELECT * FROM+ DatabaseActivity.TABLE_NAME,NULL);    明确();    如果(mCursor.moveToFirst())
    {
        做
        {
            userId.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_ID)));
            FirstName.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_FNAME)));
            LastName.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_LNAME)));
            EmailID.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_EMAIL_ID)));        }而(mCursor.moveToNext());
    }
    UserList的disadpt =新的UserList(HomePage.this,用户id,姓氏,EMAILID,名字);
    userList.setAdapter(disadpt);
    mCursor.close();
}公共无效清除()
{
    userId.clear();
    FirstName.clear();
    LastName.clear();
    EmailID.clear();
}
}

和让你的XML文件类似的东西。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=center_vertical
机器人:方向=横向><的TextView
    机器人:ID =@ + ID / txt_userName
    机器人:layout_width =0dp
    机器人:layout_height =19dp
    机器人:layout_weight =2
    机器人:比重=中心/><的TextView
    机器人:ID =@ + ID / txt_fName
    机器人:layout_width =0dp
    机器人:layout_height =19dp
    机器人:layout_weight =2/><的TextView
    机器人:ID =@ + ID / txt_lName
    机器人:layout_width =0dp
    机器人:layout_height =19dp
    机器人:layout_weight =3.51
    机器人:重力=左/> < / LinearLayout中>

和列表视图声明这样

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=CENTER_HORIZONTAL><查看
    机器人:ID =@ + ID /一
    机器人:layout_width =match_parent
    机器人:layout_height =0dp
    机器人:paddingBottom会=10dp/>< ListView控件
    机器人:ID =@ + ID /列表
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>< / RelativeLayout的>

希望这有助于....

I would like show the data sqlite in the textview but i don't no how i do ?

LoginDataBaseAdapter.java

package com.example.audevardlast;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class LoginDataBaseAdapter 
{
        static final String DATABASE_NAME = "login.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.

        static final String DATABASE_CREATE = "CREATE TABLE " + "LOGIN" + " ("
                + "ID" + " INTEGER PRIMARY KEY AUTOINCREMENT, " + "USERNAME" + " TEXT, "
                +"PRENOM" + " TEXT, "
                +"EMAIL" + " TEXT, "
                +"SCORE" + " TEXT, "
                + "CODE" + " TEXT );";

        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }



        public void insertEntry(String userName,String prenom,String mail,String cp,String point)
        {
           ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", userName);
            newValues.put("PRENOM",prenom);
            newValues.put("EMAIL",mail);
            newValues.put("CODE",cp);
            newValues.put("SCORE",point);




            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String UserName)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }   
        public String getSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("USERNAME"));
            cursor.close();
            return password;                
        }


        public void  updateEntry(String userName,String prenom,String mail, String cp,String point)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put("USERNAME", userName);
            updatedValues.put("PRENOM",prenom);
            updatedValues.put("EMAIL",mail);
            updatedValues.put("CODE",cp);
            updatedValues.put("SCORE",point);



            String where="EMAIL = ?";
            db.update("LOGIN",updatedValues, where, new String[]{userName});               
        }



}

SignUPActivity.java

package com.example.audevardlast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignUPActivity extends Activity
{
    EditText editTextUserName,editTextPrenom,editTextEmail,editTextCodePostal,editTextPoint;
    Button btnCreateAccount;
    final String SCORE="score";

    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
         Intent intent = getIntent();
          EditText loginDisplay = (EditText) findViewById(R.id.editText2);

           if (intent != null)
           {
               loginDisplay.setText(intent.getStringExtra(SCORE));
           }
        // get Instance  of Database Adapter
        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();

        // Get Refferences of Views
        editTextUserName=(EditText)findViewById(R.id.editTextUserName);
        editTextPrenom=(EditText)findViewById(R.id.editTextPassword);
        editTextEmail=(EditText)findViewById(R.id.editTextMail);
        editTextCodePostal=(EditText)findViewById(R.id.editText1);
        editTextPoint=(EditText)findViewById(R.id.editText2);

        editTextPoint.setVisibility(View.INVISIBLE);

        btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
        btnCreateAccount.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String userName=editTextUserName.getText().toString();
            String prenom=editTextPrenom.getText().toString();
            String mail=editTextEmail.getText().toString();

            String cp=editTextCodePostal.getText().toString();
            String point=editTextPoint.getText().toString();




            // check if any of the fields are vaccant
            if(userName.equals("")||prenom.equals(""))
            {
                    Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                    return;
            }

            else
            {
                // Save the Data in Database
                loginDataBaseAdapter.insertEntry(userName, prenom, mail, cp, point);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
            }
        }
    });
}
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }
}

I would like total data of my sqlite appear in a textview in a homeActivity...

HomeActivity.java

package com.example.audevardlast;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity 
{
    Button btnSignIn,btnSignUp;
    LoginDataBaseAdapter loginDataBaseAdapter;
    final String SCORE="score";

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

         Intent intent = getIntent();
           TextView loginDisplay = (TextView) findViewById(R.id.textView1);

           if (intent != null)
           {
               loginDisplay.setText(intent.getStringExtra(SCORE));
           }
         // create a instance of SQLite Database
         loginDataBaseAdapter  =new LoginDataBaseAdapter(this);
         loginDataBaseAdapter=loginDataBaseAdapter.open();

         // Get The Refference Of Buttons
         btnSignIn=(Button)findViewById(R.id.buttonSignIN);
         btnSignUp=(Button)findViewById(R.id.buttonSignUP);






        // Set OnClick Listener on SignUp button 
        btnSignUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity


            Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
            TextView score1=(TextView) findViewById(R.id.textView1);

            intentSignUP.putExtra(SCORE, score1.getText().toString());
            startActivity(intentSignUP);
            }
        });
    }
    // Methos to handleClick Event of Sign In Button
    public void signIn(View V)
       {
            final Dialog dialog = new Dialog(HomeActivity.this);
            dialog.setContentView(R.layout.login);
            dialog.setTitle("Login");

            // get the Refferences of views


            final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);

            Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

            // Set On ClickListener
            btnSignIn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // get The User name and Password
                    String userName=editTextUserName.getText().toString();


                    // fetch the Password form database for respective user name
                    String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                    // check if the Stored password matches with  Password entered by user
                    if(userName.equals(storedPassword))
                    {
                         Intent intent = new Intent(HomeActivity.this, ProfilActivity.class);
                            TextView score1=(TextView) findViewById(R.id.textView1);

                             intent.putExtra(SCORE, score1.getText().toString());

                            startActivity(intent);
                    }
                    else
                    {
                        Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                    }
                }
            });

            dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Close The Database
        loginDataBaseAdapter.close();
    }
}

解决方案

you can declare adapter like that

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class UserList extends BaseAdapter
{
    private Context mContext;
    private ArrayList<String> id;
    private ArrayList<String> emailID;
    private ArrayList<String> firstName;
    private ArrayList<String> lastName;

    public UserList(Context c, ArrayList<String> id, ArrayList<String> fname, ArrayList<String> lname, ArrayList<String> emailID) 
    {
        this.mContext = c;

        this.id = id;
        this.emailID = emailID;
        this.firstName = fname;
        this.lastName = lname;
    }


    public int getCount() {
        return id.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

@Override
public View getView(int pos, View child, ViewGroup arg2) {
    Holder mHolder;
    LayoutInflater layoutInflater;
    if (child == null) {
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.listcell, null);
        mHolder = new Holder();
        //mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
        mHolder.txt_userName = (TextView) child.findViewById(R.id.txt_userName);
        mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
        mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
        child.setTag(mHolder);
    } else {
        mHolder = (Holder) child.getTag();
    }
    //mHolder.txt_id.setText(id.get(pos));
    mHolder.txt_userName.setText(emailID.get(pos));
    mHolder.txt_fName.setText(firstName.get(pos));
    mHolder.txt_lName.setText(lastName.get(pos));
    return child;
}

public class Holder 
{
    //TextView txt_id;
    TextView txt_userName;
    TextView txt_fName;
    TextView txt_lName;
}

}

and after that write your activity code something like that

package com.example.registrationapp;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class HomePage extends Activity
{
private DatabaseActivity mHelper;
private SQLiteDatabase dataBase;
private AlertDialog.Builder build;

private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> FirstName = new ArrayList<String>();
private ArrayList<String> LastName = new ArrayList<String>();
private ArrayList<String> EmailID = new ArrayList<String>();

private ListView userList;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_activity);

    userList = (ListView) findViewById(R.id.List);

    mHelper = new DatabaseActivity(this);

@Override
protected void onResume()
{
    displayData();
    super.onResume();
}

private void displayData()
{
    dataBase = mHelper.getWritableDatabase();
    Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DatabaseActivity.TABLE_NAME , null );

    clear();

    if (mCursor.moveToFirst()) 
    {
        do 
        {       
            userId.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_ID)));
            FirstName.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_FNAME)));
            LastName.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_LNAME)));
            EmailID.add(mCursor.getString(mCursor.getColumnIndex(DatabaseActivity.KEY_EMAIL_ID)));

        } while (mCursor.moveToNext());
    }
    UserList disadpt = new UserList(HomePage.this ,userId, LastName, EmailID, FirstName);
    userList.setAdapter(disadpt);
    mCursor.close();
}

public void clear()
{
    userId.clear();
    FirstName.clear();
    LastName.clear();
    EmailID.clear();
}
}

and make your xml files something like that

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
    android:id="@+id/txt_userName"
    android:layout_width="0dp"
    android:layout_height="19dp"
    android:layout_weight="2"
    android:gravity="center" />

<TextView
    android:id="@+id/txt_fName"
    android:layout_width="0dp"
    android:layout_height="19dp"
    android:layout_weight="2" />

<TextView
    android:id="@+id/txt_lName"
    android:layout_width="0dp"
    android:layout_height="19dp"
    android:layout_weight="3.51"
    android:gravity="left" />

 </LinearLayout>

and declare listview like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >

<View
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:paddingBottom="10dp" />

<ListView
    android:id="@+id/List"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

hope this helps ....

这篇关于显示TextView的数据源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆