Android example source code non-third party implementation alphabetically sorted by city list

Source: Internet
Author: User

Values under Dimens.xml

<resources>

<!--Default screen margins, per the Android Design guidelines. -
<dimen name= "Activity_horizontal_margin" >16dp</dimen>
<dimen name= "Activity_vertical_margin" >16dp</dimen>

</resources>

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"
Tools:context= ". Mainactivity ">

<listview
Android:id= "@+id/listview1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
android:scrollbars= "None" >
</ListView>

<listview
Android:id= "@+id/listview2"
Android:layout_width= "15DP"
android:layout_height= "Match_parent"
Android:layout_alignparentright= "true"
Android:layout_alignparenttop= "true"
Android:cachecolorhint= "#00000000"
Android:divider= "@null"
android:scrollbars= "None" >
</ListView>

</RelativeLayout>

Letter_list.xml

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Wrap_content"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">

<textview
Android:id= "@+id/letterlisttextview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
Android:textcolor= "#3aa2cf"/>
</RelativeLayout>

Letter.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"
Tools:context= ". Mainactivity ">

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:orientation= "Vertical"
Android:background= "#eeeeee"
android:gravity= "Center_vertical" >

<textview
Android:id= "@+id/lettertextview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "5DP"
android:layout_margintop= "5DP"
Android:layout_marginbottom= "5DP"
android:text= "TextView"/>

</LinearLayout>

</RelativeLayout>

City.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"
Tools:context= ". Mainactivity ">

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:orientation= "Vertical"
Android:background= "#ffffff"
android:gravity= "Center_vertical" >

<textview
Android:id= "@+id/citytextview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "10DP"
android:layout_margintop= "10DP"
Android:layout_marginbottom= "10DP"
android:text= "TextView"/>

</LinearLayout>

</RelativeLayout>

Letter_city.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"
tools:context= ". Mainactivity

<linearlayout
android:id= "@+id/ll"
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_alignparentleft= "true"
Android:layout_alignparenttop = "true"
android:orientation= "vertical"
android:gravity= "center_vertical";

<textview
Android:id= "@+id/lettertextview"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content "
android:layout_marginleft=" 10DP "
android:text=" TextView "/>

</linearlayout>

</ Relativelayout>

 
   

Mainactivity.java

Package Com.example.zimusortlistview;

Import java.util.ArrayList;
Import java.util.List;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.BaseAdapter;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;

/**
* @author Peng Xiangming
* Load multiple layouts in the ListView
* Take the city list as an example to ensure that there are no identical elements in Cityletter and CityName, and that there are no identical elements in letter
*/
public class Mainactivity extends Activity implements onitemclicklistener{

String[] letter={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X "," Y "," Z "};
String[] cityletter={"C", "B", "H", "G", "W", "C", "s", "s", "C", "W", "H", "Z", "C", "D", "N", "L", "Q", "s", "s"
, "L", "N", "H", "X", "H", "S", "Z", "G", "N", "J", "T", "S", "X", "A"};
String[] cityname={"Changsha", "Beijing", "Hangzhou", "Guangzhou", "Wuhan", "Chongqing", "Shanghai", "Shenzhen", "Changchun", "Urumqi", "Harbin", "Zhengzhou", "Chengdu", "Dalian", "Nanchang", "Lanzhou", "Qiqihar", " Shantou "," Suzhou "
, "Lhasa", "Nanjing", "Hohhot", "Xiamen", "Hefei", "Shenyang", "Zhangjiajie", "Guizhou", "Ningxia", "Jinan", "Tianjin", "Shijiazhuang", "Xi ' an", "Macau"};
List<string> lettertocity=new arraylist<string> ();
int count;
ListView LV;

ListView Lv1;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

String str= "";
for (int i=0;i<letter.length;i++) {
Str=letter[i];
Boolean isaddletter=false;
for (int j=0;j<cityletter.length;j++) {
if (Str.equals (Cityletter[j])) {
if (!isaddletter) {
Lettertocity.add (str);
Isaddletter=true;
}
Lettertocity.add (Cityname[j]);
}
}
}

lv= (ListView) Findviewbyid (R.ID.LISTVIEW1);
Lv.setadapter (New Myadapter ());
Lv.setonitemclicklistener (this);

lv1= (ListView) Findviewbyid (R.ID.LISTVIEW2);
Lv1.setadapter (New MyAdapter1 ());
Lv1.setonitemclicklistener (this);
}

Class MyAdapter1 extends baseadapter{

@Override
public int GetCount () {
TODO auto-generated Method Stub
return letter.length;
}

@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return letter[position];
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
View View=layoutinflater.from (mainactivity.this). Inflate (r.layout.letter_list, NULL);
TextView tv= (TextView) View.findviewbyid (R.id.letterlisttextview);
Tv.settext (Letter[position]);
return view;
}

}

Class Myadapter extends baseadapter{

final static int type_1=1;
final static int type_2=2;

@Override
public int GetCount () {
TODO auto-generated Method Stub
return Lettertocity.size ();
}

@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return Lettertocity.get (position);
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

@Override
public int Getitemviewtype (int position) {
for (int i=0;i<letter.length;i++) {
if (Lettertocity.get (position). Equals (Letter[i])) {
return type_1;
}
}
return type_2;
}

@Override
public int Getviewtypecount () {
return 3;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
/**
* Even though Convertview caches some layouts, when refactoring, you don't know how to get Convertview back to the layout you need, you need to
* To let adapter know what layouts I currently have, I'll refactor the layout selection rules when I refactor item so that Convertview can return to the layout you need.
* You need to rewrite the following two functions
* @Override
* public int getitemviewtype (int position) {} This function gets the type of view created in GetView
* @Override
* public int Getviewtypecount () {} returns the number of view types created in GetView
* As for the detailed use of these two methods, you can see the API
*/
ViewHolder1 Vh1=null;
ViewHolder2 Vh2=null;
int Type=getitemviewtype (position);
if (convertview==null) {
Switch (type) {
Case Type_1:
Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter, NULL);
Vh1=new ViewHolder1 ();
vh1.tv= (TextView) Convertview.findviewbyid (R.id.lettertextview);
Convertview.settag (VH1);
Break
Case type_2:
Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.city, NULL);
Vh2=new ViewHolder2 ();
vh2.tv= (TextView) Convertview.findviewbyid (R.id.citytextview);
Convertview.settag (VH2);
Break
Default
Break
}
}else{
Switch (type) {
Case Type_1:
vh1= (ViewHolder1) Convertview.gettag ();
Break
Case type_2:
Vh2= (ViewHolder2) Convertview.gettag ();
Break
Default
Break
}
}
Switch (type) {
Case Type_1:
Vh1.tv.setText (Lettertocity.get (position));
Break
Case type_2:
Vh2.tv.setText (Lettertocity.get (position));
Break
Default
Break
}
return convertview;

This method is not advisable and the view refreshes when refreshed
Boolean isletter=false;
for (int i=0;i<letter.length;i++) {
if (Lettertocity.get (position). Equals (Letter[i])) {
Isletter=true;
Break
// }
// }
Viewholder Vh=null;
if (convertview==null) {
Vh=new Viewholder ();
if (isletter) {
Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter, NULL);
vh.tv= (TextView) Convertview.findviewbyid (R.id.lettertextview);
}else{
Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.city, NULL);
vh.tv= (TextView) Convertview.findviewbyid (R.id.citytextview);
// }
Convertview.settag (VH);
}else{
VH = (Viewholder) convertview.gettag ();
// }
Vh.tv.setText (Lettertocity.get (position));
return convertview;

List of cities, modifying layouts
Boolean isletter=false;
for (int i=0;i<letter.length;i++) {
if (Lettertocity.get (position). Equals (Letter[i])) {
Isletter=true;
Break
// }
// }
View view;
Viewholder VH;
if (convertview==null) {
View=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter_city, NULL);
Vh=new Viewholder ();
vh.tv= (TextView) View.findviewbyid (R.id.lettertextview);
Vh.ll= (LinearLayout) View.findviewbyid (R.ID.LL);
Convertview=view;
Convertview.settag (VH);
}else{
view= (View) Convertview;
Vh= (Viewholder) Convertview.gettag ();
// }
Vh.tv.setText (Lettertocity.get (position));
if (isletter) {
Vh.ll.setBackgroundColor (Color.ltgray);
Vh.tv.setPadding (5, 5, Vh.tv.getPaddingRight (), 5);
}else{
Vh.ll.setBackgroundColor (Color.White);
Vh.tv.setPadding (Ten, Vh.tv.getPaddingRight (), 10);
// }
return view;
}
}

@Override
public void Onitemclick (adapterview<?> parent, view view, int position,
Long id) {
Switch (Parent.getid ()) {
Case R.ID.LISTVIEW1:
Boolean isletter=false;
for (int i=0;i<letter.length;i++) {
if (Letter[i].equals (lettertocity.get (position))) {
Isletter=true;
Break
}
}
if (!isletter) {
Toast.maketext (this, lettertocity.get (position), Toast.length_short). Show ();
}
Break
Case R.ID.LISTVIEW2:
for (int i=0;i<lettertocity.size (); i++) {
if (Letter[position].equals (Lettertocity.get (i))) {
Lv.setselection (i);
Break
}
}
Break
Default
Break
}
}
}

Class viewholder{
TextView TV;
LinearLayout ll;
}
Class viewholder1{
TextView TV;
}
Class viewholder2{
TextView TV;
}

Android example source code non-third party implementation alphabetically sorted by city list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.