Note that there are some explanatory texts on larger screens.

plurals
  1. POShow some values from database android?
    primarykey
    data
    text
    <p>I have 4 class. 1:databasehandler</p> <pre><code>package as.d.d; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHandler extends SQLiteOpenHelper{ private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "contactsManager"; private static final String TABLE_CONTACTS = "contacts"; private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_PH_NO = "phone_number"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); onCreate(db); } void addContact(StudentInfo studentInfo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, studentInfo.getName()); // Contact Name values.put(KEY_PH_NO, studentInfo.getPhoneNumber()); // Contact Phone db.insert(TABLE_CONTACTS, null, values); db.close(); } StudentInfo getContact(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PH_NO }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null); if (cursor != null) cursor.moveToFirst(); StudentInfo studentInfo = new StudentInfo(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)); // return contact return studentInfo; } public List&lt;StudentInfo&gt; getAllStudentInfo() { List&lt;StudentInfo&gt; studentList = new ArrayList&lt;StudentInfo&gt;(); String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { StudentInfo studentInfo = new StudentInfo(); studentInfo.setID(Integer.parseInt(cursor.getString(0))); studentInfo.setName(cursor.getString(1)); studentInfo.setPhoneNumber(cursor.getString(2)); studentList.add(studentInfo); } while (cursor.moveToNext()); } return studentList; } public int updateContact(StudentInfo studentInfo) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, studentInfo.getName()); values.put(KEY_PH_NO, studentInfo.getPhoneNumber()); return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(studentInfo.getID()) }); }} </code></pre> <p>2:StudentInfo</p> <pre><code>package as.d.d; public class StudentInfo { int _id; String _name; String _phone_number; public StudentInfo(){ } public StudentInfo(int id, String name, String _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } public StudentInfo(String name, String _phone_number){ this._name = name; this._phone_number = _phone_number; } public int getID(){ return this._id; } public void setID(int id){ this._id = id; } public String getName(){ return this._name; } public void setName(String name){ this._name = name; } public String getPhoneNumber(){ return this._phone_number; } public void setPhoneNumber(String phone_number){ this._phone_number = phone_number; }} </code></pre> <p>3:add</p> <pre><code>package as.d.d; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class add extends Activity implements OnClickListener{ Button b1,b2; EditText e2,e3; TextView t1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); e2=(EditText)findViewById(R.id.editText2); e3=(EditText)findViewById(R.id.editText3); t1=(TextView)findViewById(R.id.AtextView1); b1.setOnClickListener(this); b2.setOnClickListener(this); DatabaseHandler db = new DatabaseHandler(this); List&lt;StudentInfo&gt; studentInfo = db.getAllStudentInfo(); for (StudentInfo cn : studentInfo){ t1.setText("Id:"+(cn.getID()+1)); } } @Override public void onClick(View v) { if(v==b2){ startActivity(new Intent(add.this, FdActivity.class)); } else if(v==b1){ String s2=e2.getText().toString(); String s3=e3.getText().toString(); if(s2.trim().equals("")||s3.trim().equals("")){ Toast.makeText(getApplicationContext(), "Please Submit Student Information",Toast.LENGTH_SHORT).show(); } else{ DatabaseHandler db = new DatabaseHandler(this); Log.d("Insert: ", "Inserting .."); db.addContact(new StudentInfo(s2,s3)); Log.d("Reading: ", "Reading all contacts.."); List&lt;StudentInfo&gt; studentInfo = db.getAllStudentInfo(); for (StudentInfo cn : studentInfo){ t1.setText("Id:"+(cn.getID()+1)); }}}}} </code></pre> <p>4:addmark</p> <pre><code>package as.d.d; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.TextView; import android.widget.Toast; public class addmark extends Activity{ AutoCompleteTextView a1; TextView t1,t2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addmark); a1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); t1=(TextView)findViewById(R.id.amtextView1); t2=(TextView)findViewById(R.id.amtextView2); DatabaseHandler db = new DatabaseHandler(this); final List&lt;StudentInfo&gt; studentInfo = db.getAllStudentInfo(); final ArrayList&lt;String&gt; s1 = new ArrayList&lt;String&gt;(); final ArrayList&lt;String&gt; s3 = new ArrayList&lt;String&gt;(); for (StudentInfo cn : studentInfo) { s1.add(cn.getName()); s3.add(cn.getPhoneNumber()); } ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_dropdown_item_1line,s1); a1.setThreshold(1); a1.setAdapter(adapter); a1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { } }); </code></pre> <p>In 3rd class(add) I am using a Textview for autogenerated student id(numerical(int) and two text field for submit student name and phone number into database by pressing a button.Here the Student Id,Name and phone number submitted succesfully.In fourth class I use an autocomplete textview for showing the names which I submitted from class 3.When I select a name suppose Farhan(which I already submitted into database and the id is 4 and phone number is 99876 stored in database) then I want to see the id and phone number in two textview(TextView t1,t2;)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload