안녕하세요 저는 서버에서 데이터를 가져와 내 사용자 정의 어댑터에 전달하여 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;
}
제거 할 행의 거부 버튼을 클릭하면됩니다. 이 코드를 수정하려면 무엇을 변경해야합니까?
바로 위의 if 문에서 특정 행에 사용할 수 있습니다. 새 AppointmentAdapter를 삭제하면 adapter.notifyDataSetChanged()에서 NullExceptionError가 발생합니다.작동 할 것이라고 생각하는 코드를 붙이시겠습니까 – appLogic
이제'adapter'가 할당되지 않고 null로 남았 기 때문입니다. 그 변수를 전혀 필요로하지 않는다면,'private AppointmentAdapter adapter;'라인을 제거하십시오. 자세한 내용은 업데이트 된 답변을 확인하십시오. –