使用onListItemClick从ListView中删除所选项

时间:2015-01-06 18:50:02

标签: android listview onclicklistener

我想从列表视图中删除所选项目...在Vaccination类中,我解析了一些数据并使用Vaccination_Custom类获取所有数据。 现在我想从这个列表视图中删除所选项目(**在DailogBox中单击确定后**)我正在使用OnListItemClick .......

public class Vaccination extends ListActivity{
    ListView listView;
    SharedPreferences babyPreps;
    String CHILD_ID;
    String NOTIFICATION_ID;
    String[] Notification_id= new String[]{};
    String[] Notification_comnt= new String[]{};
    String[] Notification_date= new String[]{};

    private static final String GET_CHILD_URL = "http://10.0.2.2/Baby/Baby_login_api/get_child_vaccination.php";
    private static final String Update_Seen="http://10.0.2.2/Baby/Baby_login_api/update_seen.php";
    private ProgressDialog pDialog;
    //JSON parser class
    JSONParser jsonParser = new JSONParser();

    JSONArray Notification_array=null; 
    String Update_info="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vaccination);

        babyPreps =getSharedPreferences("BabyPrefs",0);
        //Editor editor = babyPreps.edit();
        CHILD_ID = babyPreps.getString("Key_Child_id", null);
        //Log.d("Child id :", CHILD_ID);
        listView=(ListView) findViewById(R.id.list_v);
        new Get_vaccination().execute();
    }

    class Get_vaccination extends AsyncTask<String, String, String>{

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog = new ProgressDialog(Vaccination.this);
            pDialog.setMessage("Loading info..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }


        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

             List<NameValuePair> params2 = new ArrayList<NameValuePair>();
             params2.add(new BasicNameValuePair("child_id", CHILD_ID));

             JSONObject json = jsonParser.makeHttpRequest(GET_CHILD_URL, "POST",params2 );
             Log.d("Child Single Notification ", json.toString());

             ArrayList<String> List_id = new ArrayList<String>();
             ArrayList<String> List_cmnt = new ArrayList<String>();
             ArrayList<String> List_date = new ArrayList<String>();
             try {
                Notification_array=json.getJSONArray("notifications");
                for(int i = 0; i<Notification_array.length();i++)
                {
                    JSONObject c = Notification_array.getJSONObject(i);
                    String id = c.getString("id");
                    String comnt = c.getString("comnt");
                    String date = c.getString("date");

                    List_id.add(id);
                    List_cmnt.add(comnt);
                    List_date.add(date);
                }

                Notification_id = List_id.toArray(new String[List_id.size()]);
                Notification_comnt=List_cmnt.toArray(new String[List_cmnt.size()]);
                Notification_date = List_date.toArray(new String[List_date.size()]);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();

            Vaccination_Custom vc = new Vaccination_Custom(Vaccination.this, Notification_id,Notification_comnt,Notification_date);
            setListAdapter(vc);
        }
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        String item= (String)getListAdapter().getItem(position);
        Log.d("this is notification id :", item);
        Editor editor = babyPreps.edit();
        editor.putString("Key_Notification_id", item);
        editor.commit();

        AlertDialog.Builder builder = new AlertDialog.Builder(Vaccination.this);

        builder.setMessage("Are You Show this ?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dailog, int id) {
                // TODO Auto-generated method stub

                NOTIFICATION_ID=babyPreps.getString("Key_Notification_id", null);
                Log.d("Child id :",CHILD_ID );
                Log.d("Notification Id:", NOTIFICATION_ID);

                //new Update_Seen().execute();
                dailog.cancel();

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dailog, int id) {
                // TODO Auto-generated method stub
                dailog.cancel();
            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

    class Update_Seen extends AsyncTask<String, String, String> {

        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog = new ProgressDialog(Vaccination.this);
            pDialog.setMessage("Loading info..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            List<NameValuePair> params3 = new ArrayList<NameValuePair>();
            params3.add(new BasicNameValuePair("child_id", CHILD_ID));
            params3.add(new BasicNameValuePair("notification_id", NOTIFICATION_ID));

            JSONObject json = jsonParser.makeHttpRequest(Update_Seen, "POST", params3);

            try {

                Update_info = json.getString("info");
                //Log.d("Update Return :", Update_info.toString());

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pDialog.dismiss();
        }
    }
}

这是我的Vaccination_Custom班...........

public class Vaccination_Custom extends ArrayAdapter<String>{

    Context CONTEXT;
    String[] NOTIFICATINON_ID;
    String[] NOTIFICATINON_COMNT;
    String[] NOTIFICATINON_DATE;

    public Vaccination_Custom(Context context,String[] notification_id, String[] notification_comnt,String[] notification_date) {
        // TODO Auto-generated constructor stub
        super(context, R.layout.vaccination_list_item,notification_id);

        this.CONTEXT=context;
        this.NOTIFICATINON_ID=notification_id;
        this.NOTIFICATINON_COMNT=notification_comnt;
        this.NOTIFICATINON_DATE= notification_date;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        LayoutInflater inflater = (LayoutInflater) CONTEXT
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.vaccination_list_item, parent, false);

        TextView comnt = (TextView) rowView.findViewById(R.id.notification_name);
        TextView date = (TextView) rowView.findViewById(R.id.notification_date);

        comnt.setText(NOTIFICATINON_COMNT[position]);
        date.setText(NOTIFICATINON_DATE[position]);
        return rowView;
    }
}

现在LogCat给出错误&#34;引起:java.lang.RuntimeException:您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;&#34; < / p>

AND XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_v"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

如果我剪切listview =(ListView)findviewbuId(R.id.list_v)并将XML作为&#34; android:id =&#34; @android:id / list&#34;然后它给我回复但不删除所选的Listview。

1 个答案:

答案 0 :(得分:1)

尝试使用此选项从列表中删除该项并更新适配器:

arrayAdapter = (ArrayAdapter) listView.getAdapter();
arrayAdapter.remove(arrayAdapter.getItem(position));
arrayAdapter.notifyDataSetChanged();

将其置于此onClick方法的某个地方

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int id) {
        // TODO Auto-generated method stub

        NOTIFICATION_ID=babyPreps.getString("Key_Notification_id", null);
        Log.d("Child id :",CHILD_ID );
        Log.d("Notification Id:", NOTIFICATION_ID);

        //new Update_Seen().execute();
        dialog.cancel();
    }
})