2017-06-13 23 views
-1

我想添加刪除按鈕旁邊我的項目的細節基於this answer。 我嘗試了一個新的項目,它的工作原理perefect:自定義listview片段 - 不能使用getActivity()

enter image description here

,當我點擊刪除,我刪除的項目。

不幸的是,當我的listView在calendar_tab.xml中時,我試圖在我自己的項目中使用它。 calendar_tab使用CompactCalendarTab.java - 片段類。 因此Android工作室出錯: E:\下載\ MyCustomAdapter.java

錯誤:(49,63)錯誤:無法找到符號方法getSystemService(字符串)

我試圖改變

LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LayoutInflater充氣=(LayoutInflater)getActivity()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。

但沒有運氣。

enter image description here

custom_listview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/list_item_string" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:paddingLeft="8dp" 
    android:textSize="18sp" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/delete_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="5dp" 
    android:text="Delete" /> 

calendar_tab.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:id="@+id/calendar_tab" 
android:layout_height="match_parent" 
> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/toolbar_calendar" 
    android:background="@color/teal_300" 
    android:layout_alignParentTop="true" 
    android:padding="10sp" 
    android:layout_alignParentStart="true"> 

    <ImageButton 
     android:id="@+id/back_button" 
     android:src="@mipmap/ic_arrow_back_black_24dp" 
     android:background="@null" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:onClick="goBackmain" 
     /> 

<ImageButton 
    android:id="@+id/next_button" 
    android:src="@mipmap/ic_keyboard_arrow_left_black_24dp" 
    android:background="@null" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/showdate" 
    android:layout_toStartOf="@+id/showdate" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textSize="25sp" 
    android:textStyle="bold" 
    android:textColor="@color/black" 
    android:id="@+id/showdate" 
    android:layout_alignBaseline="@+id/prev_button" 
    android:layout_alignBottom="@+id/prev_button" 
    android:layout_centerHorizontal="true" /> 

<ImageButton 
    android:id="@+id/prev_button" 
    android:src="@mipmap/ic_keyboard_arrow_right_black_24dp" 
    android:background="@null" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/showdate" 
    android:layout_toEndOf="@+id/showdate" /> 

    </RelativeLayout> 

<com.github.sundeepk.compactcalendarview.CompactCalendarView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/compactcalendar_view" 
    android:layout_width="fill_parent" 
    android:layout_height="250dp" 
    app:compactCalendarTargetHeight="250dp" 
    app:compactCalendarTextSize="12sp" 
    app:compactCalendarBackgroundColor="@null" 
    app:compactCalendarTextColor="@color/blue_grey_700" 
    app:compactCalendarCurrentSelectedDayBackgroundColor="@color/teal_300" 
    app:compactCalendarCurrentDayBackgroundColor="@color/teal_600" 
    app:compactCalendarCurrentDayIndicatorStyle="fill_large_indicator" 
    app:compactCalendarEventIndicatorStyle="small_indicator" 
    app:compactCalendarOtherMonthDaysTextColor="#534c4c" 
    app:compactCalendarShouldSelectFirstDayOfMonthOnScroll="true" 
    android:layout_below="@+id/toolbar_calendar" 
    /> 


<ListView 
    android:id="@+id/bookings_listview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/compactcalendar_view" 
    > 
</ListView> 

我的片段:

public class CompactCalendarTab extends Fragment { 
final ListView bookingsListView = (ListView) v.findViewById(R.id.bookings_listview); 
    adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mutableBookings); 
    final ArrayList<String> list = new ArrayList<>(); 
    final MyCustomAdapter adapter = new MyCustomAdapter(list, this); 
    bookingsListView.setAdapter(adapter); 
    compactCalendarView = (CompactCalendarView) 
    v.findViewById(R.id.compactcalendar_view); 
    } 

我的自定義適配器:

public class MyCustomAdapter extends BaseAdapter implements ListAdapter { 
private ArrayList<String> list = new ArrayList<String>(); 
private CompactCalendarTab context; 



public MyCustomAdapter(ArrayList<String> list, CompactCalendarTab context) { 
    this.list = list; 
    this.context = context; 
} 

@Override 
public int getCount() { 
    return list.size(); 
} 

@Override 
public Object getItem(int pos) { 
    return list.get(pos); 
} 

@Override 
public long getItemId(int pos) { 
    return 0; 
    //just return 0 if your list items do not have an Id variable. 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.custom_listview, null); 
    } 

    //Handle TextView and display string from your list 
    TextView listItemText = (TextView)view.findViewById(R.id.list_item_string); 
    listItemText.setText(list.get(position)); 

    //Handle buttons and add onClickListeners 
    Button deleteBtn = (Button)view.findViewById(R.id.delete_btn); 

    deleteBtn.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      //do something 
      list.remove(position); //or some other task 
      notifyDataSetChanged(); 
     } 
    }); 

    return view; 
} 

}

+0

將上下文'getActivity()'作爲適配器構造函數的參數 – Ali

回答

1

改變這一行

LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

對此

LayoutInflater inflater = LayoutInflater.from(parent.getContext());