在列表视图中调用onItemClick

时间:2014-05-30 15:17:13

标签: android sqlite uri phone-call onitemclick

在我的Android应用中,我创建了一个包含SQLite数据库

(_ id,FRIEND_NAME,FRIEND_PLACE(详细地址),FRIEND_PHONE,FRIEND_LOC(位置),FRIEND_CATEG)

此外,我实施了一个列表视图,它显示了所有FRIEND_NAME(来自数据库)。

我想在点击他的名字时给所选的朋友打电话。

尽管拨打了他的电话号码,但该应用程序始终将此号码称为“74663”。

事实上,我认为问题在于变量“phone”的分配,所以当我使用(Intent.ACTION_CALL,Uri.parse(手机))时,它每次给出相同的数字(74663)(也许当它会“解析uri”或在数据库列表视图的光标中。)

1)问题出在哪里?

2)为什么总是拨打号码74663拨打

3)您能否(请)告诉我如何分配可变电话以获取点击的FRIEND_NAME的FRIEND_PHONE?

这是班级的代码

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

                }

                });
        }

}

2 个答案:

答案 0 :(得分:0)

显然你会给出相同的价值。 使用getItemAtPosition(position);获取您点击的确切项目。

答案 1 :(得分:0)

为姓名和电话创建两个单独的列表。

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

更改代码的这一部分:

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

names设置为适配器的数据源。

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

并在列表中访问相应的电话号码,如下所示:

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