How to make a call by clicking button in listview?

Jul 18 2015 5:48 AM
application adapter.java
package com.sj.jsondemo;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationAdapter extends ArrayAdapter<Application>{
private List<Application> items;

private String TAG="test";

public ApplicationAdapter(Context context, List<Application> items) {
super(context, R.layout.app_custom_list, items);
this.items = items;
}

@Override
public int getCount() {
return items.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;

if(v == null) {
LayoutInflater li = LayoutInflater.from(getContext());
v = li.inflate(R.layout.app_custom_list, null);
}



final Application app = items.get(position);

if(app != null) {
ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
TextView phoneText=(TextView)v.findViewById(R.id.phoneTxt);
TextView locationText=(TextView)v.findViewById(R.id.locationTxt);
TextView categoryText=(TextView)v.findViewById(R.id.categoryTxt);
Button callButton =(Button)v.findViewById(R.id.callbutton);
callButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + app.getPhonenumber()));

}
});


if(icon != null) {
InputStream in =null;
Bitmap bmp=null;
String sIcon = app.getIcon();

int responseCode = -1;
try{

URL url = new URL(sIcon);//"http://192.xx.xx.xx/mypath/img1.jpg
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.connect();
responseCode = con.getResponseCode();

if(responseCode == HttpURLConnection.HTTP_OK)
{
//download
in = con.getInputStream();
bmp = BitmapFactory.decodeStream(in);
in.close();
icon.setImageBitmap(bmp);
}

}
catch(Exception ex){
Log.e("Exception",ex.toString());
}
}


if(titleText != null) titleText.setText(app.getTitle());

if(phoneText != null) phoneText.setText(app.getPhonenumber());

if(locationText!=null)locationText.setText(app.getLocation());


if(categoryText!=null)categoryText.setText(app.getCategory());



}


return v;
}





}

Application.java
 
 
package com.sj.jsondemo;

public class Application {
private String title;
private long totalDl;
private int rating;
private String icon;
private String phonenumber;
private String location;
private String category;


public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

public String getLocation(){
return location;
}

public void setLocation(String location){
this.location=location;
}

public String getCategory(){
return category;
}

public void setCategory(String category){
this.category=category;
}




public String getPhonenumber(){
return phonenumber;
}

public void setPhonenumber(String phonenumber){
this.phonenumber=phonenumber;
}

public long getTotalDl() {
return totalDl;
}
public void setTotalDl(long totalDl) {
this.totalDl = totalDl;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
}

FetchDataTask.java
package com.sj.jsondemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;

public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;

public FetchDataTask(FetchDataListener listener) {
this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
if(params == null) return null;

// get url from params
String url = params[0];

try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);

// connect
HttpResponse response = client.execute(httpget);

// get response
HttpEntity entity = response.getEntity();

if(entity == null) {
msg = "No response from server";
return null;
}

// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
}
catch(IOException e){
msg = "No Network Connection";
}

return null;
}

@Override
protected void onPostExecute(String sJson) {
if(sJson == null) {
if(listener != null) listener.onFetchFailure(msg);
return;
}

try {
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
// create apps list
List<Application> apps = new ArrayList<Application>();

for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Application app = new Application();
app.setTitle(json.getString("app_title"));
app.setTotalDl(Long.parseLong(json.getString("total_dl")));
app.setRating(Integer.parseInt(json.getString("rating")));
app.setIcon(json.getString("icon"));
app.setPhonenumber(json.getString("phone_number"));
app.setLocation(json.getString("location"));
app.setCategory(json.getString("category"));


// add the app to apps list
apps.add(app);
}

//notify the activity that fetch data has been complete
if(listener != null) listener.onFetchComplete(apps);
} catch (JSONException e) {
msg = "Invalid response";
if(listener != null) listener.onFetchFailure(msg);
return;
}
}

/**
* This function will convert response stream into json string
* @param is respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;

try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
throw e;
}
finally {
try {
is.close();
}
catch (IOException e) {
throw e;
}
}

return sb.toString();
}
}

 MainActivity.java
 
package com.sj.jsondemo;

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;

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

private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");

String url = "http://192.168.186.1/apps.php";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}

@Override
public void onFetchFailure(String msg) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}

appcustomlist.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<ImageView
android:id="@+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:contentDescription="image"
/>
<TextView
android:id="@+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/appIcon"
android:layout_toRightOf="@id/appIcon"
android:layout_marginLeft="3dp"
android:text="Application Title"
android:textSize="22dp"/>

<TextView
android:id="@+id/phoneTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_below="@id/titleTxt"
/>

<TextView
android:id="@+id/locationtag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneTxt"
android:text="LOCATION:"
/>

<TextView
android:id="@+id/locationTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneTxt"
android:layout_toRightOf="@id/locationtag"

/>

<TextView
android:id="@+id/categorytag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneTxt"
android:layout_toRightOf="@id/locationTxt"
android:text="CATEGORY:"
android:paddingLeft="50dp"
/>

<TextView
android:id="@+id/categoryTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneTxt"
android:layout_toRightOf="@id/categorytag"

/>

<Button
android:id="@+id/callbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/titleTxt"
android:background="#f48d0d"
android:text="CALL"
android:focusable="true"

/>

</RelativeLayout> 
 
the list view is not showing image as well?