Activity + fragment + listview + adapter + bean routines in the same class, fragmentlistview

Source: Internet
Author: User

Activity + fragment + listview + adapter + bean routines in the same class, fragmentlistview

1. xml

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>

Fargment_main.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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"></ListView>
</LinearLayout>

List_item_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TEXT1"
android:textSize="16sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text1"
android:layout_below="@+id/text1"
android:text="TEXT2"
android:textSize="12sp" />

</RelativeLayout>

2. java

MainActivity. java

Public class MainActivity extends FragmentActivity {
MapView mMapView = null;
Private BaiduMap mBaiduMap;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Initialize the context information before using the SDK components and pass in the ApplicationContext
// Note that this method must be implemented before the setContentView method.
SetContentView (R. layout. activity_main );
If (savedInstanceState = null ){
Getsuppfrfragmentmanager (). beginTransaction (). add (R. id. container, new PlaceholderFragment (). commit ();
}

}

Public static class PlaceholderFragment extends Fragment implements AdapterView. OnItemClickListener {
Private ListView listView;
Private MainAdapter adapter;
@ Nullable
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
View view = inflater. inflate (R. layout. fragment_main, container, false );
ListView = (ListView) view. findViewById (R. id. list );
Adapter = new MainAdapter (getContext (), R. layout. list_item_main, getDateBean ());
ListView. setAdapter (adapter );
ListView. setOnItemClickListener (this );
Return view;
}
Public static List <DataBean> getDateBean (){
List <DataBean> list = new ArrayList <> ();
List. add (new DataBean ("basic map function", "Create a Baidu map and manage the map lifecycle "));
List. add (new DataBean ("layer display", "display common diagram, satellite diagram, traffic flow diagram, Baidu urban heat map "));
List. add (new DataBean ("map operation function", "describes basic map control methods "));
Return list;
}

@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id ){
Intent intent;
Switch (position ){
Case 0:
Intent = new Intent (getActivity (), BaseMap. class );
StartActivity (intent );
Break;
Case 1:
Intent = new Intent (getActivity (), Layers. class );
StartActivity (intent );
Break;
Case 2:
Intent = new Intent (getActivity (), Location. class );
StartActivity (intent );
Break;
Default:
Break;
}
}
}

Public static class MainAdapter extends ArrayAdapter <DataBean> {
Private int resourceID;
Public MainAdapter (Context context, int resource, List <DataBean> objects ){
Super (context, resource, objects );
ResourceID = resource;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ViewHolder holder;
If (convertView = null ){
ConvertView = View. inflate (getContext (), resourceID, null );
Holder = new ViewHolder ();
Holder. text1 = (TextView) convertView. findViewById (R. id. text1 );
Holder. text2 = (TextView) convertView. findViewById (R. id. text2 );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}
DataBean dataBean = getItem (position );
Holder. text1.setText (dataBean. text1 );
Holder. text2.setText (dataBean. text2 );
Return convertView;
}

Class ViewHolder {
TextView text1;
TextView text2;
}
}
Public static class DataBean {
String text1;
String text2;

Public DataBean (String text1, String text2 ){
This. text1 = text1;
This. text2 = text2;
}
}

}

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.