您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
package com.example.testlist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.app.Activity; import android.content.Context; import android.graphics.Color; public class MainActivity extends Activity implements OnItemClickListener{ private MyAdapter adapter; private List<Map<String, Object>> list; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1);// 取得控件 listView.setOnItemClickListener(this); list = getData();// 获取数据 adapter = new MyAdapter(this); listView.setAdapter(adapter); } private List<Map<String, Object>> getData(){ list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; for(int i=0; i<2; i++){ map = new HashMap<String, Object>(); map.put("title", "title" + i); map.put("img", R.drawable.ic_launcher); list.add(map); } return list; } public void add(View v){ Map<String, Object> map; map = new HashMap<String, Object>(); map.put("title", "title" + 100); map.put("img", R.drawable.ic_launcher); list.add(map); adapter.notifyDataSetChanged(); } // ViewHolder静态类 static class ViewHolder { public ImageView img; public TextView title; } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater = null; private MyAdapter(Context context) { // 根据context上下文加载布局 this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // 在此适配器中所代表的数据集中的条目数 return list.size(); } @Override public Object getItem(int position) { // 获取数据集中与指定索引对应的数据项 return position; } @Override public long getItemId(int position) { // 获取在列表中与指定索引对应的行id return position; } // 获取一个在数据集中指定索引的视图来显示数据 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; // 如果缓存convertView为空,则需要创建View if (convertView == null) { holder = new ViewHolder(); // 根据自定义的Item布局加载布局 convertView = mInflater.inflate(R.layout.item, null); holder.title = (TextView) convertView.findViewById(R.id.textView1); holder.img = (ImageView) convertView.findViewById(R.id.p_w_picpathView1); // 将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.title.setText((String) list.get(position).get("title")); holder.img.setBackgroundResource((Integer) list.get(position).get("img")); return convertView; } } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "" + arg2, Toast.LENGTH_SHORT).show(); } } //item.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" android:orientation="horizontal" > <ImageView android:id="@+id/p_w_picpathView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> //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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="300dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" > </ListView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/listView1" android:layout_below="@+id/listView1" android:layout_marginRight="56dp" android:layout_marginTop="16dp" android:onClick="add" android:text="Button" /> </RelativeLayout>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。