2

사용자 정의 배열 어댑터에 의해 부풀려진 목록보기가 있는데 onclick은 해당 행과 관련된 데이터가있는 다른 활동으로 이동합니다. 목록에서 항목을 삭제하고 다시 가져 오기로 한 삭제를 클릭하십시오. 목록에.다른 활동에서 행 제거

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

finish(); 

을하지만 오류가이 라인에있다 :

나는이 아래의 코드를 사용하고

CustomizedListView.list.removeViewAt(deleteposition); 

해결 방법을하는 방법을 가르쳐주세요?

로그 캣 세부 사항 :

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

       } 
      }); 
} 

} 

답변

2

당신은이 문제에 접근하고 잘못된 태도. View를 AdapterView에서 제거하는 대신 데이터 세트에서 데이터를 제거하고 notifyDataSetChanged()으로 호출하면됩니다. (원하지 않는보기가 자동으로 제거됩니다.)

+0

감사합니다. songlist를 정체시키고 신기술에서 아래 코드를 추가했습니다. ** CustomizedListView.songsList.remove (deleteposition); ** –

+0

여러분, 반갑습니다. 어댑터에서 데이터를 삭제할 수도 있습니다 :'CustomizedListView.adapter.remove (deleteposition);'이렇게하면 notifyDataSetChanged()를 호출 할 필요가 없습니다. – Sam

+0

위의 코드를 사용하면 remove 아래에 오류가 발생하고 주어진 2 개의 옵션은 lazyadapter에서 remove (int) 메서드를 만들거나 메서드 수신기에 캐스트를 추가하는 것입니다 .Sir 내가 제안한 것과 동일한 작업을 수행하고 있습니까? –