简体   繁体   English

按下后退按钮时刷新listview-android

[英]refresh listview when back button is pressed - android

sir, how do i refresh my listview if i pressed the back button in android emulator? 先生,如果我在android模拟器中按下“后退”按钮,如何刷新列表视图? i've clicked an item on activityA then goes to an edit form in activityB. 我单击了活动A上的一个项目,然后转到活动B中的编辑表单。 after saving updates, if i pressed the back button, it should refresh the list. 保存更新后,如果我按了后退按钮,它将刷新列表。

here is my activityA 这是我的活动

public class CustomListView extends Activity {
final Context context = this;
public static String name;
public static String number;
int REQUEST_CODE = 0;
@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();

    MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(CustomListView.this, searchResults);
    mcba.updateResults(searchResults);
    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 intent = new Intent();
                    intent.setClass(CustomListView.this, EditDetails.class);

                    startActivityForResult(intent, REQUEST_CODE);

                }
              })

             .setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Toast.makeText(getApplicationContext(), "Delete ", Toast.LENGTH_LONG).show();

                }
              }) 
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }

    });
}
public void onResume()
{  // After a pause OR at startup
super.onResume();
//Refresh your stuff here
GroupDb info = new GroupDb(this);
info.open();
ArrayList<Contact> searchResults = info.getView();

MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(CustomListView.this, searchResults);
mcba.updateResults(searchResults);

info.close();
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == REQUEST_CODE) {
        GroupDb info = new GroupDb(this);
        info.open();
        ArrayList<Contact> searchResults = info.getView();

        MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(CustomListView.this, searchResults);
        mcba.updateResults(searchResults);

        info.close();
    }
}
//edit or delete list when clicked
public void deleteEditOption()
{

}//end deleteEditOption()
}

and here is my activityB 这是我的活动B

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+1, nameChanged, numChanged);
            Toast.makeText(getApplicationContext(), rowid+" "+nameChanged+" "+numChanged, Toast.LENGTH_LONG).show();
            ArrayList<Contact> searchResults = info.getView();
            MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults);
            mcba.updateResults(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);
            }
        });
}

}

if i clicked this bView button in my activityB, it updates the listview. 如果我在activityB中单击此bView按钮,它将更新listview。 but pressing the back button just shows my previous unupdated listview. 但按“后退”按钮仅显示我以前的未更新列表视图。 thanks for help in advance 预先感谢您的帮助

edit 编辑

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 void updateResults(ArrayList<Contact> results) {
    searchArrayList = results;
    //Triggers the list update
    notifyDataSetChanged();
}

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

} }

Rewrite 改写

While I recommend customizing a CursorAdapter to work with your database. 虽然我建议自定义CursorAdapter以与您的数据库一起使用。 Let's change a couple points with your current code: 让我们用当前代码更改几点:

First create a field variable for mbca , (just like name and number ). 首先为mbca创建一个字段变量(就像namenumber )。

MyCustomBaseAdapter mcba;

Second update how you initialize mbca in onCreate() : 第二次更新如何在onCreate()初始化mbca

mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults);
//mcba.updateResults(searchResults); this line isn't necessary here

Third make a small change to onResume() : 第三,对onResume()进行少量更改:

public void onResume()
{  // After a pause OR at startup
    super.onResume();
    //Refresh your stuff here
    GroupDb info = new GroupDb(this);
    info.open();
    mcba.updateResults(info.getView());
    info.close();
}

This works because updateResults() calls notifyDataSetChanged() and this updates the ListView automatically. 之所以notifyDataSetChanged()是因为updateResults()调用notifyDataSetChanged()并自动更新ListView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM