gpt4 book ai didi

android - 如何在android中使用baseadapter刷新自定义 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:47 27 4
gpt4 key购买 nike

先生,我如何使用 baseadapter 刷新我的自定义 ListView 。我不知道该放什么,或者在我的代码中放在哪里。请帮我。提前致谢

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);
}
});
}

}

这里是我显示 ListView 的地方

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);
}
})

这是我的基础适配器类

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 的引用,以便稍后修改实际的列表数据(因为列表未被复制,修改Adapter 仍然会更新 Adapter 引用的指针),或者重写 Adapter 以允许将列表重置为另一个对象。

无论哪种情况,在 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 - 如何在android中使用baseadapter刷新自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672700/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com