Recently, a project requirement was discussed, and a phone-like icon was placed in the ListView item, and the number was changed to the dial-up interface by clicking on the icon. It is easy to realize, the principle is understandable, more practical, the project needs can be directly introduced.
I modeled a simple demo. The code is as follows:
1.listadapter.java:
PackageCom.example.listviewphone;ImportJava.util.List;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView; Public class listadapter extends baseadapter { PrivateList<test> tests;PrivateContext context; Layoutinflater Layoutinflater; Public ListAdapter(Context context,list<test> Tests) { This. tests=tests; This. Context=context; Layoutinflater=layoutinflater.from (context); }@Override Public int GetCount() {returnTests.size (); }@Override PublicObjectGetItem(intPosition) {returnTests.get (position); }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(Final intPosition, View Convertview, ViewGroup parent) {Viewholder viewholder=NULL;if(convertview==NULL) {viewholder=NewViewholder (); Convertview=layoutinflater.inflate (R.layout.item_list,NULL); viewholder.mtitlelistextview= (TextView) Convertview.findviewbyid (r.id.tv_title_list); viewholder.mphonetextview= (TextView) Convertview.findviewbyid (r.id.tv_phone_list); Convertview.settag (Viewholder); }Else{viewholder= (Viewholder) Convertview.gettag (); } viewHolder.mTitleLisTextView.setText (Tests.get (position). Gettitle_lost ()); ViewHolder.mPhoneTextView.setText (Tests.get (position). Getphone_lost ()); ViewHolder.mPhoneTextView.setOnClickListener (NewOnclicklistener () {@Override Public void OnClick(View v) {//Intent Intent = new Intent (Intent.action_call,uri.parse ("Tel:" +tests.get (position). Getphone_lost ())); Direct calls, more profiteering, cautious use! Intent Intent =NewIntent (Intent.action_dial,uri.parse ("Tel:"+tests.get (position). Getphone_lost ()));//Jump to user interface more moderate, recommended use! Context.startactivity (Intent); } });returnConvertview; } class Viewholder {PrivateTextView Mphonetextview;PrivateTextView Mtitlelistextview; Viewholder () {}}}
2.javabean-test.java:
Package com.example.listviewphone; Public classTest {PrivateString content_test;PrivateString phone_test;PrivateString title_test;PrivateString username; PublicStringgetcontent_test() {returnContent_test;} Public void setcontent_test(String content_test) { This. content_test = Content_test;} PublicStringgetphone_test() {returnPhone_test;} Public void setphone_test(String phone_test) { This. phone_test = Phone_test;} PublicStringgettitle_test() {returnTitle_test;} Public void settitle_test(String title_test) { This. title_test = Title_test;} PublicStringGetUserName() {returnUsername;} Public void Setusername(String username) { This. Username = Username;}}
3.mainactivity.java:
PackageCom.example.listviewphone;ImportJava.util.ArrayList;ImportJava.util.List;ImportJavax.security.auth.PrivateCredentialPermission;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.Window;ImportAndroid.widget.ListView; Public class mainactivity extends Activity { PrivateListView Mlistview;PrivateListAdapter adapter;PrivateList<test> tests;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); mlistview= (ListView) Findviewbyid (R.id.listview); Initdatas (); Adapter=NewListAdapter ( This, tests); Mlistview.setadapter (adapter); }Private void Initdatas() {tests=NewArraylist<test> (); for(inti =0; I < -; i++) {Test test =NewTest (); Test.settitle_test ("Phone"); Test.setphone_test ("123456789"+i); Tests.add (test); } }}
The following is a simple two layout file:
1.activity_main.xml:
<relativelayout 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 " > <ListViewandroid:id="@+id/listview"android:layout_width="Match_ Parent "android:layout_height=" wrap_content " /> </relativelayout>
2.item_list.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 =" wrap_content " android:padding =" 10DP " Android:background = "#ffffff" android:o Rientation =; <TextViewandroid:id= "@+id/tv_title_list"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Phone " android:singleline="true" > </TextView> <TextView Android:id="@+id/tv_phone_list" android:textsize="12SP" Android:layout_height="Wrap_content" Android:layout_width="Wrap_content" Android:background="@color/green" Android:text="138024249542" android:padding="4DP" Android:layout_marginleft="140DP" Android:drawableleft="@drawable/icon_photo"> </TextView></linearlayout>
Run the following example:
Summary: There are two kinds of jumps, 1-intent.action_call direct calls, more profiteering cautious use;
2-intent.action_dial jump to the dial interface, recommended to use
Have the need to introduce their own projects, like a friend to pay attention to it! Thanks for your support!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Combat Simple Tutorial-the 37th Shot (click button in the ListView to jump to the dial-up interface instance)