ListView赢得';在滚动Android Studio后动态更新时无法保留状态

ListView赢得';在滚动Android Studio后动态更新时无法保留状态,android,listview,android-studio,android-listview,Android,Listview,Android Studio,Android Listview,我正在弹出窗口中创建一个列表视图。目前,我的列表视图已显示,部件将从现在起更新,但如果单击某个索引以更改背景颜色,滚动将其从页面中删除,然后滚动回该索引,则不会保留我的更改(这是文本更改)。另外,到目前为止,另一个问题是,如果单击特定索引,“parent.getChildAt(position).setBackgroundColor(Color.BLUE);”会导致错误 下面是包含我的popupwindow和列表视图的代码部分 public void bringUpEventsPopover()

我正在弹出窗口中创建一个列表视图。目前,我的列表视图已显示,部件将从现在起更新,但如果单击某个索引以更改背景颜色,滚动将其从页面中删除,然后滚动回该索引,则不会保留我的更改(这是文本更改)。另外,到目前为止,另一个问题是,如果单击特定索引,“parent.getChildAt(position).setBackgroundColor(Color.BLUE);”会导致错误

下面是包含我的popupwindow和列表视图的代码部分

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);
    }
public void bringUpEventsPopover(){
final LayoutInflater LayoutInflater=(LayoutInflater)getActivity().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
//对自定义弹出窗口布局进行充气
最终视图充气视图=LayoutFlater.充气(R.layout.popup\u布局,null,false);
//在弹出式布局中找到ListView(我们仍需要构建此视图)
最终ListView ListView=(ListView)inflatedView.findViewById(R.id.commentsListView);
//获取设备大小
/*显示显示=getActivity().getWindowManager().getDefaultDisplay();
最终点大小=新点();
display.getSize(size)*/
int width=view.getWidth();
int height=view.getHeight();
//将数据填入列表项
设置简单列表(listView);
//根据设备大小设置高度
//popWindow=新的PopupWindow(充气视图,尺寸.x,尺寸.y,真);
popWindow=新的PopupWindow(充气视图,宽度,高度-50,真);
//使其可聚焦显示键盘以输入“edittext”
popWindow.setFocusable(真);
popWindow.setBackgroundDrawable(新的彩色Drawable(Color.TRANSPARENT));
//在屏幕底部显示弹出窗口,并在底部设置一些边距
popWindow.showAtLocation(视图,重心,0,0);
//当在弹出区域外触摸时,使其可在外部触摸,以关闭弹出窗口
popWindow.setOutsideTouchable(真);
更新();
最终FrameLayout mainFrame=(FrameLayout)getActivity().findViewById(R.id.frametop);
mainFrame.get前台().setAlpha(160);
popWindow.setOnDismissListener(新的PopupWindow.OnDismissListener(){
@凌驾
公共空间{
mainFrame.get前台().setAlpha(0);
}
});
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id)
{
//选定项目
//事件项=(事件)listView.getItemAtPosition(位置);
TextView tv=(TextView)view.findViewById(R.id.eventCreator);
父.getChildAt(位置).setBackgroundColor(颜色.蓝色);
}
});
}
void setSimpleList(ListView ListView){
eventList=新的ArrayList();
对于(int-index=0;index<6;index++){
事件列表。添加(新事件(“BT”,“我们可以玩体育和其他东西”,“标题”,“时间是“+索引+”今天下午”,“3小时”,“15/30”));
}
用于(事件项:事件列表)
{
System.out.println(item.getTitle()+“\n”+item.getTime()+“\n”+item.getNumPeople()+”\n”);
}
adapter=newitemAdapter(getActivity(),R.layout.view\u事件,eventList);
setAdapter(适配器);
}
这是我的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;

    }


}
公共类ItemAdapter扩展了ArrayAdapter{
//声明项目的ArrayList
私有ArrayList对象;
/*这里我们必须重写ArrayAdapter的构造函数
*我们现在唯一关心的变量是ArrayList对象,
*因为它是我们要显示的对象列表。
*/
公共项适配器(上下文上下文、int textViewResourceId、ArrayList对象){
超级(上下文、textViewResourceId、对象);
this.objects=对象;
}
/*
*我们在这里重写getView方法-这就是定义每个
*列表项将显示。
*/
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u充气器\u SERVICE);
//将要转换的视图指定为局部变量
视图v=转换视图;
//首先检查视图是否为空。如果是,我们必须将其充气。
//充气基本上意味着渲染或显示视图。
如果(v==null){
v=充气机充气(R.layout.view_事件,空);
v、 设置标签(位置);
}
其他的
{
v、 getTag();
}
/*
*回想一下,变量位置作为参数发送到此方法。
*变量只是引用当前对象在列表中的位置。(ArrayAdapter
*遍历我们发送的列表)
*
*因此,我引用了当前的Item对象。
*/
事件i=objects.get(位置);
如果(i!=null){
//这是获取对TextView的引用的方式。
//这些文本视图是在我们定义的XML文件中创建的。
TextView创建者=(TextView)v.findViewById(R.id.eventCreator);
TextView title=(TextView)v.findViewById(R.id.eventTitle);
TextView description=(TextView)v.findViewById(R.id.eventDescription);
TextView时间=(TextView)v.findViewById(R.id.eventTime);
TextView持续时间=(TextView)v.findViewById(R.id.eventDuration);
TextView numPeople=(TextView)v.findViewById(R.id.eventAttention);
//检查每个文本视图是否为空。
//如果没有,请指定一些文本!
<?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>
// 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) {
            /* This is where you initialize new rows, by:
         *  - Inflating the layout,
         *  - Instantiating the ViewHolder,
         *  - And defining any characteristics that are consistent for every row */
            v = inflater.inflate(R.layout.view_event, null);
            v.setTag(position);
        }
        else
        {
            /* Fetch data already in the row layout,
         *    primarily you only use this to get a copy of the ViewHolder */
            v.getTag();
        }
     /* Set the data that changes in each row, like `title` and `size`
     *    This is where you give rows there unique values. */
        if(objects.get(position).isSelected())
        {
            v.setBackgroundColor(Color.CYAN);
        }
        else
        {
            v.setBackgroundColor(Color.WHITE);
        }
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id)
            {

                // selected item
                adapter.objects.get(position).setSelected(true);
                adapter.notifyDataSetChanged();
            }
        });