无法单击listView中的TextView

时间:2017-07-13 14:54:50

标签: android

我的代码是列出员工的所有个人详细信息,一旦我们点击电话号码,就可以拨打该号码。 但是,如何解决ListView中无法解锁的TextView? 我需要点击两次才能拨打号码..寻求帮助谢谢

MainActivity

listView = (ListView) findViewById(R.id.list_view);
                    adapter = new AgentAdapter(this, arrayList);
                    listView.setAdapter(adapter);

                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            final SalesAgent agent = arrayList.get(position);

                            TextView phone_field = (TextView)view.findViewById(R.id.phone);
                            phone_field.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    intent.setData(Uri.parse(agent.getPhone()));
                                    startActivity(intent);
                                }
                            });
                        }
                    });

自定义ArrayAdapter

            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                    View listItemView = convertView;
                    if(listItemView == null){
                        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
                    }

                    //change background colours
                    listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.colorSkyBlue));

                    currentAgent = getItem(position);

                    TextView name_field = (TextView) listItemView.findViewById(R.id.name);
                    TextView website_field = (TextView) listItemView.findViewById(R.id.website);
                    final TextView phone_field = (TextView) listItemView.findViewById(R.id.phone);

                    name_field.setText(currentAgent.getName());
                    website_field.setText(currentAgent.getWebsite());
                    phone_field.setText(currentAgent.getPhone());

                    return listItemView;
                }

5 个答案:

答案 0 :(得分:0)

你应该在Adapter#getView中设置TextView#OnClickListener,而不是在ListView#OnItemClickListener中设置

像这样:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View listItemView = convertView;
                if(listItemView == null){
                    listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
                }

                //change background colours
                listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.colorSkyBlue));

                currentAgent = getItem(position);

                TextView name_field = (TextView) listItemView.findViewById(R.id.name);
                TextView website_field = (TextView) listItemView.findViewById(R.id.website);
                final TextView phone_field = (TextView) listItemView.findViewById(R.id.phone);

                name_field.setText(currentAgent.getName());
                website_field.setText(currentAgent.getWebsite());
                phone_field.setText(currentAgent.getPhone());
                phone_field.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                intent.setData(Uri.parse(agent.getPhone()));
                                startActivity(intent);
                            }
                        });
                return listItemView;
            }

修改

你应该在你的构造函数适配器中添加你的activity实例以使用startActivity方法,所以假设&#34; myActivity&#34;你的变数应该是这样的

myActivity.startActivity(intent);

答案 1 :(得分:0)

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

    final ViewHolder holder;

    if (convertView == null) {

        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout. list_item, null);

        holder.name_field = (TextView) convertView.findViewById(R.id.name);
        holder.website_field = (TextView) convertView.findViewById(R.id.website);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final CurrentAgent currentAgent = getItem(position);

    holder.name_field.setText(currentAgent.getName());

    holder.name_field.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // DO WHATEVER YOU WANT
        }
    });
return convertView;
}

public class ViewHolder {

       TextView name_field;
       TextView website_field;

}

答案 2 :(得分:0)

您必须通过以下方式执行简单的点击操作:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final SalesAgent agent = arrayList.get(position);
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(agent.getPhone()));
        startActivity(intent);
        }
     });
}

您无需再次使用click listener的{​​{1}}。可以TextView方法中的ListView参数检索position中商品的位置。

答案 3 :(得分:0)

作为建议,最好使用recyclerview

答案 4 :(得分:0)

你必须在你的适配器中添加一个监听器,我希望和sriva我的例子,问候。

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                    View listItemView = convertView;
                    if(listItemView == null){
                        listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
                    }

                    //change background colours
                    listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.colorSkyBlue));

                    currentAgent = getItem(position);

                    TextView name_field = (TextView) listItemView.findViewById(R.id.name);
                    TextView website_field = (TextView) listItemView.findViewById(R.id.website);
                    final TextView phone_field = (TextView) listItemView.findViewById(R.id.phone);
                    //here add listener texview
                    phone_field.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                          // your code call phone 
                      }
                    });

                    name_field.setText(currentAgent.getName());
                    website_field.setText(currentAgent.getWebsite());
                    phone_field.setText(currentAgent.getPhone());

                    return listItemView;
                }