samedi 7 mars 2015

how to retrieve specific data from sqlite using android listview?

I'm a newbie on android, I am trying to create a simple phonebook school project, which has a database and listview, what i wanted to do here is that when i click the lists(persons) on the listView it will open a new activity that shows only their phonenumber. can someone help me figure out the codes.its been a days looking for a solution, great thanks! :)



This is my list view acticity:


----------
public class Kitoy extends Activity{

ListView listView;
private DBAdapter DBhelper;
String name,con,add;


int howmany;
String[] recdata;

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



listView = (ListView) findViewById(R.id.listview1);
DBhelper = new DBAdapter(this);
DBhelper.open();


final Cursor getall = DBhelper.fetchAllRows();
howmany = getall.getCount();

recdata = new String[howmany];
int x = 0;

getall.moveToFirst();
while (!getall.isAfterLast()) {
name= getall.getString(getall.getColumnIndex(DBAdapter.NAME)).toString();
//con = getall.getString(getall.getColumnIndex(DBAdapter.CONTACT)).toString();
// add = getall.getString(getall.getColumnIndex(DBAdapter.ADDRESS)).toString();

recdata[x] = name + con;

x++;
getall.moveToNext();
}


final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, recdata);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {


public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub

Intent i = new Intent (view.getContext(), Sapero.class);
i.putExtra("item", adapter.getItem(position));

// i.putExtra("con", Cursor.FIELD_TYPE_STRING(getall.getAllRecords(DBAdapter.CONTACT)));
//i.putExtra(DBAdapter.KEY_ROWID, getall.getLong(getall.getColumnIndex(DBAdapter.KEY_ROWID)));

startActivity(i);


};
});
}
and this is my second activity to open:


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

call = (Button) findViewById(R.id.button_call);
sms = (Button) findViewById(R.id.button_sms);
delete = (Button) findViewById(R.id.delete);

// id = getIntent ().getLongExtra(DBAdapter.KEY_ROWID, id);
//contact =getIntent ().getStringExtra(DBAdapter.CONTACT);

contact = getIntent ().getStringExtra("con");

passedView = (TextView)findViewById(R.id.passed);
passedView.setText("" + contact);

sms.setOnClickListener(new OnClickListener() {


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

String phoneNo = passedView.getText().toString();

// smsManager.sendTextMessage(phoneNo, null,null, null, null);
Intent i = new Intent (Intent.ACTION_VIEW);
i.putExtra("address", phoneNo);
i.putExtra("sms_body", "");
i.setType("vnd.android-dir/mms-sms");
startActivity(i);
}
catch (Exception e){
Toast.makeText(getApplicationContext(),
"SMS, failed, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();

}
}
} );

Aucun commentaire:

Enregistrer un commentaire