我自定义a ListView
使用ArrayAdapter
。我在ListView
的每个项目中都有一个按钮(我的语言中为“干草”)。当我点击进入按钮时,它变成了相反的不喜欢。在自定义ArrayAdapter中的onClick时不显示新文本
我点击那个按钮,函数onClick回调,它不立即显示变化,返回到上一个活动,并返回它显示。
我想要点击按钮'干草'(如赞),'干草'改变'B'干草'(如不喜欢)和蓝色。但它是立即!请帮帮我!谢谢! (对不起,我的英语不好)。
Link image 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>
这里的ListView
<?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>
但是文本'干草'变成蓝色文本其他。 –
所以当“Hay”被按下时你想要“Bo Hay”作为绿色。 –
是的。 'Hay'变成蓝色'Bo Hay'。 –