我有一个带有两列列表视图的片段,其中第二个列有电话号码。我正在努力使这些号码可以点击,以便拨打电话(使用意图)。问题是当我运行应用程序时,点击文本没有任何反应。
(请注意,我已经设置了
<uses-permission android:name="android.permission.CALL_PHONE" />
清单中的。
@Override
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.contact_list);
populateList();
listviewAdapter adapter = new listviewAdapter(getActivity(), list);
lview.setAdapter(adapter);
final TextView tv= (TextView)LayoutInflater.from(getActivity()).inflate(R.layout.rows, null).findViewById(R.id.number);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phone_no=tv.getText().toString().trim();
callIntent.setData(Uri.parse("tel:"+phone_no));
startActivity(callIntent);
}});
return view;
}
rows.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/name"
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/number"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:clickable="true"
/>
</LinearLayout>
listviewAdapter.java
public class listviewAdapter extends BaseAdapter
{
public static final String FIRST_COLUMN = "Column 1";
public static final String SECOND_COLUMN = "Column 2";
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) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
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.name);
holder.txtSecond = (TextView) convertView.findViewById(R.id.number);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
holder.txtFirst.setText(map.get(FIRST_COLUMN));
holder.txtSecond.setText(map.get(SECOND_COLUMN));
return convertView;
}
}
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>