remove row from another activity

穿精又带淫゛_ 提交于 2019-12-11 02:36:45

问题


I have a listview which is inflated by custom array adapter and onclick takes it to another activity with the data related to that row .After clicking on delete its supposed to delete the item from the list and get back to the list.

I am using the code below for this :

int deleteposition=CustomizedListView.deleteposition;
CustomizedListView.list.removeViewAt(deleteposition);
CustomizedListView.adapter.notifyDataSetChanged();

finish();

But there is an error at this line :

 CustomizedListView.list.removeViewAt(deleteposition);

Please Tell me how to fix it ?

Logcat Details:

java.lang.UnsupportedOperationException: removeViewAt(int) is not supported in AdapterView
at android.widget.AdapterView.removeViewAt(AdapterView.java:511)
    at com.example.androidhive.openedmsg$1.onClick(openedmsg.java:35)
    at android.view.View.performClick(View.java:3549)
    at android.view.View$PerformClick.run(View.java:14393)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4945)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

The entire Code :

CustomizedListView.class

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "https://itunes.apple.com/us/rss/topalbums/limit=20/json";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    static ListView list;
    static LazyAdapter adapter;

    HashMap<String, String> map;

    public static  String newactivityno;

    public static int deleteposition;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        JSONObject json = JSONfunctions.getJSONfromURL(URL);


        try {
            JSONObject arr2 = json.getJSONObject("feed");
            JSONArray arr = arr2.getJSONArray("entry");

            for (int i = 0; i < arr.length(); i++) {
                JSONObject e1 = arr.getJSONObject(i);

                JSONArray arr3 = e1.getJSONArray("im:image");

                JSONObject arr8 = e1.getJSONObject("im:name");

                JSONObject arr10 = e1.getJSONObject("im:artist");

                    JSONObject e12 = arr3.getJSONObject(0);

            // creating new HashMap
             map = new HashMap<String, String>();

            map.put(KEY_THUMB_URL,  e12.getString("label"));

            map.put(KEY_ARTIST, arr8.getString("label"));
            map.put(KEY_TITLE, arr10.getString("label"));
            // adding HashList to ArrayList
            songsList.add(map);
            }

        } catch (JSONException e) {
            // Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getBaseContext(),
                    "Network communication error!", 5).show();
        }


        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {

                                @SuppressWarnings("unchecked")
                                HashMap<String, String> o= (HashMap<String, String>) list.getItemAtPosition(position);

                           //   Toast.makeText(CustomizedListView.this, "ID '" + o.get("title") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

                                deleteposition=position;
                             newactivityno= o.get("title");
                             Intent ii= new Intent(getBaseContext(),newactivity.class); 
                                startActivity(ii);

            }
        });     
    }   
}

newactivity.class

public class newactivity extends Activity{


     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newlayout);


            TextView tv1=(TextView)findViewById(R.id.textView11);
            TextView tv2=(TextView)findViewById(R.id.textView12);

            Button bn=(Button)findViewById(R.id.button1);

            tv2.setText(CustomizedListView.newactivityno);


            bn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int deleteposition=CustomizedListView.deleteposition;
                    CustomizedListView.list.removeViewAt(deleteposition);
                    CustomizedListView.adapter.notifyDataSetChanged();

                    finish();

                }
            });
}

}

回答1:


You are approaching this problem in the wrong manner. Rather than remove the View from the AdapterView, simply remove the data from the data set and call notifyDataSetChanged(). (This will remove the unwanted View automatically.)



来源:https://stackoverflow.com/questions/15821664/remove-row-from-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!