0

안녕하세요 저는 서버에서 데이터를 가져와 내 사용자 정의 어댑터에 전달하여 ListView를 채 웁니다. 아래는 내 맞춤 어댑터 코드입니다.ListView에서 행을 삭제합니다 onClickListener의 사용자 정의 어댑터로 작성했습니다.

public class AppointmentAdapter extends BaseAdapter { 

private AppointmentAdapter adapter; 
private Activity activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater=null; 
HashMap<String, String> todo = new HashMap<String, String>(); 
// String confirmation; 
RelativeLayout confirm_corner; 


public AppointmentAdapter(Activity a, ArrayList<HashMap<String,  String>> d) { 
    activity = a; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    //imageLoader=new ImageLoader(activity.getApplicationContext()); 
} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 



public View getView(final int position, View convertView, ViewGroup parent) { 
    //View vi=convertView; 
    //if(convertView==null) 
     final View vi = inflater.inflate(R.layout.appointments_list_item, null); 
    adapter = new AppointmentAdapter(activity,data); 
    TextView aid = (TextView)vi.findViewById(R.id.aid); // id 
    TextView apptitle = (TextView)vi.findViewById(R.id.apptitle); // title 
    TextView starts_date = (TextView)vi.findViewById(R.id.starts_date); // created_at 
    TextView starts_time = (TextView)vi.findViewById(R.id.starts_time); 
    TextView contact = (TextView)vi.findViewById(R.id.contact); 
    TextView confirmation = (TextView)vi.findViewById(R.id.confirmation); 
    confirm_corner = (RelativeLayout)vi.findViewById(R.id.confirm_corner); 

    //CheckBox check = (CheckBox)vi.findViewById(R.id.tododone); // checkbox 


    todo = data.get(position); 

    // Setting all values in listview 
    aid.setText(todo.get(AppointmentsFragment.TAG_AID)); 
    apptitle.setText(todo.get(AppointmentsFragment.TAG_APPTITLE)); 
    starts_date.setText(todo.get(AppointmentsFragment.TAG_STARTDATE)); 
    starts_time.setText(todo.get(AppointmentsFragment.TAG_STARTTIME)); 
    contact.setText(todo.get(AppointmentsFragment.TAG_CONTACT)); 
    confirmation.setText(todo.get(AppointmentsFragment.TAG_CONFIRMATION)); 
    String test = todo.get(AppointmentsFragment.TAG_USER_EMAIL); 
    //Handle buttons and add onClickListeners 
    ImageView accept = (ImageView)vi.findViewById(R.id.accept); 
    ImageView deny = (ImageView)vi.findViewById(R.id.deny); 
    //String test = confirmation.getText().toString(); 
    //&& (!test.equals(MainActivity.user_email)) 
    Log.d("CONFIRMATION: ", MainActivity.user_email); 
    if (confirmation.getText().toString().equals("pending") ){ 
     Log.d("PASS CONFIRMATION: ", confirmation.getText().toString()); 
     confirm_corner.setVisibility(View.VISIBLE); 
    } 

    accept.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      //do something 
      confirm_corner = (RelativeLayout)vi.findViewById(R.id.confirm_corner); 
      Log.d("Button accept: ", MainActivity.user_email); 
      new Confirm_appointment().execute(); 
      //vi.setBackgroundColor(Color.RED); 
      confirm_corner.setVisibility(View.GONE); 


     } 
    }); 
    deny.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      //do something 
      Log.d("Button deny: ", MainActivity.user_email); 
      new Deny_appointment().execute(); 
      confirm_corner.setVisibility(View.INVISIBLE); 
      //adapter = new AppointmentAdapter(this,data); 
      data.remove(position); 
      data.clear(); 
      data.addAll(data); 
      adapter.notifyDataSetChanged(); 
     } 
    }); 


    return vi; 
} 

제거 할 행의 거부 버튼을 클릭하면됩니다. 이 코드를 수정하려면 무엇을 변경해야합니까?

답변

1

AppointmentAdapter 안에 새 AppointmentAdapter을 만들지 마십시오. 행을 제거한 후 notifyDataSetChanged()을 시도하면 잘못된 어댑터에 알립니다. 데이터를 변경 한 어댑터가 아닌 새로 생성 된 어댑터에 알립니다. 새로 생성 된 목록은 목록보기에 첨부되지 않으므로 알림이 도달 할 수 있도록 할 수 없습니다.

ListView와 함께 사용하려면 하나의 어댑터 만 필요합니다. 항목보기. 항목보기 당 하나의 어댑터를 구성하지만 작동하지 않을 수 있습니다. 어댑터는 항목보기가 아닌 ListView와 협력해야합니다.

코드와는 다른 많은 일이 있으며 뷰 홀더 패턴을 구현해야한다고 생각합니다. 당신은 그것에 대해 here 읽을 수 있습니다. 대신 adapter.notifyDataSetChanged()를 호출

갱신

당신은 this에 전화를해야합니다. 여기서 this은 방금 행을 제거한 어댑터 여야합니다. View.OnClickListener에 대해 익명 (비 정적) 내부 클래스를 사용하고 있으므로 원하는 어댑터 인 AppointmentAdapter.this을 사용하여 외부 클래스에 연결할 수 있습니다.

이 시도 :

deny.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View v) { 
     //do something 
     Log.d("Button deny: ", MainActivity.user_email); 
     new Deny_appointment().execute(); 
     confirm_corner.setVisibility(View.INVISIBLE); 
     AppointmentAdapter.this.data.remove(position); 
     AppointmentAdapter.this.notifyDataSetChanged(); 
    } 
}); 

을 그건 그렇고, 내가 다시 당신이 명확한 데이터 출력 및 라인은 단지 잘못된 것 같다 윌리스에 동의합니다. 그래서 나는 그들을 버렸다.

+0

바로 위의 if 문에서 특정 행에 사용할 수 있습니다. 새 AppointmentAdapter를 삭제하면 adapter.notifyDataSetChanged()에서 NullExceptionError가 발생합니다.작동 할 것이라고 생각하는 코드를 붙이시겠습니까 – appLogic

+0

이제'adapter'가 할당되지 않고 null로 남았 기 때문입니다. 그 변수를 전혀 필요로하지 않는다면,'private AppointmentAdapter adapter;'라인을 제거하십시오. 자세한 내용은 업데이트 된 답변을 확인하십시오. –

1

Adapter (adapter.notifyDataSetChanged())를 업데이트 단순히 data.remove(position)를 호출하여 ListView에서 요소를 제거하려면 다음과 같은 두 가지 호출?

data.clear(); 
data.addAll(data); 

의 목적은 무엇인가. 그게 작동하는지 확인하십시오.

+0

당신의 방법을 사용할 때 자동으로 ui를 업데이트하지 않습니다. 조각을 포함하는 탭을 스 와이프해야하며 행을 반환 할 때 사라집니다. 자동으로 처리하지 않아야합니까? – appLogic

+0

왜'adapter' getView 메소드에서'onClickListener'가 생깁니 까? 리스너를'Fragment'에 넣고 거기에서 행을 지워야합니다. – Willis

+0

각 행의 단추 일 때 getView 내부에서 onClickListener를 사용하는 많은 예제를 보았습니다. – appLogic