您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。