0

谁能告诉我重新加载 Spinner 的代码?

我创建了一个小应用程序,我可以在其中添加一些项目并删除不需要的项目。添加的项目将显示在微调器中。一旦我从微调器中选择了一个项目并单击删除按钮将其删除,该项目将从数据库中删除,并且我得到一个显示“已删除项目”的 Toast。但它仍然显示在微调器中,直到我注销并再次登录。在这里,我想我需要在单击删除按钮时再次重新加载微调器。任何人都可以帮我做到这一点吗?

public class DeleteChildActivity extends Activity {
    TextView name;
    Button delete;
    Spinner spinner2;

    private String URL = "/ParentProfileServlet";
    private String URL1 = "/ChildProfileServlet";
    private String URL2 = "/DeleteChildServlet";
    ArrayList<NameValuePair> postparameter;

    public static int selectChildId;
    public static String imei;

    ParentDetailsMod parentModel;
    private ArrayList<ChildDetails> childArray = null;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.delete_child);

        delete = (Button) findViewById(R.id.B_delchild);

        spinner2 = (Spinner) findViewById(R.id.childspinner);


        childArray = new SelectParser().parseSelectXml(response);
        ArrayList<String> stringArray = new ArrayList<String>();
        for (ChildDetails childModel : childArray) {
            String str;
            str = childModel.getName();
            stringArray.add(str);
        }
        // spinner = (Spinner) findViewById(R.id.spinner11);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(), android.R.layout.simple_list_item_1,
                stringArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);
        spinner2.setPrompt(getString(R.string.selectLabel));
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                imei = childArray.get(position).getImei_num();
                selectChildId = childArray.get(position).getChild_id();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // spinner.performClick();
                // String id = spinner2.getSelectedItem().toString();
                // selectChildId = id.substring(0, id.indexOf("--"));
                postparameter = new ArrayList<NameValuePair>();

                String parent_id = LoginPageActivity.id;
                postparameter
                        .add(new BasicNameValuePair("parent_id", parent_id));
                postparameter.add(new BasicNameValuePair("child_id",
                        selectChildId + ""));
                String response = null;

                try {
                    response = CustomHttpClient.executeHttpPost(URL2,
                            postparameter);
                    System.out.println("response:" + response);

                    if (response.trim().compareTo("success") == 0) {
                        Toast.makeText(getApplicationContext(),
                                "Child deleted", Toast.LENGTH_LONG).show();

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Failed to delete", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
4

2 回答 2

0

您应该从适配器列表中删除该项目,然后调用 spinner.notifyDataSetChanged() 方法来刷新您的微调器

于 2013-05-03T09:23:34.090 回答
0

您可以使用 notifyDataSetChanged(); 方法来重新加载适配器或显示更改的数据。

于 2013-05-03T09:08:04.623 回答