如何在android中使用baseadapter刷新自定义列表视图

问题描述 投票:10回答:5

先生,我如何使用baseadapter刷新我的自定义列表视图。我不知道放置什么,或者将它放在我的代码中。请帮我。提前致谢

public class EditDetails extends Activity{
public String nameChanged;
public String numChanged;
public String name;
public String num;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editdetails);
    final EditText sqlName = (EditText)findViewById(R.id.editName);
    final EditText sqlNumber = (EditText)findViewById(R.id.editNumber);
    name = CustomListView.name;
    num = CustomListView.number;
    Button bUpdate = (Button)findViewById(R.id.editUpdate);
    Button bView = (Button)findViewById(R.id.editView);
    sqlName.setText(name);
    sqlNumber.setText(num);

    bUpdate.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            nameChanged = sqlName.getText().toString();
            numChanged = sqlNumber.getText().toString();
            GroupDb info = new GroupDb(EditDetails.this);
            info.open();
            long rowid = info.getRowId(name, num);
            info.updateNameNumber(rowid, nameChanged, numChanged);
            ArrayList<Contact> searchResults = info.getView();
            MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults);
            Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show();
            info.close();
            }
        });
    bView.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setClass(EditDetails.this, CustomListView.class);

            startActivityForResult(intent, 0);
            }
        });
}

}

这是我显示我的列表视图的地方

public class CustomListView extends Activity {
final Context context = this;
public static String name;
public static String number;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GroupDb info = new GroupDb(this);
    info.open();
    ArrayList<Contact> searchResults = info.getView();


    final ListView lv = (ListView) findViewById(R.id.srListView);
    lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
    info.close();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            // TODO Auto-generated method stub
            Object o = lv.getItemAtPosition(position);
            final Contact fullObject = (Contact)o;
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder
            .setMessage("Select action")
            .setCancelable(false)
            .setPositiveButton("Edit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Toast.makeText(getApplicationContext(), "Edit ", Toast.LENGTH_LONG).show();
                    name = fullObject.getName();
                    number = fullObject.getPhoneNumber();
                    Intent contactIntent = new Intent("myfolder.proj.EDITDETAILS");
                    startActivity(contactIntent);
                }
              })

这是我的baseadapter类

public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Contact> searchArrayList;

private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) {
    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return searchArrayList.size();
}

public Object getItem(int position) {
    return searchArrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_row_view, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.name);

        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

        holder.status = (TextView) convertView.findViewById(R.id.status);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtName.setText(searchArrayList.get(position).getName());

    holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber());

    holder.status.setText(searchArrayList.get(position).getStatus());

    return convertView;
}

static class ViewHolder {
    TextView txtName;
    TextView txtPhone;
    TextView status;
}
}
android listview baseadapter
5个回答
33
投票

两个选项:要么保留传入构造函数的ArrayList的引用,以便稍后修改实际列表数据(因为列表未复制,修改适配器外部的数据仍然会更新适配器引用的指针) ,或重写适配器以允许列表重置为另一个对象。

在任何一种情况下,在ArrayList发生变化之后,你必须调用notifyDataSetChanged()来更新你的ListView。这可以在适配器内部或外部完成。所以,例如:

public class MyCustomBaseAdapter extends BaseAdapter {
    //TIP: Don't make this static, that's just a bad idea
    private ArrayList<Contact> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
        searchArrayList = initialResults;
        mInflater = LayoutInflater.from(context);
    }

    public void updateResults(ArrayList<Contact> results) {
        searchArrayList = results;
        //Triggers the list update
        notifyDataSetChanged();
    }

    /* ...The rest of your code that I failed to copy over... */
}

HTH


8
投票

在BaseAdapter中创建一个自定义方法

喜欢:

 public void updateAdapter(ArrayList<Contact> arrylst) {
        this.arrylst= arrylst;

        //and call notifyDataSetChanged
        notifyDataSetChanged();
    }

这个函数调用你想要调用的地方:例如

adapterObject.updateAdapter(这里传递ArrayList);

完成。


2
投票

谢谢以上解决方案的人为我工作。我在每个事件中调用listupdate方法

  public void updateResults(List<TalebeDataUser> results) {
    talebeList = results;
    //Triggers the list update
    notifyDataSetChanged();
}

更新列表后,我还会在每次触摸时刷新我的按钮操作。例如,我在listview项目中有很多按钮可以点击,所以每次触摸都会改变其他风格

    private void setColor(TalebeDataUser talebeDataUser) {
    if (talebeDataUser.isVar()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_secili);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isGorevli()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_secili);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isYok()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_secili);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isIzinli()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_secili);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow);
    } else if (talebeDataUser.isHatimde()) {
        holder.mVar.setBackgroundResource(R.drawable.aw_shadow);
        holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mYok.setBackgroundResource(R.drawable.aw_shadow);
        holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow);
        holder.mHatimde.setBackgroundResource(R.drawable.aw_secili);
    }

}

只是我的一个按钮内的一个例子

  holder.mYok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //talebeList.remove(currentTalebe);
            setOgrenciNameByDurum(talebeList.get(i));
            talebeList.get(i).setYok(true);
            //setOgrenciNameByDurum(currentTalebe);
            talebeList.get(i).setVar(false);
            talebeList.get(i).setGorevli(false);
            talebeList.get(i).setIzinli(false);
            talebeList.get(i).setHatimde(false);
            updateResults(talebeList);
            setColor(talebeList.get(i));
            //saveCurrentTalebeOnShare(currentTalebe);
        }
    });

talebeList只是List<MyModel> talebeList


1
投票

我解决了这个问题,将此功能添加到我的自定义适配器

public void newCursor(Cursor cursor)
 {
  this.cursor=cursor;
  this.notifyDataSetChanged();
 }

从我的主类我创建一个新的游标,重新查询数据库,然后通过此函数发送到我的自定义适配器。

祝好运


1
投票

只需使用BaseAdapter,无需使用上下文

listsOfNotes.remove(listsOfNotes.get(position));
notifyDataSetChanged();

只需将此代码放在setOnClickListner上即可

© www.soinside.com 2019 - 2024. All rights reserved.