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: