Android:带CustomAdapter的CustomListView,调用按钮不起作用

时间:2015-12-03 10:49:08

标签: android listview custom-adapter

在我的应用中,我有3个观看

  • ImagiveView
  • 的TextView
  • 按钮

ImagView相应地显示图像,TextView和Button显示相应的名称和编号。但问题是,当我点击按钮时,它不会调用按钮上显示的数字。虽然它确实打开了android调用者应用程序。

电话号码在string.xml文件中。

这里我提供了我的所有文件。请帮帮我

的strings.xml

<string-array name="names">
    <item>Abdul Malik</item>
    <item>Adeel ur Rehman</item>
    <item>Asad Majeeb</item>
    <item>Ata ul Salam</item>
    <item>Atta ul Qadir</item>
    <item>Bilal Scunder</item>
    <item>Chaudry Adnan Ahmed</item>
    <item>Chaudry Imran</item>
    <item>Ejaz Ahmed Saroya</item>
    <item>Hamid Joya</item>

</string-array>

<string-array name="telephones">
        <item>0000000000</item>
        <item>0486607636</item>
        <item>0485256515</item>
        <item>0485128196</item>
        <item>0465922084</item>
        <item>0487150005</item>
        <item>0488627993</item>
        <item>0484783792</item>
        <item>0484688663</item>
        <item>0497697050</item>
    </string-array>

MainActivity.xml

package com.example.android.listview_with_custom_layout;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {

    ListView listView;
    int [] movie_poster_resource={
            R.drawable.movie_1,
            R.drawable.movie_2,
            R.drawable.movie_3,
            R.drawable.movie_4,
            R.drawable.movie_5,
            R.drawable.movie_6,
            R.drawable.movie_7,
            R.drawable.movie_8,
            R.drawable.movie_9,
            R.drawable.movie_10,

    };

    String [] names ={};
    String [] telephones ={};

    MoviesAdapter moviesAdapter;

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

        listView=(ListView) findViewById(R.id.list_view);
        telephones =getResources().getStringArray(R.array.telephones);
        names =getResources().getStringArray(R.array.names);
        int i=0;

        moviesAdapter= new MoviesAdapter(getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(moviesAdapter);

        for(String titles: names){
            MovieDataProvider movieDataProvider= new MovieDataProvider(movie_poster_resource[i], titles, telephones[i]);

            moviesAdapter.add(movieDataProvider);
            i++;
        }
    }
}

MoviesDataAdapter

package com.example.android.listview_with_custom_layout;

/**
 * Created by temp on 2/11/2015.
 */
public class MovieDataProvider {
    private int movie_poster_resource;
    private String movie_title;
    private String telePhone;

    public MovieDataProvider(int movie_poster_resource, String movie_title, String telePhone) {
        this.setMovie_poster_resource(movie_poster_resource);
        this.setMovie_title(movie_title);
        this.telePhone = telePhone;
    }

    public int getMovie_poster_resource() {
        return movie_poster_resource;
    }

    public String getMovie_title() {
        return movie_title;
    }

    public String getTelePhone() {
        return telePhone;
    }

    public void setMovie_poster_resource(int movie_poster_resource) {
        this.movie_poster_resource = movie_poster_resource;
    }

    public void setMovie_title(String movie_title) {
        this.movie_title = movie_title;
    }

    public void setTelePhone(String telePhone) {
        this.telePhone = telePhone;
    }
}

MoviesAdapter

package com.example.android.listview_with_custom_layout;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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;

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

/**
 * Created by temp on 2/11/2015.
 */
public class MoviesAdapter extends ArrayAdapter {

    List list = new ArrayList();

    MovieDataProvider dataProvider;



    public MoviesAdapter(Context context, int resource) {
        super(context, resource);

    }

    static class DataHandler {
        ImageView Poster;
        TextView title;
        Button telePhone;
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

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

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

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


        DataHandler handler;
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row_layout, parent, false);
            handler = new DataHandler();
            handler.Poster = (ImageView) row.findViewById(R.id.movie_poster);
            handler.title = (TextView) row.findViewById(R.id.movie_title);
            handler.telePhone = (Button) row.findViewById(R.id.btn_call);

            row.setTag(handler);

        } else {
            handler = (DataHandler) row.getTag();
        }


        dataProvider = (MovieDataProvider) this.getItem(position);
        handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
        handler.title.setText(dataProvider.getMovie_title());
        handler.telePhone.setText(dataProvider.getTelePhone());
        handler.telePhone.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Intent to launch phone dialer


                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + dataProvider.getTelePhone()));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            }
        });

        return row;
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffd953"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>

row_layout.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="80dp"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingTop="10dp">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_alignParentLeft="true"
        android:src="@drawable/movie_1" />

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_toRightOf="@+id/movie_poster"
        android:gravity="center"
        android:text="This is movie name"
        android:textColor="#FFFFFF" />


    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/movie_title"
        android:gravity="center"
        android:text="call"
        android:textColor="#FFFF" />

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_below="@+id/movie_poster"
        android:background="#FFFF"></View>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您忘记在MainActivity中初始化电话字符串,执行相同的操作,因为它应该可以正常工作,并且不要忘记添加权限

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

最后你可以用

更新按钮点击代码
Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+dataProvider.getTelePhone()));
    startActivity(callIntent);

并使用此

更新AdapterClass
public class MoviesAdapter  extends ArrayAdapter {

List list = new ArrayList();
public MoviesAdapter(Context context, int resource) {
    super(context, resource);

}

static class DataHandler {
    ImageView Poster;
    TextView title;
    Button telePhone;
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

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

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    View row;
    row = convertView;
    final MovieDataProvider dataProvider = (MovieDataProvider) this.getItem(position);;

    DataHandler handler;
    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.row_layout, parent, false);
        handler = new DataHandler();
        handler.Poster = (ImageView) row.findViewById(R.id.movie_poster);
        handler.title = (TextView) row.findViewById(R.id.movie_title);
        handler.telePhone = (Button) row.findViewById(R.id.btn_call);
    } else {
        handler = (DataHandler) row.getTag();
    }

   handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
    handler.title.setText(dataProvider.getMovie_title());
    handler.telePhone.setText(dataProvider.getTelePhone());
    handler.telePhone.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + dataProvider.getTelePhone()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        }
    });
    row.setTag(handler);
    return row;
}
}