标签:android c class blog code java
<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/lv_users" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="40dp" android:layout_marginTop="10dp" android:text="TextView" /> <TextView android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv_name" android:layout_below="@+id/tv_name" android:layout_marginTop="10dp" android:text="TextView" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_name" android:layout_marginLeft="30dp" android:layout_toRightOf="@+id/tv_age" android:text="TextView" /> <TextView android:id="@+id/tv_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tv_name" android:paddingLeft="10dp" android:text="1" /> </RelativeLayout>
package com.example.android_sqlite; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.CheckedTextView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.android_sqlite.dao.UserDao; import com.example.android_sqlite.dao.impl.UserDaoImpls; import com.example.android_sqlite.database.DatabaseHelper; import com.example.android_sqlite.domain.Users; /** * BaseAdapter * @author zhaoyazhi * */ public class MainActivity extends Activity implements OnItemClickListener { private ListView lv_users; // 模型层 private List<Users> entites; private UserDao userDao; private DatabaseHelper dh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 设置视图层 lv_users = (ListView) findViewById(R.id.lv_users); // 创建数据库 dh = new DatabaseHelper(this); userDao = new UserDaoImpls(dh); // 获取显示数据 entites = userDao.findAll(); // 创建自定义的adapter对象 MyAdapyer adapter = new MyAdapyer(); // adapter作为控制层 lv_users.setAdapter(adapter); //注册点击事件 lv_users.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) { //获取点击我的数据 Users object = (Users) parent.getItemAtPosition(position); Toast.makeText(this, object.toString(), 0).show(); } /** * 继承了BaseAdapter 这个BaseAdapter实现了ListAdapter接口 * * @author zhaoyazhi * */ class MyAdapyer extends BaseAdapter { /** * 返回的总条目数量 */ @Override public int getCount() { return entites.size(); } /** * 返回当前条目对应的对象 */ @Override public Object getItem(int position) { return entites.get(position); } /** * */ @Override public long getItemId(int position) { return position; } /** * 返回每个条目 */ @Override public View getView(int position, View convertView, ViewGroup parent) { return definedItem(position); } private View definedItem(int position) { // 获取当前位置显示的对象 Users entity = entites.get(position); View v = View.inflate(MainActivitys.this, R.layout.item_phone, null); TextView tv_id = (TextView) v.findViewById(R.id.tv_id); TextView tv_name = (TextView) v.findViewById(R.id.tv_name); TextView tv_age = (TextView) v.findViewById(R.id.tv_age); TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone); tv_id.setText(""+entity.getUserId()); tv_name.setText("姓名:" + entity.getUserName()); tv_age.setText("年龄:" + entity.getUserAge()); tv_phone.setText("电话"+entity.getUserPhone()); return v; } /** * 单选框 * * @param position * @return */ private View singleCheck(int position) { // 获取当前位置显示的对象 Users entity = entites.get(position); View v = View.inflate(MainActivitys.this, android.R.layout.simple_list_item_single_choice, null); CheckedTextView tv = (CheckedTextView) v .findViewById(android.R.id.text1); tv.setText(entity.getUserName() + "---" + entity.getUserPhone()); tv.setHeight(40); return tv; } /** * 复选框 * * @param position * @return */ private View multipleCheck(int position) { // 获取当前位置显示的对象 Users entity = entites.get(position); View v = View.inflate(MainActivitys.this, android.R.layout.simple_list_item_multiple_choice, null); CheckedTextView tv = (CheckedTextView) v .findViewById(android.R.id.text1); tv.setText(entity.getUserName() + "---" + entity.getUserPhone()); tv.setHeight(40); return tv; } } }
赵雅智_ListView_BaseAdapter,布布扣,bubuko.com
标签:android c class blog code java
原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/29636669