에 추가 의견을 추가 한? 데이터 집합 (예 : ArrayList)을 생성자의 ListAdapter에 전달합니다. 그런 다음 데이터 세트를 조작하면 (예 : dataset.remove(Object object)
) ListAdapter에서 .notifyDatasetChanged()
으로 전화하십시오. 그러면 어댑터가 속한보기를 갱신합니다.
업데이트
'까지 그 나사 홀더의 위치'에 의해 당신이 무엇을 의미하는지 정말 모르겠어요
. ViewHolder는 기본적으로 getView()
-method에서 조작하려는 View
에 대한 참조를 보유하기위한 것입니다. 데이터 세트는 ViewHolder에서 분리되어 있으므로 데이터 세트에서 항목을 제거해도 레이아웃에 영향을 미치지 않아야합니다. 목록보기에서 항목을 삭제하는 경우는 제외합니다.
다음은 작동해야하는 예입니다. 나는 가능한 한 많이 설명하려고 노력했다. 대부분을 알고 있을지 모르지만 나중에 참조 할 때/Google 직원을 위해 몇 가지 추가 정보를 제공합니다.
참고 : 완벽하지는 않지만 잘 처리되어야합니다.
MyAdapter myAdapter = new MyAdapter(this, R.layout.listitem, cartItems);
ListView listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(myAdapter);
cartItems.remove(object);
myAdapter.notifyDatasetChanged();
이리스트 뷰에서 항목을 제거하고 예,이 위치의 인덱스를 재설정합니다 :
public class MyAdapter extends ArrayAdapter<CartItem> {
// This is our data-model.
// let's say your cartitems only contain an id and name
// normally this would be defined elsewhere in your code
public class CartItem {
public int id;
public String product_name;
}
/*
* ViewHolders are basically meant to keep a reference to the Views,
* so that you don't have to use .findViewById() on every getView()
* for the elements you're trying to manipulate.
*
* .findViewById() finds Views by traversing the hierarchy (heavy).
* This generally isn't a problem, but we want to avoid this in
* ListViews because getView() gets called a lot - which makes our app slow.
*
* We will want to keep a reference to our TextView and Button,
* because these are the elements we want to change every getView().
* Not to the RelativeLayout, because it's already passed in convertView
* (after you inflated it for the first time) and we're not manipulating it
* anyway.
*/
public class ViewHolder {
public Button deleteButton;
public TextView mTextView;
}
public MyAdapter(Context context, int textViewResourceId,
ArrayList<CartItem> cartItems) {
// here, we tie our data (ArrayList of CartItem's)
// to the ListAdapter (ArrayAdapter = extended ListAdapter)
super(context, textViewResourceId, cartItems);
}
// this method
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
// if convertView == null, that means we haven't inflated our listitem yet.
if(convertView == null) {
// so, we'll inflate our listitem now.
// after this, convertView will contain our RelativeLayout
// and its children/subviews
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem, null);
// now we're gonna instantiate the ViewHolder to keep a reference
// to our TextView and ImageView
holder = new ViewHolder();
holder.mTextView = (TextView) convertView
.findViewById(R.id.listitem_textview);
holder.deleteButton = (Button) convertView
.findViewById(R.id.listitem_deletebutton);
// Now that we have our reference, we want to make sure we can
// keep our reference by using tags. Tags are a way to attach
// data to a View.
convertView.setTag(holder);
} else {
// if we have already inflated our listitem, we just get the
// references to our Views from the tag
holder = (ViewHolder) convertView.getTag();
}
// we want to read/do-stuff-with a specific CartItem. First, get
// a reference to the data-object.
final CartItem mCartItem = (CartItem) getItem(position);
// now, it's time to manipulate our views
holder.mTextView.setText(mCartItem.product_name);
holder.deleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// then, pass a signal to MyAdapter that we want to remove
// this item from our dataset
remove(mCartItem);
// now, we want to update any (list)views attached to our MyAdapter
// this will let the ListView update itself
notifyDataSetChanged();
}
});
// convertView will be recycled, which means what we output here
// will be the input for the next getView()-call
return convertView;
}
}
당신이 당신의 활동에서 데이터 집합을 조작하려면
는 다음을 수행합니다. 그러나 ListAdapter 권한을 설정하면 문제가되지 않습니다.
notifydatasetchanged()를 사용해 보셨나요? – dymmeh
listadapter를 사용하여 listview를 사용하고 어댑터 (직접 메소드가 ArrayAdapter에 존재 함)를 조작하고 어댑터에서 notifyDatasetChanged를 사용하여 뷰를 새로 고침해야합니다. – njzk2