gpt4 book ai didi

android - 当我在滚动 Android Studio 后动态更新时,ListView 不会保留状态

转载 作者:行者123 更新时间:2023-11-29 01:26:45 25 4
gpt4 key购买 nike

我正在弹出窗口内创建一个 ListView。目前,我有我的 ListView 显示,部分将更新到现在,但如果单击索引以更改背景颜色,滚动以将其从页面上移开,然后滚动回该索引,它不会保留我的更改(这是一个文本更改)。此外,截至目前,另一个问题是“parent.getChildAt(position).setBackgroundColor(Color.BLUE);”如果我点击特定索引会导致错误。

这是包含我的弹出窗口和 ListView 的代码部分

public void bringUpEventsPopover() {
final LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflate the custom popup layout
final View inflatedView = layoutInflater.inflate(R.layout.popup_layout,null,false);

//find the listeview in the popup layout (we will need to construct this still)
final ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);

//get device size
/*Display display = getActivity().getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);*/
int width = view.getWidth();
int height = view.getHeight();
//fill the data to the list items
setSimpleList(listView);

//set the height depends on the device size
//popWindow= new PopupWindow(inflatedView, size.x, size.y, true);
popWindow= new PopupWindow(inflatedView, width, height-50, true);
//make it focusable to show the keyboard to enter in 'edittext'
popWindow.setFocusable(true);
popWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//show the popup at bottom of the screen and set some margin at bottom
popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

//make it outside touchable to dismiss the popup window when touched outside the popup area
popWindow.setOutsideTouchable(true);
popWindow.update();
final FrameLayout mainFrame = (FrameLayout) getActivity().findViewById(R.id.frametop);
mainFrame.getForeground().setAlpha(160);
popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mainFrame.getForeground().setAlpha(0);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// selected item
//Event item = (Event)listView.getItemAtPosition(position);
TextView tv = (TextView)view.findViewById(R.id.eventCreator);
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
}
});
}
void setSimpleList(ListView listView){

eventList = new ArrayList<>();


for (int index = 0; index < 6; index++) {
eventList.add(new Event("BT","We gon play sports and stuff","Title","Time is "+index+"pm today","3 Hours","15/30"));
}

for(Event item: eventList)
{
System.out.println(item.getTitle()+"\n"+item.getTime()+"\n"+item.getNumPeople()+"\n");
}
adapter = new ItemAdapter(getActivity(),R.layout.view_event,eventList);
listView.setAdapter(adapter);
}

这是我的 ListView 的 ItemAdapter 类

public class ItemAdapter extends ArrayAdapter<Event> {

// declaring our ArrayList of items
private ArrayList<Event> objects;

/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public ItemAdapter(Context context, int textViewResourceId, ArrayList<Event> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}

/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
v = inflater.inflate(R.layout.view_event, null);
v.setTag(position);
}
else
{
v.getTag();
}

/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current Item object.
*/
Event i = objects.get(position);

if (i != null) {

// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.

TextView creator = (TextView) v.findViewById(R.id.eventCreator);
TextView title = (TextView) v.findViewById(R.id.eventTitle);
TextView description = (TextView) v.findViewById(R.id.eventDescription);
TextView time = (TextView) v.findViewById(R.id.eventTime);
TextView duration = (TextView) v.findViewById(R.id.eventDuration);
TextView numPeople = (TextView) v.findViewById(R.id.eventAttendance);

// check to see if each individual textview is null.
// if not, assign some text!
if(creator!=null)
{
creator.setText("Creator: "+i.getCreator());
}
if (title != null){
title.setText("Title: "+i.getTitle());
}
if(description!=null)
{
description.setText("Description: "+i.getDescription());
}
if( time != null) {
time.setText("Time: "+i.getTime());
}
if(duration!=null)
{
duration.setText("Duration: "+i.getDuration());
}
if (numPeople != null){
numPeople.setText(i.getNumPeople());
}
}

// the view must be returned to our activity
return v;

}


}

这是每个列表项的布局

<?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"
android:orientation="vertical">
<TextView
android:id="@+id/eventCreator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:text="Creator: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="5dp" />
<TextView
android:id="@+id/eventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventCreator"
android:text="Title: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="5dp" />

<TextView
android:id="@+id/eventDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventTitle"
android:text="Description: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="5dp" />

<TextView
android:id="@+id/eventTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventDescription"
android:text="Time: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="5dp" />

<TextView
android:id="@+id/eventDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventTime"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Duration: "
android:layout_margin="5dp" />

<TextView
android:id="@+id/eventAttendance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventDuration"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="15/30 Going"
android:layout_margin="5dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/eventAttendance"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_margin="20dp"
android:text="I'm Going!"
android:layout_centerHorizontal="true"
android:focusable="false"
android:clickable="false"/>

</RelativeLayout>

最佳答案

两个想法。 1)也许考虑为您的事件/项目提供一个容器,以便您可以为它们提供动态 ID,然后使用它们来设置背景颜色。当它在屏幕上滚动/关闭时,您可能会更幸运地保存状态。 2) 我不是 100% 确定这一点,所以不要把它当作福音,但我认为当您将 ListView 滚动到 View 之外时,适配器可能会弄乱子结构中的索引变量。还有其他方法可以访问该 ListView 的子节点吗?抱歉,我没有一个很好的答案,只是一个让你追逐的想法。祝你好运!

  • 乔恩

关于android - 当我在滚动 Android Studio 后动态更新时,ListView 不会保留状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com