Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从其他活动中删除行_Android_Listview_Android Arrayadapter_Listadapter_Notifydatasetchanged - Fatal编程技术网

Android 从其他活动中删除行

Android 从其他活动中删除行,android,listview,android-arrayadapter,listadapter,notifydatasetchanged,Android,Listview,Android Arrayadapter,Listadapter,Notifydatasetchanged,我有一个由自定义数组适配器扩展的listview,onclick将它带到另一个活动,其中包含与该行相关的数据。单击delete后,它将从列表中删除该项并返回列表 我正在为此使用以下代码: int deleteposition=CustomizedListView.deleteposition; CustomizedListView.list.removeViewAt(deleteposition); CustomizedListView.adapter.notifyDataSetChanged(

我有一个由自定义数组适配器扩展的listview,onclick将它带到另一个活动,其中包含与该行相关的数据。单击delete后,它将从列表中删除该项并返回列表

我正在为此使用以下代码:

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

            }
        });     
    }   
}

你以错误的方式处理这个问题。不要从AdapterView中删除视图,只需从数据集中删除数据并调用
notifyDataSetChanged()
。(这将自动删除不需要的视图。)

谢谢,先生,这样做有效。将songlist设置为静态,并在newactivity中添加以下代码。CustomizedListView.songsList.remove(删除位置);不客气,我很高兴能帮上忙。您还可以尝试从适配器中删除数据:
CustomizedListView.adapter.remove(deleteposition)这样你就不必调用
notifyDataSetChanged()
。使用上面的代码,它在下面给我一个错误,删除和给出的两个选项是在lazyadapter中创建方法删除(int)或将强制转换添加到方法接收器。先生,我正在做你建议的事情吗?是的,
CustomizedListView.songsList.remove(deleteposition)
CustomizedListView.adapter.remove(删除位置)几乎相同。我不知道您正在使用哪个LazyAdapter,所以我不知道它是否有
remove()
。但如果
songsList.remove()
有效,则使用此选项。:)谢谢你,先生,这是我在上面发布的lazyAdapter类的同一类。再次感谢,我现在将使用它。祝你有一个美好的一天。
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();

                }
            });
}

}