Search code examples
androidsqliteuriphone-callonitemclick

make a call onItemClick in a List View


In my Android app, I made an SQLite database containig

(_id, FRIEND_NAME, FRIEND_PLACE(detailed address), FRIEND_PHONE, FRIEND_LOC(location), FRIEND_CATEG)

Also i implemented a List View wich displays all the FRIEND_NAMEs (from the database).

I want to call the selected friend when i click his name.

Infortunatly in despite of calling his number, the app calls always this number "74663".

In fact, I think that the problem is in the assignement of the variable "phone" so when i use (Intent.ACTION_CALL, Uri.parse(phone)) , it gives everytime this same number(74663) (maybe when it does "parse uri" or in the cursor of the database's listview).

1) Where is the problem?

2) Why it always gives the number 74663 to dial

3) can you (please) tell me how to assign the variable phone to get the FRIEND_PHONE of the clicked FRIEND_NAME?

this is the code of the class

    `package com.softeq.prepopdb.activity;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.softeq.android.prepopdb.R;
import com.softeq.prepopdb.dbhelper.ExternalDbOpenHelper;`        



//������� ��������� �������� ������� ���� ����� �� 


public class PrepopSqliteDbActivity extends ListActivity {
private static final String DB_NAME = "yourdb.sqlite";

private static final String TABLE_NAME = "friends";
private static final String FRIEND_ID = "_id";
private static final String FRIEND_NAME = "name";
public static final String FRIEND_PLACE = "place";
public static final String FRIEND_PHONE = "phone";
public static final String FRIEND_LOC = "loc";
public static final String FRIEND_CATEG = "categ";

private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> friends;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //��� �������� ������
    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();
    //��� ���� ����������
    fillFreinds();
    setUpList();      
}
//filling the list with friends// 
private void fillFreinds() {
    friends = new ArrayList<String>();
    Cursor friendCursor = database.query(TABLE_NAME,
            new String[] 
                     {FRIEND_ID, FRIEND_NAME,FRIEND_PLACE,FRIEND_PHONE,FRIEND_LOC,FRIEND_CATEG},
                     null, null,null,null, FRIEND_CATEG);

    friendCursor.moveToFirst();
    if(!friendCursor.isAfterLast()) {
        do {
            String name = friendCursor.getString(1);
            String phone= friendCursor.getString(3);
            friends.add(name);
            friends.add(phone);
            } while (friendCursor.moveToNext());
    }
    friendCursor.close();
}

//setting up the list to call (or dial) a friend's number onItemClick  

private void setUpList() {

    setListAdapter(new ArrayAdapter<String>(this,android.R.layout. simple_expandable_list_item_1 , friends));
    listView = getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,int position,long id) {

                    String phone = "tel:" + FRIEND_PHONE.toString().trim();
                    Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(phone));
                    startActivity(i);

                }

                });
        }

}

Solution

  • Create two separate lists for names and phones.

    ArrayList<String> names;
    ArrayList<String> phones;
    

    Change this portion of your code:

    do {
            String name = friendCursor.getString(1);
            String phone = friendCursor.getString(3);
            names.add(name);
            phones.add(phone);
    } while (friendCursor.moveToNext());
    

    Set names as the data source of your adapter.

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, names));
    

    And access the appropriate phone number in the list like this:

    String phone = "tel:" + phones.get(position);
    Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(phone));
    startActivity(i);