我自定义一个ListView
使用ArrayAdapter
。在ListView
. 当我点击进入按钮时,它变为相反的不喜欢。
我单击该按钮,函数 onClick 回调,它不会立即显示返回到上一个活动并返回它显示的更改。
当点击按钮“Hay”(喜欢)时,我想要“Hay”更改“Bỏ Hay”(不喜欢)和蓝色。但它是立即!请帮我!谢谢!(对不起,我的英语不好)。
链接图片 http://dl.dropbox.com/u/14415363/item.png
我的代码在这里:它是 Java - CustonArrayAdapter。
public class CustomArrayAdapter extends ArrayAdapter<LoiChucItem>{
Holder holder;
public CustomArrayAdapter(Context context, int textViewResourceId, List<LoiChucItem> objects)
{
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// holder pattern
holder = null;
if (convertView == null)
{
holder = new Holder();
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.loichuc_item_listview, null);
holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
holder.btnLike = (Button) convertView.findViewById(R.id.btnLike);
holder.btnMessage = (Button) convertView.findViewById(R.id.btnMessage);
holder.btnShare = (Button) convertView.findViewById(R.id.btnShare);
convertView.setTag(holder);
}
else
{
holder = (Holder) convertView.getTag();
}
holder.tvContent.setText(getItem(position).getContent());
holder.btnLike = (Button) convertView.findViewById(R.id.btnLike);
// Không điền trực tiếp vì không nên để position final.
final int id = getItem(position).getId();
final int like = getItem(position).getLike();
if(like == 1){
holder.btnLike.setTextColor(Color.BLUE);
} else {
holder.btnLike.setTextColor(Color.DKGRAY);
}
holder.btnLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoiChucOpenHelper.clickLike(id, like);
if(like == 1){
holder.btnLike.setTextColor(Color.DKGRAY);
} else {
holder.btnLike.setTextColor(Color.BLUE);
}
CustomArrayAdapter.this.notifyDataSetChanged();
}
});
return convertView;
}
class Holder{
TextView tvContent;
Button btnLike;
Button btnMessage;
Button btnShare;
}
}
这是 xml 文件:item_listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Content"
android:textSize="18sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/btnLike"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/rating_good"
android:gravity="left|center_vertical"
android:text="Hay"
android:textSize="10sp"
/>
<Button
android:id="@+id/btnMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/social_send_now"
android:gravity="left|center_vertical"
android:text="Tin nhắn"
android:textSize="10sp"
/>
<Button
android:id="@+id/btnShare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/social_share"
android:gravity="left|center_vertical"
android:text="Chia sẻ"
android:textSize="10sp"
/>
</LinearLayout>
这是列表视图
<?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"
android:background="#a0a0a0" >
<ListView
android:background="#a0a0a0"
android:layout_margin="10dp"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"
android:scrollbars="none"
>
</ListView>