可点击的textview(电话意向)

时间:2014-07-26 14:24:58

标签: java android xml textview

我有这个片段,它显示了两列列表(用适配器实现)。它代表一个电话号码列表,所以第二列只是一个数字列表:我想让每个号码都可以点击,点击后,电话必须开始。这是我的代码:

MyFragment.java

public class MyFragment extends Fragment {
    private ArrayList<HashMap<String, String>> list;
    public static final String column1 = "column1";
    public static final String column2 = "column2";
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


    }

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_my, container, false);
    ListView lview = (ListView)view.findViewById(R.id.listview);
    populateList();
    ListViewAdapter adapter = new ListViewAdapter(getActivity(), list);
    lview.setAdapter(adapter);

    return view;

}

public void onActivityCreated(Bundle savedInstanceState) {
    final TextView tv = (TextView)getActivity().findViewById(R.id.column2);
    tv.setOnClickListener(new View.OnClickListener() { **//this is the line getting NPE**
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_CALL);
            String phone_no=tv.getText().toString().trim();
            intent.setData(Uri.parse("tel:"+phone_no));
            startActivity(intent);

        }
    });
    super.onActivityCreated(savedInstanceState);

}



private void populateList() {

    list = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> temp1 = new HashMap<String, String>();
    temp1.put(column1,"Home");
    temp1.put(column2, "55501982");
    list.add(temp1);

}
}

fragment_my.xml

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

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

</LinearLayout>

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter
{

    public static final String colonna1 = "column1";
    public static final String colonna2 = "column2";

    public ArrayList<HashMap<String,String>> list;
    Activity activity;

    public ListViewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
        super();
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView txtFirst;
        TextView txtSecond;

    }


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

        ViewHolder holder;
        LayoutInflater inflater =  activity.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.rows, null);
            holder = new ViewHolder();
            holder.txtFirst = (TextView) convertView.findViewById(R.id.column1);
            holder.txtSecond = (TextView) convertView.findViewById(R.id.column2);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> map = list.get(position);
        holder.txtFirst.setText(map.get(column1));
        holder.txtSecond.setText(map.get(column2));


        return convertView;
    }


}

rows.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:paddingBottom="6dip"
    android:paddingTop="4dip" >

    <TextView
        android:id="@+id/column1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/column2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="@drawable/color_number"
        android:textSize="18sp"
        android:textStyle="bold"
        android:clickable="true" />

</LinearLayout>

我尝试了两次:首先,我在MyFragment.java中添加了这段代码:

final TextView tv = (TextView)getActivity().findViewById(R.id.column2);
tv.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        String phone_no=tv.getText().toString().trim();
        intent.setData(Uri.parse("tel:"+phone_no));
        startActivity(intent);

    }
});

但是我得到了NullPointerException:

07-26 16:58:50.746: E/AndroidRuntime(3871): FATAL EXCEPTION: main
07-26 16:58:50.746: E/AndroidRuntime(3871): java.lang.NullPointerException
07-26 16:58:50.746: E/AndroidRuntime(3871):     at com.example.myproject.fragments.MyFragment.onActivityCreated(MyFragment.java:44)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.os.Handler.handleCallback(Handler.java:730)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.os.Looper.loop(Looper.java:137)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at java.lang.reflect.Method.invoke(Method.java:525)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 16:58:50.746: E/AndroidRuntime(3871):     at dalvik.system.NativeStart.main(Native Method)

然后,我尝试添加这一行:

  

机器人:自动链接= “电话”

到rows.xml,但点击时没有任何反应。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

编辑:

ListView可以有多个项目,因此您需要为其分配多个TextView。但是,如果有多个具有相同ID的视图,您如何在活动中找到该视图? :-)这就是为什么在充气自定义项目布局时需要在适配器内部设置监听器的原因。像这样:

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

    ViewHolder holder;
    LayoutInflater inflater =  activity.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.rows, null);
        holder = new ViewHolder();
        holder.txtFirst = (TextView) convertView.findViewById(R.id.column1);
        holder.txtSecond = (TextView) convertView.findViewById(R.id.column2);
        convertView.setTag(holder);
        // Set the listener
        holder.txtSecond.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                String phone_no=tv.getText().toString().trim();
                intent.setData(Uri.parse("tel:"+phone_no));
                startActivity(intent);

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

    HashMap<String, String> map = list.get(position);
    holder.txtFirst.setText(map.get(column1));
    holder.txtSecond.setText(map.get(column2));


    return convertView;
}

请注意,我们仅在视图膨胀时设置侦听器。之后它才被重新使用。