个性化的listview 方法有很多,我还是喜欢自定义adapter 因为看起来比较条理清晰。
首先你要建立你的listview
<ListView android:layout_width="fill_parent"
android:cacheColorHint="#00000000"
android:id="@layout/userone"
android:dividerHeight="0.5dip"
android:layout_height="wrap_content">
</ListView>
android:cacheColorHint="#00000000"
这句的意思是底色设为透明。如果你是黑色的listview的话这句就没必要了。这句是个性化其他颜色和风格的listview的时候
解决listview点击不松开下拉的时候部分变黑的问题。
android:id="@layout/userone"
这句的意思是定义id 名称,为什么这么定义了因为我在layout目录下有一个userone.xml作为listview的每一项。个性化由此开始。
那是不是你的userone.xml就是跟你的listview每项都一样了呢。没这么简单哇。要进行代码编写。如下。
public class ModelActivity extends Activity {
private ListView listview;
public void onCreate(Bundle savedInstanceState) {
listview = (ListView) findViewById(R.layout.userone);//你直接找listview的坐标也哦哦
listview.setAdapter(new MyAdapter(this));// 加入适配器-适配器就是简单的说就是copy的工具,就是当很多一样的布局可以进行复制。
}
}
//需要重写4个方法
public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;//这个是神马?反正自定义界面都要用它。
private View myView;
public MyAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
}
public int getCount() {
return 5;//这个就是适配器复制的数量。返回1就1项。
}
public Object getItem(int position) {
return null;//返回每项的对象
}
public long getItemId(int position) {
return 0;//返回每项的id
}
//getView为主要方法
public View getView(int position, View convertView, ViewGroup parent) {
myView = inflater.inflate(R.layout.userone, null);
return myView ;
}
}
//这样自定义listview就成功啦,当然这是个简单的啦。
上面的userone.xml进行自定义。
贴一下我的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="horizontal"
android:gravity="center_vertical" android:layout_height="75dip">
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="75dip"
android:layout_weight="1">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/TextView_model"
android:layout_weight="2" android:layout_width="fill_parent"
android:textColor="@color/ziti" android:gravity="center_vertical"
android:textSize="20sp" android:layout_height="35dip"
android:layout_marginLeft="10dip"></TextView>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView android:id="@+id/TextView01" android:text="@string/ps_jiawu"
android:layout_width="fill_parent" android:layout_height="30dip"
android:textColor="@color/ziti" android:layout_weight="2"
android:textSize="13sp" android:gravity="center_vertical"
android:layout_marginLeft="10dip"></TextView>
</TableRow>
</TableLayout>
<CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content"
android:layout_height="wrap_content"></CheckBox>
</LinearLayout>
下面你就修改getview方法啦
public View getView(int position, View convertView, ViewGroup parent) {
myView = inflater.inflate(R.layout.userone, null);
mCursor.moveToPosition(position);
final TextView textView = (TextView) myView
.findViewById(R.id.TextView_model);
final TextView textView01 = (TextView) myView
.findViewById(R.id.TextView01);
final CheckBox CheckBox01 = (CheckBox) myView
.findViewById(R.id.CheckBox01);
int a = mCursor.getInt(8);//哈哈这里就用到数据库拉。我吧checkbox的状态存在了数据库中 2为选中,1为不选中。当然你也可以不存数据库,用MyPreference来存取。也不错哦
textView.setText(mCursor.getString(1));//getString(1)就是textview的显示,从数据库读取的。
if (a == 2) {
CheckBox01.setChecked(true);
}
if (a == 1) {
CheckBox01.setChecked(false);
}
CheckBox01.setFocusable(false);
CheckBox01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for (int num = 0; num < mCursor.getCount(); num++) {
if (mCursor.moveToPosition(num)
&& mCursor.getInt(8) == 2) {
mDateBase.update(mCursor.getString(1), 1);
}
}
modelname = textView.getText().toString();
mDateBase.update(modelname, 2);
Toast.makeText(ModelActivity.this,
modelname + " " + getString(R.string.toast_defult),
Toast.LENGTH_SHORT).show();
//发送消息通知数据库更新
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
});
return myView;
}
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
listview.invalidateViews();
mCursor.deactivate();
break;
}
super.handleMessage(msg);
}
};
哦啦嘿嘿。数据库不明白恩。。下次写个数据库吧
原文链接: http://blog.csdn.net/peijiangping1989/article/details/6289108