我已经在我的ListView中实现了一个搜索栏,到目前为止它的工作正常,除了 适配器表现得很奇怪。每当我调用其清除功能时,我的列表视图仍会显示“旧”内容。
public abstract class MYLISTITEM extends BaseAdapter {
private Context activity;
private List<String> data;
private static LayoutInflater inflater=null;
private int ItemIcon;
private boolean moreVis;
public MYISTITEM(Context a, int IconID,boolean vis) {
activity = a;
data = new ArrayList<String>();
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ItemIcon = IconID;
moreVis = vis;
}
public void add(String object)
{
data.add(object);
notifyDataSetChanged();
}
public void replace(int index,String object)
{
data.set(index, object);
notifyDataSetChanged();
}
public void clear()
{
data.clear();
notifyDataSetChanged();
}
public void remove(Object object)
{
data.remove(object);
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public void insert(String object,int c)
{
data.set(c, object);
notifyDataSetChanged();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
if(position >= 0 && position < data.size())
{
return data.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class ViewHolder{
public TextView text;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item,parent, false);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(data.get(position));
holder.image.setImageDrawable(activity.getResources().getDrawable(ItemIcon));
ImageButton btn_more = (ImageButton) vi.findViewById(R.id.more);
if(moreVis)
{
btn_more.setOnClickListener(new OnMoreClickListener(btn_more,position));
}
else
{
btn_more.setVisibility(View.GONE);
}
vi.setOnClickListener(new OnRowClickListener(vi,position));
return vi;
}
public abstract void OnRowClick(View mView, int position);
public abstract void OnMoreClick(View mView, int position);
private class OnRowClickListener implements OnClickListener{
private int mPosition;
private View mView;
OnRowClickListener(View view,int position){
mPosition = position;
mView = view;
}
@Override
public void onClick(View arg0) {
OnRowClick(mView,mPosition);
}
}
private class OnMoreClickListener implements OnClickListener{
private int mPosition;
private View mView;
OnMoreClickListener(View view,int position){
mPosition = position;
mView = view;
}
@Override
public void onClick(View arg0) {
OnMoreClick(mView,mPosition);
}
}
}
调试器显示只更改了数据大小但没有更改其内容。我在这里想念什么吗?
一些额外的代码:
public abstract class MYSCREEN extends ListView{
public abstract void Init();
public abstract void OnLoadFinished();
public abstract void OnRowClicked(int position);
public abstract void onCreateQuickAction(MYQUICKACTION action,int position);
public abstract void CreateOptionMenu(Menu menu);
public abstract boolean OptionsItemSelected(MenuItem item);
public MYLISTITEM adapter = null;
public static void StartupScreen(Activity current,Class<?> next)
{
Intent myIntent = new Intent(current,next);
current.startActivity(myIntent);
}
public MYSCREEN(Activity context,int Icon) {
super(context);
boolean moreisvis = true;
MYQUICKACTION canarybirdybirdy = new MYQUICKACTION(new View(context));
onCreateQuickAction(canarybirdybirdy,0);
if(canarybirdybirdy.anchor.getVisibility() != View.VISIBLE)
{
moreisvis = false;
}
adapter = new MYLISTITEM(context,Icon,moreisvis){
@Override
public void OnRowClick(View mView, int position) {
// TODO Auto-generated method stub
OnRowClicked(position);
}
@Override
public void OnMoreClick(View mView, int position) {
// TODO Auto-generated method stub
final MYQUICKACTION mQuickAction = new MYQUICKACTION(mView);
final ImageButton mMoreImage = (ImageButton) mView.findViewById(MY.views.R.id.more);
mMoreImage.setImageResource(MY.views.R.drawable.ic_list_more_selected);
onCreateQuickAction(mQuickAction,position);
mQuickAction.setAnimStyle(MYQUICKACTION.ANIM_AUTO);
mQuickAction.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
mMoreImage.setImageResource(MY.views.R.drawable.ic_list_more);
}
});
mQuickAction.show();
}
};
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage("loading...");
dialog.show();
new LOADER(){
@Override
public void RunThread() {
Init();
}
@Override
public void FinishLoad() {
dialog.dismiss();
OnLoadFinished();
}
}.Start();
}
public abstract class MYTHREAD extends AsyncTask<Void, Void, Void> {
public abstract void InitThread();
public abstract void RunThread();
public abstract void FinishThread();
@Override
protected Void doInBackground(Void... params) {
RunThread();
// TODO Auto-generated method stub
return null;
}
//ui stuff allowed
@Override
protected void onPreExecute() {
InitThread();
}
//ui stuff allowed
@Override
protected void onProgressUpdate(Void... value) {
super.onProgressUpdate(value);
}
//ui stuff allowed
@Override
protected void onPostExecute(final Void unused) {
FinishThread();
}
public void Start()
{
this.execute();
}
}
public abstract class LOADER extends MYTHREAD{
public abstract void FinishLoad();
@Override
public void InitThread() {
// TODO Auto-generated method stub
}
@Override
public void FinishThread() {
// TODO Auto-generated method stub
FinishLoad();
}
}}
这是让我头痛的功能。它来自MYSCREEN派生类之一,哦,它也是从搜索按钮clickevent中调用的。
@Override
public void OnLoadFinished() {
// TODO Auto-generated method stub
SelectedTable = TabController.Instance.Clients[TabController.Instance.selectedtab ? 1 : 0].DbTablename;
adapter.clear();
TabController.Instance.Clients[TabController.Instance.selectedtab ? 1 : 0].MetaEntities(SelectedTable, true, new ClientCallBackHandler(){
@Override
public void StartCallBack() {
// TODO Auto-generated method stub
prgdialog.setMessage("connecting service...");
prgdialog.show();
}
@Override
public void EndCallBack(boolean success) {
// TODO Auto-generated method stub
if(success)
{
try{
ExceptionResponse except = (ExceptionResponse) this.response;
ab.setMessage(Html.fromHtml("<b><font color=#ff0000>" + except.Message));
ab.setPositiveButton("ok", null);
ab.show();
success = false;
}catch(Exception e)
{
MetadataEntitiesResponse resp = (MetadataEntitiesResponse) response;
if(resp.Entities[0] != null)
{
Ent = resp.Entities[0];
int i = 0;
//search for the first string in our database ...
for(AttributeMetaData Att : Ent.Attributes)
{
if(Att.Type.contentEquals("string"))
{
SelectedAttribute = i;
break;
}
i++;
}
StartQuery(0); //here the adapter gets filled with info
}
}
}
}
});
}
private void StartQuery(int page)
{
if(currentpage != -1)
{
nextpage = page;
return;
}
Query query = new Query();
query.EntityName = Ent.Name;
if(SearchFilter != null)
{
query.Filter = new Filter();
query.Filter.Filters = SearchFilter;
query.Filter.FilterOperator = Filter.E_FilterOperator.And;
}
query.Columns = new AllColumns();
query.Limit = new Limit();
query.Limit.Count = 15;
Order order = new Order();
order.OrderType = Order.OrderTypeE.Ascending;
order.AttributeName = Ent.Attributes[SelectedAttribute].Name;
query.Orders = new Order[] {order};
currentpage = query.Limit.Page = page;
TabController.Instance.Clients[TabController.Instance.selectedtab ? 1 : 0].RetrieveMultiple(query, new ClientCallBackHandler(){
@Override
public void StartCallBack() {
// TODO Auto-generated method stub
}
@Override
public void EndCallBack(boolean success) {
// TODO Auto-generated method stub
try{
ExceptionResponse except = (ExceptionResponse) this.response;
ab.setMessage(Html.fromHtml("<b><font color=#ff0000>" + except.Message));
ab.setPositiveButton("ok", null);
ab.show();
success = false;
}catch(Exception e)
{
RetrieveMultipleResponse rmResp = (RetrieveMultipleResponse) this.response;
if(adapter.getCount() == 0)
{
for(int i = 0; i< rmResp.TotalRecords;i++)
{
prgdialog.setMessage( ((int) (i/rmResp.TotalRecords * 100)) + "%");
adapter.add("connecting...");
}
prgdialog.dismiss();
}
int i = 0;
for(Entity ent : rmResp.Entities)
{
adapter.replace(currentpage*15+i, ent.Properties[SelectedAttribute].ToString());
i++;
}
if(currentpage == nextpage)
{
nextpage = -1;
}
else
{
StartQuery(nextpage);
}
currentpage = -1;
}
}
});
}
答案 0 :(得分:5)
我试过这段代码.. 在我们创建CustomListView适配器的oncreate()中,调用一个函数。
adapter.clear();
CustomListViewAdapter类上的创建一个清晰的函数
public void clear() {
// TODO Auto-generated method stub
movieItems.clear();
}
其中movieItems是ListView的名称
答案 1 :(得分:0)
改为呼叫notifyDataSetChanged()
。
一般情况下,如果项属性发生更改,则使用notifyDataSetInvalidated();如果项本身发生更改,则使用notifyDataSetChanged()(即删除或添加项目)。