java - 我正在使用自定义列表有两个按钮,一个表单和第二个用于 whatsapp

标签 java android android-intent android-activity

我使用的自定义列表有两个按钮,一个用于 whatsaap,第二个用于 whatsaap。我的问题是当我单击第一个按钮然后调用电话时,它对我不起作用...请帮助我

自定义列表:

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Movie> movieItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<Movie> movieItems) {
        this.activity = activity;
        this.movieItems = movieItems;
    }

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

    @Override
    public Object getItem(int location) {
        return movieItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);
        Button year = (Button) convertView.findViewById(R.id.releaseYear);


        year.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity,  "ramu...", Toast.LENGTH_LONG).show();
                 Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:123456789"));
                    startActivity(callIntent);
            }
        });

        // getting movie data for the row
        Movie m = movieItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        System.out.println("thumbNail===============>"+thumbNail);

        // title
        title.setText(m.getTitle());

        // rating
        rating.setText("Rating: " + String.valueOf(m.getRating()));



        // release year
        year.setText(String.valueOf(m.getYear()));
        return convertView;
    }

    private void startActivity(Intent callIntent) {
        // TODO Auto-generated method stub

    }

}

`

最佳答案

你犯了一些不容忽视的错误。 Listview 在滚动时回收 View ,因此处理此检查非常重要。不这样做会导致错误位置的观点膨胀。使用 ViewHolder

解决方案:

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        /* This is where you initialize new rows, by:
         *  - Inflating the layout,
         *  - Instantiating the ViewHolder,
         *  - And defining any characteristics that are consistent for every row */
    } else {
        /* Fetch data already in the row layout, 
         *    primarily you only use this to get a copy of the ViewHolder */
    }

    /* Set the data that changes in each row, like `title` and `size`
     *    This is where you give rows there unique values. */

    return convertView;
}

您需要在检查之外实现 OnClickListener,例如:

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

    if (convertView == null) {
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_row, null);

        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView rating = (TextView) convertView.findViewById(R.id.rating);
        TextView genre = (TextView) convertView.findViewById(R.id.genre);
        Button year = (Button) convertView.findViewById(R.id.releaseYear);

        }else {

        }        

    year.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            Toast.makeText(activity,  "ramu...", Toast.LENGTH_LONG).show();
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123456789"));
            startActivity(callIntent);
        }
    });

        // getting movie data for the row
        Movie m = movieItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        System.out.println("thumbNail===============>"+thumbNail);

        // title
        title.setText(m.getTitle());

        // rating
        rating.setText("Rating: " + String.valueOf(m.getRating()));

        // release year
        year.setText(String.valueOf(m.getYear()));
        return convertView;
    }

在适配器构造函数中初始化 ImageLoader:

关于java - 我正在使用自定义列表有两个按钮,一个表单和第二个用于 whatsapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481260/

相关文章:

java - Gradle:从外部编译的类生成jar

java - DJ Native Swing javascript 命令问题

android - 在 Android 中禁用抽屉导航

android - 运行监听 android 日历事件的服务

java - 尝试在 Android 上扩展和使用 Activity 时出错

java - 如何从抽屉导航的 fragment 启动 Activity

java - CXF:可从 wsdl2java 克隆类吗?

java - 在没有 Thread.sleep() 的情况下延迟涉及 GUI 的 java 程序

android - Android 实现 OSGI 的最佳选择

java - 仅在重启时启动 Activity