In Android, how does one set static data for ListView? androidlistview

Source: Internet
Author: User

In Android, how does one set static data for ListView? androidlistview

Sometimes we need to set fixed data for a listview. below is how to set static data

Layout file listview Homepage

<?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_width="fill_parent"        android:layout_height="fill_parent" >    </ListView></LinearLayout>

Then a layout file is the item of each listview, and listview_item.xml contains images and text

<?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="horizontal" >    <ImageView        android:id="@+id/listitem_iv"        android:layout_width="74dp"        android:layout_height="74dp"        android:src="@drawable/about_brand" />    <TextView        android:id="@+id/listitem_tv"        android:layout_width="match_parent"        android:layout_height="74dp"        android:text="TextView"        android:textAlignment="center"        android:textSize="55dp" /></LinearLayout>

Then the key is how to set static data:
ListViewUseAdapter. java

Import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. toast; import android. widget. adapterView. onItemClickListener; import android. widget. listView; public class ListViewUseAdapter extends Activity {private ListView listview; @ Ov Erride protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. listview_test); listview = (ListView) this. findViewById (R. id. listview); // set the image resource int [] imageId = new int [] {R. drawable. chat_tool_camera, R. drawable. chat_tool_location, R. drawable. chat_tool_paint, R. drawable. chat_tool_video, R. drawable. chat_tool_voice, R. drawable. about_brand }; // Set the title String [] title = new String [] {"camera", "positioning", "paint brush", "video", "sound ", "chat" }; List <Map <String, Object> listitem = new ArrayList <Map <String, Object> (); // convert the above resources into a list set for (int I = 0; I <title. length; I ++) {Map <String, Object> map = new HashMap <String, Object> (); map. put ("image", imageId [I]); map. put ("title", title [I]); listitem. add (map);} ListViewAdapter adapter = new ListViewAdapter (th Is, listitem); listview. setAdapter (adapter); listview. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (ListViewUseAdapter. this, "haha", Toast. LENGTH_SHORT). show ();}});}}

The required adapter is as follows:

import java.util.List;import java.util.Map;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {    private Context context;    private List<Map<String, Object>> listitem;    public ListViewAdapter(Context context, List<Map<String, Object>> listitem) {        this.context = context;        this.listitem = listitem;    }    @Override    public int getCount() {        return listitem.size();    }    @Override    public Object getItem(int position) {        return listitem.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);        }        ImageView imageView = (ImageView) convertView.findViewById(R.id.listitem_iv);        TextView textView = (TextView) convertView.findViewById(R.id.listitem_tv);        Map<String, Object> map = listitem.get(position);        imageView.setImageResource((Integer) map.get("image"));        textView.setText(map.get("title") + "");        return convertView;    }}

The effect is as follows:

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.