先生,我如何使用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;@OverrIDepublic 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;}}
解决方法:
两个选项:要么保留传递给构造函数的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
总结以上是内存溢出为你收集整理的如何在android中使用baseadapter刷新自定义列表视图全部内容,希望文章能够帮你解决如何在android中使用baseadapter刷新自定义列表视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)