从另一个活动中删除行

时间:2013-04-04 21:04:12

标签: android listview android-arrayadapter listadapter notifydatasetchanged

我有一个listview,它被自定义数组适配器膨胀,onclick将它带到另一个活动,其中包含与该行相关的数据。点击删除后,它应该从列表中删除该项目并返回列表。 / p>

我正在使用以下代码:

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

finish();

但此行存在错误:

 CustomizedListView.list.removeViewAt(deleteposition);

请告诉我如何解决它?

Logcat详细信息:

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)

整个守则:

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 个答案:

答案 0 :(得分:2)

您正以错误的方式解决此问题。不要从AdapterView中删除View,只需从数据集中删除数据并调用notifyDataSetChanged()即可。 (这将自动删除不需要的视图。)