-1

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

                }

                });
        }

}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
M.M.Rame
  • 113
  • 1
  • 9

2 Answers2

0

Obviously you will give same value. use getItemAtPosition(position); to get the exact item on which you have clicked.

suraj shukla
  • 258
  • 1
  • 3
  • 14
  • where i should use it? after onItemClick it didn't work – M.M.Rame May 30 '14 at 15:48
  • use `Object o = ListView.getItemAtPosition(position); String str=(String)o;//As you are using Default String Adapter Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();` – suraj shukla May 30 '14 at 15:55
  • Thx suraj for being interrested by my question but also it didn't work :Cannot make a static reference to the non-static method getItemAtPosition(int) from the type AdapterView – M.M.Rame May 30 '14 at 16:08
  • check this [link](http://stackoverflow.com/questions/9097723/adding-a-onclicklistener-to-listview-android) – suraj shukla May 30 '14 at 16:09
  • also note that i want to call the number of the friend's name clicked not to Toast its name.... – M.M.Rame May 30 '14 at 16:10
  • better use custom adapter for this. then you will be able to get the phoneNumber of exact location and also will be able to call the number – suraj shukla May 30 '14 at 16:18
  • the customized adapter isn't also an option because the phone number must not be displayed in the list (i make it appear trying to maketext...toString but also didn't work) I've checked the link that you gave it to me:I still confused – M.M.Rame May 30 '14 at 16:33
  • use custom adapter and custom listview. it will solve most of the problem. – suraj shukla May 30 '14 at 16:37
0

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);
Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50