关于Android实现ListView一直混乱,今天终于清楚一点,赶紧记录下来
这可以看作是主程序
/**
*
*/
package com.JessicaDong.knowtheworld;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
/**
* @author JessicaDong
*
*/
public class Me66 extends ListActivity {
//public EditText edcity;
private List<Map<String, Object>> mData;
public List<Map<String, Object>> list;
//public MyAdapter adapter;
private Me6Adapter adapter;
private ListView friendListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.me66);
Log.i("Me66", "setview");
// edcity=(EditText)findViewById(R.id.citypost);
mData = getData();
//friendListView=(ListView)findViewById(R.id.list); 这样写会报“必须要定义一个id是R.id.list的什么东东,下行那样写就可以
friendListView=getListView();
adapter = new Me6Adapter(Me66.this,mData);
friendListView.setAdapter(adapter);
}
private List<Map<String, Object>> getData() {
list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.f1);
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f2);
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f3);
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f4);
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f5);
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f6);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f7);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f8);
map = new HashMap<String, Object>();
map.put("img", R.drawable.f9);
list.add(map);
return list;
}
protected void onListItemClick (ListView l, View v, int position, long id)
{
Log.i("itemclick", "click");
if(position==1)
{
Intent it=new Intent(Me66.this,Collec.class);
startActivity(it);
}
}
// public final class ViewHolder{
// public ImageView img;
// public Button viewBtndelete;
// public Button viewBtnadd;
// }
//
//
// public class MyAdapter extends BaseAdapter{
// public EditText edcity;
// private LayoutInflater mInflater;
//
// public MyAdapter(Context context){
// this.mInflater = LayoutInflater.from(context);
// edcity=(EditText)findViewById(R.id.citypost);
// }
// @Override
// public int getCount() {
// // TODO Auto-generated method stub
// return mData.size();
// }
//
// @Override
// public Object getItem(int arg0) {
// // TODO Auto-generated method stub
//
// return null;
// }
//
// @Override
// public long getItemId(int arg0) {
// // TODO Auto-generated method stub
// return 0;
// }
// public String string;
// @Override
// public View getView(final int position, View convertView, ViewGroup parent) {
//
// ViewHolder holder = null;
// if (convertView == null) {
//
//
// holder=new ViewHolder();
//
// convertView = mInflater.inflate(R.layout.vlist6, null);
// holder.img = (ImageView)convertView.findViewById(R.id.imgpost);
// holder.viewBtndelete = (Button)convertView.findViewById(R.id.view_btndelete);
// holder.viewBtnadd = (Button)convertView.findViewById(R.id.view_btnadd);
// convertView.setTag(holder);
// Log.i("final position", position+"");
// }else {
//
// holder = (ViewHolder)convertView.getTag();
// }
// holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
// holder.viewBtndelete.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View arg0) {
// // TODO Auto-generated method stub
// //点击delete
// list.remove(position);
// adapter.notifyDataSetChanged();
// }
// });
// holder.viewBtnadd.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View arg0) {
// // TODO Auto-generated method stub
// Map<String, Object> map = new HashMap<String, Object>();
//
// map.put("img", R.drawable.f1);
// list.add(map);
// adapter.notifyDataSetChanged();
// Log.i("Me66add", "调用了add");
// }
// });
// return convertView;
// }
}
Adapter
package com.JessicaDong.knowtheworld;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
public class Me6Adapter extends BaseAdapter{
private Context mContext;
private LayoutInflater mInflater;
private List<Map<String, Object>> list;
public Me6Adapter (Context context, List<Map<String, Object>> list)
{
this.mInflater = LayoutInflater.from(context);
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
//return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder=new ViewHolder();
convertView = mInflater.inflate(R.layout.vlist6, null);
holder.img = (ImageView)convertView.findViewById(R.id.imgpost);
holder.viewBtndelete = (Button)convertView.findViewById(R.id.view_btndelete);
holder.viewBtnadd = (Button)convertView.findViewById(R.id.view_btnadd);
convertView.setTag(holder);
Log.i("final position", position+"");
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)list.get(position).get("img"));
holder.viewBtndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//点击delete
list.remove(position);
notifyDataSetChanged();
}
});
holder.viewBtnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.f1);
list.add(map);
notifyDataSetChanged();
Log.i("Me66add", "调用了add");
}
});
return convertView;
//return null;
}
public final class ViewHolder{
public ImageView img;
public Button viewBtndelete;
public Button viewBtnadd;
}
}
主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="vertical" >
<Button android:id="@+id/friendadd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
vlist6.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:fillViewport="true"
android:layout_marginTop="0dp" >
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imgpost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/view_btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:layout_gravity="bottom|right" />
<Button
android:id="@+id/view_btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>