2

先生,如果我在 android 模拟器中按下后退按钮,如何刷新我的列表视图?我在activityA 上单击了一个项目,然后转到activityB 中的编辑表单。保存更新后,如果我按下后退按钮,它应该刷新列表。

这是我的活动A

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

这是我的活动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);
            }
        });
}

}

如果我在我的活动 B 中单击此 bView 按钮,它会更新列表视图。但按下后退按钮只会显示我之前未更新的列表视图。提前感谢您的帮助

编辑

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

}

4

1 回答 1

4

改写

虽然我建议自定义 CursorAdapter 以使用您的数据库。让我们用您当前的代码更改几点:

首先为 , 创建一个字段变量mbca(就像nameand一样number)。

MyCustomBaseAdapter mcba;

第二次更新您的初始化mbca方式onCreate()

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

第三个小改动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();
}

这是有效的,因为updateResults()调用notifyDataSetChanged()并自动更新 ListView。

于 2012-12-02T21:22:35.780 回答