从ListView拨打电话(OnitemClick)

时间:2015-11-18 08:59:32

标签: javascript java android listview

我想尝试从ListView拨打电话,(将“Toast”更改为直接拨打ListView)。

我希望你们帮助我。

ContactListActiviy.java

package com.ngohung.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.markupartist.android.widget.ActionBar;
import com.ngohung.example.adapter.ExampleContactAdapter;
import com.ngohung.example.models.ExampleContactItem;
import com.ngohung.example.models.ExampleDataSource;
import com.ngohung.widget.ContactItemComparator;
import com.ngohung.widget.ContactItemInterface;
import com.ngohung.widget.ContactListView;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ContactListActivity extends Activity implements TextWatcher {

    private ExampleContactListView listview;

    private EditText searchBox;
    private String searchString;

    private Object searchLock = new Object();
    boolean inSearchMode = false;

    private final static String TAG = "com.ngohung.view.ContactListActivity";

    List<ContactItemInterface> contactList;
    List<ContactItemInterface> filterList;
    private SearchListTask curSearchTask = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);

        final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);

        actionBar.setTitle("Layanan Publik Kendari");
        actionBar.setHomeLogo(R.drawable.ic_launcher);


        filterList = new ArrayList<ContactItemInterface>();
        contactList = ExampleDataSource.getSampleContactList();

        ExampleContactAdapter adapter = new ExampleContactAdapter(this, R.layout.example_contact_item, contactList);

        listview = (ExampleContactListView) this.findViewById(R.id.listview);
        listview.setFastScrollEnabled(true);
        listview.setAdapter(adapter);

        // use this to process individual clicks
        // cannot use OnClickListener as the touch event is overrided by IndexScroller
        // use last touch X and Y if want to handle click for an individual item within the row
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v, int position,
                    long id) {
                List<ContactItemInterface> searchList = inSearchMode ? filterList : contactList ;

                float lastTouchX = listview.getScroller().getLastTouchDownEventX();
                if(lastTouchX < 45 && lastTouchX > -1){
                    Toast.makeText(ContactListActivity.this, "User image is clicked ( " + searchList.get(position).getItemForIndex()  + ")", Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(ContactListActivity.this, "Nomor: " + searchList.get(position).getItemForIndex() , Toast.LENGTH_SHORT).show();
            }
        });

        //My Trial to Add Call

        //listview.setOnClickListener(new View.OnClickListener() {

          //  @Override
            //public void onClick(View arg0) {
                // TODO Auto-generated method stub
              //  String phone_no= listview.getScroller().toString().replaceAll("-", ""); //Create "getNumber" exampleContactListView
                //Intent callIntent = new Intent(Intent.ACTION_CALL);
                //callIntent.setData(Uri.parse(phone_no));
                //callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                //startActivity(callIntent);
            //}
       // });

        //END Trial


        searchBox = (EditText) findViewById(R.id.input_search_query);
        searchBox.addTextChangedListener(this);
    }

    @Override
    public void afterTextChanged(Editable s) {


        searchString = searchBox.getText().toString().trim().toUpperCase();

        if(curSearchTask!=null && curSearchTask.getStatus() != AsyncTask.Status.FINISHED)
        {
            try{
                curSearchTask.cancel(true);
            }
            catch(Exception e){
                Log.i(TAG, "Fail to cancel running search task");
            }

        }
        curSearchTask = new SearchListTask();
        curSearchTask.execute(searchString); // put it in a task so that ui is not freeze
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // do nothing
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // do nothing
    }

    private class SearchListTask extends AsyncTask<String, Void, String> {



        @Override
        protected String doInBackground(String... params) {
            filterList.clear();

            String keyword = params[0];

            inSearchMode = (keyword.length() > 0);

            if (inSearchMode) {
                // get all the items matching this
                for (ContactItemInterface item : contactList) {
                    ExampleContactItem contact = (ExampleContactItem)item;

                    if ((contact.getNama().toUpperCase().indexOf(keyword) > -1) ) {
                        filterList.add(item);
                    }

                }


            } 
            return null;
        }

        protected void onPostExecute(String result) {

            synchronized(searchLock)
            {

                if(inSearchMode){

                    ExampleContactAdapter adapter = new ExampleContactAdapter(ContactListActivity.this, R.layout.example_contact_item, filterList);
                    adapter.setInSearchMode(true);
                    listview.setInSearchMode(true);
                    listview.setAdapter(adapter);
                }
                else{
                    ExampleContactAdapter adapter = new ExampleContactAdapter(ContactListActivity.this, R.layout.example_contact_item, contactList);
                    adapter.setInSearchMode(false);
                    listview.setInSearchMode(false);
                    listview.setAdapter(adapter);
                }
            }

        }
    }


}

ExampleDataSource.java (顺便说一句,我使用的是“ArrayList”)

package com.ngohung.example.models;

import java.util.ArrayList;
import java.util.List;

import com.ngohung.widget.ContactItemInterface;

public class ExampleDataSource {

    public static List<ContactItemInterface> getSampleContactList(){
         List<ContactItemInterface>  list = new  ArrayList<ContactItemInterface> ();

         list.add(new ExampleContactItem("Polda SULTRA" , "3195040" ,"Sentral PABX jl haluoleo 1" ) );
         list.add(new ExampleContactItem("Polda SULTRA" ,"3195041" , "Sentral PABX jl haluoleo 1") );
         list.add(new ExampleContactItem("Polda SULTRA" ,"3195042" , "Sentral PABX jl haluoleo 1" ) );
         list.add(new ExampleContactItem("Direktorat diklat" , "3190549" , "Sentral PABX jl haluoleo 1") );
         list.add(new ExampleContactItem("Direktorat intelkam" , "3190041", "Jl mayjen di panjaitan 1" ) );
         list.add(new ExampleContactItem("Direktorat samapta" ,"3005114",  "Jl mayjen di panjaitan") );
         list.add(new ExampleContactItem("Direktorat lantas" , "3006112" , "Jl mayjen di panjaitan" ) );
         list.add(new ExampleContactItem("Direktorat pol air" , "3007604", "Jl mayjen di panjaitan" ) );

         return list;
    }


}

谢谢..

1 个答案:

答案 0 :(得分:2)

为您的AndroidManifest文件添加<uses-permission android:name="android.permission.CALL_PHONE" />权限。

然后只需在列表项单击侦听器中启动一个活动:

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:43..." ));
startActivity(intent);

<强>更新

只需在项目中添加以上代码,然后点击:

    @Override
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        List<ContactItemInterface> searchList = inSearchMode ? filterList : contactList ;

        float lastTouchX = listview.getScroller().getLastTouchDownEventX();
        if(lastTouchX < 45 && lastTouchX > -1){
            Toast.makeText(ContactListActivity.this, "User image is clicked ( " + searchList.get(position).getItemForIndex()  + ")", Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(ContactListActivity.this, "Nomor: " + searchList.get(position).getItemForIndex() , Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:xxx" ));// replcae "xxx" with the desired number
            startActivity(intent);
        }
   }