android ListView控件操作绑定数据、单击事件
1、主界面main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="姓名"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="备注"
/>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
/>
</LinearLayout>
2、ListView中项界面设计
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/testname"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/testmemo"
/>
</LinearLayout>
3、使用SimpleCursorAdapter绑定数据
* 使用SimpleCursorAdapter适配器绑定数据
*/
private void bindData()
{
Cursor cursor=tService.findCursorSplitPage(0, 20,"");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item, cursor,
new String[]{"name","memo"},new int[]{R.id.testname,R.id.testmemo});
listView.setAdapter(adapter);
}
4、使用SimpleAdapter绑定数据
/*
* 使用SimpleAdapter适配器绑定数据
*/
private void bindData1()
{
List<testTable> list=tService.findSplitPage(0, 20, "");
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(testTable t: list)
{
HashMap<String,Object> h=new HashMap<String, Object>();
h.put("name", t.getName());
h.put("memo", t.getMemo());
h.put("id", t.getId());
data.add(h);
}
SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.item,
new String[]{"name","memo"},new int[]{R.id.testname,R.id.testmemo});
listView.setAdapter(adapter);
}
5、使用自定义适配器绑定数据
5.1、自定义适配器
import java.util.List;
import org.jgw.entity.testTable;
import org.jgw.hello.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class testTableAdapter extends BaseAdapter {
private List<testTable> list;
private Context context;
private int layout;
private LayoutInflater inflater;
public testTableAdapter(List<testTable> list, Context context,int layout)
{
this.list = list;
this.context = context;
this.layout = layout;
inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
/*
* 获取总行数
* @see android.widget.Adapter#getCount()
*/
public int getCount() {
return list.size();
}
/*
* 根据索引获取数据
* @see android.widget.Adapter#getItem(int)
*/
public Object getItem(int position) {
return list.get(position);
}
/*
* item的id,很少使用到
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewCache cache;
if(convertView==null)
{
convertView = inflater.inflate(layout, null);
cache=new ViewCache();
cache.testname=(TextView)convertView.findViewById(R.id.testname);
cache.testmemo=(TextView)convertView.findViewById(R.id.testmemo);
convertView.setTag(cache);
}
else
{
cache=(ViewCache)convertView.getTag();
}
testTable t=list.get(position);
cache.testname.setText(t.getName());
cache.testmemo.setText(t.getMemo());
return convertView;
}
public final class ViewCache
{
public TextView testname;
public TextView testmemo;
}
}
5.2、应用自定义适配器绑定ListView
* 使用自定义适配器绑定数据
private void bindData2()
{
List<testTable> list=tService.findSplitPage(0, 20, "");
testTableAdapter adapter=new testTableAdapter(list, getApplicationContext(), R.layout.item);
listView.setAdapter(adapter);
}
6、给ListView的item添加OnClick事件
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.jgw.adapter.testTableAdapter;
import org.jgw.entity.testTable;
import org.jgw.service.testTableService;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.*;
public class HellowordActivity extends Activity {
/** Called when the activity is first created. */
ListView listView;
testTableService tService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tService=new testTableService(getApplicationContext());
listView=(ListView)this.findViewById(R.id.listView);
//bindData();
//bindData1();
bindData2();
listView.setOnItemClickListener(new ItemClick());
}
/*
* item上的OnClick事件
*/
public final class ItemClick implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
ListView lview=(ListView)parent;
testTable t=(testTable)lview.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), t.getId()+"", 1).show();
}
}