[英]Attempting to update ListView Items
I am working on a ListView
that starts off containing 4 items: Event A, Event B, Event C, and Event D. 我正在处理一个包含四个项目的ListView
:事件A,事件B,事件C和事件D。
These four events are stored in an ArrayList<Event>
called mEvents
and on a button click I would like to filter out Event B from the ArrayList
and update the ListView
accordingly through the adapter using notifyDataSetChanged()
. 这四个事件存储在一个名为mEvents
的ArrayList<Event>
,在单击按钮时,我想从ArrayList
滤除事件B,并使用notifyDataSetChanged()
通过适配器相应地更新ListView
。
I am using a second ArrayList<Event>
named newEvents
to store the events that should be displayed (Event A/C/D) and then clear mEvents
and then set it equal to newEvents
. 我正在使用第二个名为newEvents
ArrayList<Event>
来存储应显示的事件(事件A / C / D),然后清除mEvents
,然后将其设置为等于newEvents
。
When the button is pressed, for some reason that last list item always gets deleted (Event D) regardless of which event I attempt to filter out. 当按下按钮时,由于某种原因,无论我尝试滤除哪个事件,最后一个列表项始终会被删除(事件D)。
Below is the relevant code, any help would be appreciated. 以下是相关代码,将不胜感激。
ListView code: ListView代码:
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();
}
} }
ListviewAdapter code: ListviewAdapter代码:
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;
}
} }
您正在使用“ NewEvent”而不是“ newEvent”
经过进一步的尝试,我了解到,将adapter.notifyDataSetChanged()
行替换为: mListView.setAdapter(adapter)
确实给了我预期的行为,除了listview将滚动到列表的顶部,因为我正在重置适配器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.