我有一個ListView
顯示列表項目(杜)。當您在列表項目上輸入click
時,將打開另一個Activity
。部分清單項目layout
是灰星,ImageView
。當你點擊這個ImageView
時,我不想打開另一個Activity
,但我想要將星星的顏色更改爲綠色(=將項目標記爲收藏夾)或返回(=標記爲不喜歡)。我設法用OnClickListener
做到這一點,在Click
上加載另一個ImageView
,並刷新adapter
。但是對於ImageView
更改,點擊後我需要離開Activity
並再次輸入。它不會立即刷新。爲什麼,我該如何改變?我已經嘗試了很多不同的版本,到目前爲止沒有任何工作。我的ListViewAdapter
延伸到BaseAdapter
。謝謝!如何更改ImageView,它是列表項的一部分?
public class ListViewAdapterKeysAToZ extends BaseAdapter {
private ArrayList<KeyTagIntern> keyTags;
private ObservableArrayList<KeyTagIntern> list;
private Context context;
TextView name;
TextView place;
ImageView star, favoriteStar;
public ListViewAdapterKeysAToZ(Context context, ObservableArrayList<KeyTagIntern> list)
{
this.context = context;
this.list = list;
keyTags = new ArrayList<>();
for (KeyTagIntern keytag : list) {
keyTags.add(keytag);
}
//(....)
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
KeyTagIntern key = (KeyTagIntern) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_keys, parent, false);
}
name = (TextView) convertView.findViewById(R.id.text_keylist_item);
name.setText(key.getName());
place = (TextView) convertView.findViewById(R.id.text_keylist_item_place);
place.setText(key.getPlace());
star = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item);
favoriteStar = (ImageView) convertView.findViewById(R.id.right_icon_keylist_item_favorite);
if (key.isFavorite())
{
star.setVisibility(View.INVISIBLE);
favoriteStar.setVisibility(View.VISIBLE)
favoriteStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This makes key.isFavourite() = false for the next time
Paper.book().delete(FAVORIT + String.valueOf(key.getKeyTagID()));
//Since notifyDataSetChanged() didn't work for me, I tried this - but no change
int index = list.indexOf(key);
list.remove(index);
list.add(index, key);
keyTags = new ArrayList<>();
for (KeyTagIntern keytag : list) {
keyTags.add(keytag);
}
notifyDataSetChanged();
}
});
}
// Then do the opposite for if (!key.isFavourite())
UND票數DAS的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/list_item_keys"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/MiddleDarkGrey">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btn_list_item_keys"
android:layout_width="match_parent"
android:layout_height="@dimen/height_list_item"
android:layout_marginBottom="3dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="3dp"
android:background="@drawable/white_list_item"
android:paddingLeft="13dp"
android:paddingRight="10dp">
<ImageView
android:id="@+id/icon_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:visibility="visible"
app:srcCompat="@drawable/ic_key" />
<ImageView
android:id="@+id/icon_reserved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/chb_add_key"
android:layout_centerVertical="true"
android:visibility="invisible"
app:srcCompat="@drawable/ic_reservate_orange" />
<ImageView
android:id="@+id/icon_taken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/chb_add_key"
android:layout_centerVertical="true"
android:visibility="invisible"
app:srcCompat="@drawable/ic_taken_red" />
<TextView
android:id="@+id/text_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/icon_keylist_item"
android:layout_toRightOf="@+id/icon_keylist_item"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:text="Text"
android:textColor="@color/DarkGrey"
android:textSize="@dimen/text_list_item" />
<TextView
android:id="@+id/text_keylist_item_place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_keylist_item"
android:layout_alignLeft="@+id/text_keylist_item"
android:layout_marginLeft="2dp"
android:layout_marginBottom="5dp"
android:text="Where is the key?"
android:textColor="@color/DarkGrey"
android:textSize="@dimen/text_list_item_sub" />
<ImageView
android:id="@+id/right_icon_keylist_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/ic_fav_green" />
<ImageView
android:id="@+id/right_icon_keylist_item_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_fav_chosen"
android:visibility="invisible"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
你能後,你實際設置視圖中的代碼? – Mars
它看起來並不像你實際上在getView函數中設置你的視圖。我們看不到什麼明星或最喜歡的明星引用 – Mars
哦,對不起 - 我離開了它,因爲我不認爲在那部分代碼中可能有任何問題。現在我添加了該部分。 – Mira