Set static data for ListView in Android

Source: Internet
Author: User

Set static data for ListView in Android

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

Layout file listview Homepage


   

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


  
        
    
   
  

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; @ Override 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
  
   
> Listitem = new ArrayList
   
    
> (); // Convert the above resources to the list set for (int I = 0; I <title. length; I ++) {Map
    
     
Map = new HashMap
     
      
(); Map. put (image, imageId [I]); map. put (title, title [I]); listitem. add (map);} ListViewAdapter adapter = new ListViewAdapter (this, 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
  
   > listitem;    public ListViewAdapter(Context context, List
   
    > 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
    
      map = listitem.get(position);        imageView.setImageResource((Integer) map.get(image));        textView.setText(map.get(title) + );        return convertView;    }}
    
   
  

The effect is as follows:

Related Article

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.