按下后退按钮时刷新列表视图 - android

时间:2012-12-02 21:17:33

标签: android listview

先生,如果我在Android模拟器中按下后退按钮,如何刷新我的列表视图?我点击了活动A上的项目,然后转到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()
}

这是我的activityB

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

}

如果我在activityB中点击了这个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;
}

}

1 个答案:

答案 0 :(得分:4)

重写

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

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

MyCustomBaseAdapter mcba;

第二次更新mbcaonCreate()的初始化方式:

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。