Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试更新ListView项_Java_Android_Listview_Android Listview - Fatal编程技术网

Java 正在尝试更新ListView项

Java 正在尝试更新ListView项,java,android,listview,android-listview,Java,Android,Listview,Android Listview,我正在开发一个列表视图,它首先包含4项:事件a、事件B、事件C和事件D 这四个事件存储在名为mEvents的ArrayList中,单击一个按钮,我想从ArrayList中筛选出事件B,并通过适配器使用notifyDataSetChanged()相应地更新ListView 我正在使用另一个名为newEvents的ArrayList存储应该显示的事件(事件a/C/D),然后清除mEvents,然后将其设置为等于newEvents 当按下按钮时,由于某种原因,最后一个列表项总是被删除(事件D),无论我

我正在开发一个
列表视图
,它首先包含4项:事件a、事件B、事件C和事件D

这四个事件存储在名为
mEvents
ArrayList
中,单击一个按钮,我想从
ArrayList
中筛选出事件B,并通过适配器使用
notifyDataSetChanged()
相应地更新
ListView

我正在使用另一个名为
newEvents
ArrayList
存储应该显示的事件(事件a/C/D),然后清除
mEvents
,然后将其设置为等于
newEvents

当按下按钮时,由于某种原因,最后一个列表项总是被删除(事件D),无论我尝试过滤哪个事件

以下是相关代码,如有任何帮助,将不胜感激

列表视图代码:

public class ListActivity extends FragmentActivity {

private UserCustomFilters mUserCustomFilters = new UserCustomFilters();

// Our database hostname and the credentials
String url = ; //
String user = ;
String pass =;

SQLUtils sqlu ; //The SQLUtil object type that will be initialized later depending on the credentials given above.
ArrayList<Event> mEvents;       //The Array that will hold the Events that we will pass around(to Adapter,the List...)
List<Event> Even;

//Change Even to static if intent is used to refresh
ArrayList<Event> newEvent =new ArrayList<>();
ListviewAdapter adapter;

//Default Constructor for the class ListActivity
public ListActivity()
{
    sqlu = new SQLUtils(url, user, pass); //Creating Object type SQLUtils using credentials needed
    Even = sqlu.Events();  //Imports the List of Events from the Database.

    mEvents = new ArrayList<>();  //Assigning the new array where the events go.

    //Setting it into the new Array.
    for(int i=0;i<Even.size();i++)
    {
        mEvents.add(Even.get(i));
    }
}

//Injecting Buttons using ButterKnife Library
@InjectView(android.R.id.list) ListView mListView;

private void setUpFilters(){

    // Calling the FilterView class to set the layout for the filters

    FilterView filterView = new FilterView(this);
    mUserCustomFilters = filterView.init();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list2);

    setUpFilters();

    adapter=new ListviewAdapter(this, mEvents); 

    mListView.setAdapter(adapter);

    //Swipe stuff
    adapter.setMode(Attributes.Mode.Single);
}

..........

@OnClick(R.id.filterSaveButton)
public void ImplementingButton(View view)  {

    for(int i =0; i< mEvents.size();i++){

        if(mEvents.get(i).getEventName().equals("Event B")){

        }
        else{
            newEvent.add(mEvents.get(i));
        }
    }

    adapter.getData().clear();

    adapter.getData().addAll(newEvent);

    adapter.notifyDataSetChanged();

}
public class ListviewAdapter extends BaseSwipeAdapter {

private Activity mActivity;
private ArrayList<Event> mEvents;
public static int Clicks=0;

//the Constructor for the class.
public ListviewAdapter(Activity activity, ArrayList<Event> events) {
    mActivity = activity;
    mEvents = events;
}

@Override
public int getCount() {
    return mEvents.size();  //Returns length of the array of Events
}

@Override
public Object getItem(int position) {
    return mEvents.get(position);  //Returns the Item being accessed in the the array}
}

@Override
public long getItemId(int position) {
    return 0;   //Id of the Item being accessed in the view
}

public ArrayList<Event> getData() {

    return mEvents;
}

@Override
public int getSwipeLayoutResourceId(int i) {
    return R.id.swipe;
}

@Override
public View generateView(int position, ViewGroup parent) {

    //Inflates the view to be used
    View convertView = LayoutInflater.from(mActivity).inflate(R.layout.list_item, parent, false);

    ViewHolder holder = new ViewHolder(); //Making variable of class type ViewHolder def

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Assigning the Relative Layout that contains the detailed description.
            RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.DescriptionLayout);

            //Assigning the summary description stuff that will hide and reappear depending on the clicks.
            ImageView Bubble = (ImageView) v.findViewById(R.id.EventImageBubble);

            TextView EventName = (TextView) v.findViewById(R.id.TextEventName);
            TextView EventDate = (TextView) v.findViewById(R.id.TextEventDate);
            TextView EventPrice = (TextView) v.findViewById(R.id.TextEventPrice);
            TextView EventDistance = (TextView) v.findViewById(R.id.TextEventDistance);

            if (Clicks % 2 == 0) {
                //Popping the detailed description into view.
                layout.setVisibility(View.VISIBLE);

                //Hiding the summary Description from view to display the detailed description.
                Bubble.setVisibility(View.INVISIBLE);
                EventName.setVisibility(View.INVISIBLE);
                EventDate.setVisibility(View.INVISIBLE);
                EventPrice.setVisibility(View.INVISIBLE);
                EventDistance.setVisibility(View.INVISIBLE);
            } else {
                //Hiding the detailed description upon the 2nd click.
                layout.setVisibility(View.INVISIBLE);

                //Displaying the summary description back upon the 2nd click.
                Bubble.setVisibility(View.VISIBLE);
                EventName.setVisibility(View.VISIBLE);
                EventDate.setVisibility(View.VISIBLE);
                EventPrice.setVisibility(View.VISIBLE);
                EventDistance.setVisibility(View.VISIBLE);
            }

            Clicks++; //Adds to the number of times the user has tapped on an item.
        }
    });

    convertView.setTag(holder); //sets the tag

    //Summary Description of the events.
    holder.EventPicture= (ImageView) convertView.findViewById(R.id.ImageEventPicture);
    holder.EventIcon = (ImageView) convertView.findViewById(R.id.ImageEventIcon);
    holder.EventName = (TextView) convertView.findViewById(R.id.TextEventName);
    holder.EventDate = (TextView) convertView.findViewById(R.id.TextEventDate);
    holder.EventPrice= (TextView) convertView.findViewById(R.id.TextEventPrice);
    holder.EventDistance= (TextView) convertView.findViewById(R.id.TextEventDistance);

    //Initializing each item to the required type
    Event event = mEvents.get(position);

    //Detailed Description of the events.
    holder.EventDName=(TextView) convertView.findViewById(R.id.DesEventName);
    holder.EventDPrice= (TextView) convertView.findViewById(R.id.DesEventPrice);
    holder.EventLocName=(TextView) convertView.findViewById(R.id.DesEventLocName);
    holder.EventLocSt=(TextView) convertView.findViewById(R.id.DesEventLocStreet);
    holder.EventLocAdd=(TextView) convertView.findViewById(R.id.DesEventLocAddress);
    holder.EventStartDate=(TextView) convertView.findViewById(R.id.DesEventStartDate);
    holder.EventStartTime=(TextView) convertView.findViewById(R.id.DesEventStartTime);
    holder.EventEndDate=(TextView) convertView.findViewById(R.id.DesEventEndDate);
    holder.EventEndTime= (TextView) convertView.findViewById(R.id.DesEventEndTime);

    //Setting the text boxes to the information retrieved from the arrays of events

    //Setting the summary description
    holder.EventDistance.setText(event.getEventDistance()+"km");
    holder.EventName.setText(event.getEventName());
    holder.EventDate.setText(event.getEventDate());
    holder.EventPrice.setText("$"+event.getEventPrice());
    //holder.EventIcon.setImageResource(event.getEventIcon());
    //holder.EventPicture.setImageResource(event.getEventPicture());

    //Setting the detailed description.
    holder.EventDName.setText(event.getEventName());
    holder.EventDPrice.setText("$"+event.getEventPrice());
    holder.EventLocName.setText(event.getEventLocName());
    holder.EventLocSt.setText(event.getEventLocSt());
    holder.EventLocAdd.setText(event.getEventLocAdd());
    holder.EventStartDate.setText(event.getEventDate());
    holder.EventStartTime.setText(event.getEventStartTime());
    holder.EventEndDate.setText(event.getEventEndDate());
    holder.EventEndTime.setText(event.getEventEndTime());

    //Swipe methods being Implemented
    SwipeLayout swipeLayout = (SwipeLayout)convertView.findViewById(getSwipeLayoutResourceId(position));

    swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);

    swipeLayout.addDrag(SwipeLayout.DragEdge.Left, convertView.findViewById(R.id.bottom_wrapper));

    swipeLayout.addDrag(SwipeLayout.DragEdge.Right, convertView.findViewById(R.id.mLinear));

    swipeLayout.addSwipeListener(new SimpleSwipeListener() {

        @Override
        public void onOpen(SwipeLayout layout) {

        }
    });

    return convertView;
}

@Override
public void fillValues(int position, View convertView) {

}

private static class ViewHolder{
    //The values holding summary description of the event.
    ImageView EventPicture;
    ImageView EventIcon;
    TextView EventName;
    TextView EventDate;
    TextView EventPrice;
    TextView EventDistance;

    //The Values holding detailed description of the event.
    TextView EventDName;
    TextView EventDPrice;
    TextView EventLocName;
    TextView EventLocSt;
    TextView EventLocAdd;
    TextView EventStartDate;
    TextView EventStartTime;
    TextView EventEndDate;
    TextView EventEndTime;
}
公共类ListActivity扩展了FragmentActivity{
private UserCustomFilters mUserCustomFilters=new UserCustomFilters();
//我们的数据库主机名和凭据
字符串url=//
字符串user=;
字符串传递=;
SQLUtils sqlu;//稍后将根据上述凭据初始化的SQLUtil对象类型。
ArrayList mEvents;//将保存我们将传递的事件的数组(到适配器、列表…)
并列;
//如果使用意图刷新,则更改为静态
ArrayList newEvent=新的ArrayList();
ListviewAdapter适配器;
//类ListActivity的默认构造函数
公开上市活动()
{
sqlu=新SQLUtils(url、用户、过程);//使用所需凭据创建对象类型SQLUtils
偶数=sqlu.Events();//从数据库导入事件列表。
mEvents=new ArrayList();//指定事件所在的新数组。
//将其设置到新数组中。

对于(int i=0;i您正在使用“NewEvent”而不是“NewEvent”

在进一步尝试后,我了解到将
适配器.notifyDataSetChanged()
行替换为:
mListView.setAdapter(适配器)
确实给了我预期的行为,除了listview将滚动到列表顶部,因为我正在重置适配器。

对不起,这是我文章中的一个输入错误,在我的实现中它被更正了。您还可以为ListviewAdapter添加代码吗?任何特定的函数或整个类?不妨执行整个类,只是为了看看it为了实现滑动功能,使用了一个库,这导致适配器与通常的适配器不一样。当您分配新事件时?行
newEvent=new ArrayList()
?当您向该数组添加新值时,该数组是否可能不是空的?能否在
ImplementingButton()的开头为其分配一个新的ArrayList