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

时间:2012-12-02 19:07:35

标签: android listview baseadapter

先生,如何使用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;
}
}

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:8)

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

像:

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

        //and call notifyDataSetChanged
        notifyDataSetChanged();
    }

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

adapterObject.updateAdapter(这里传递ArrayList);

进行。

答案 2 :(得分:2)

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

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

在updatng列表之后,我还会在每次触摸时刷新我的按钮动作。例如,我在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

答案 3 :(得分:1)

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

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

从我的主类中创建一个新的游标,对数据库进行重新查询,然后发送给我的 通过此功能自定义适配器。

祝你好运

答案 4 :(得分:1)

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

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

只需将此代码放在setOnClickListner

上即可