3
所以我有一個具有EditText和ListFragment的FragmentActivity。 在ListFragment中,我有一個適配器來放置所有項目。從片段獲取適配器活動
public class XXXListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
ArrayAdapter<Spanned> adapter = new ArrayAdapter<Spanned>(inflater.getContext(),
R.layout.fragment_list_item_appearance, spannedArray);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
使用EditText我希望用戶過濾listitems。
public class XXX extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quests_activity);
EditText ediText = (EditText) findViewById(R.id.editext);
ediText .addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ListFragment.this.adapter.getFilter().filter(cs);
}
...
});
}
我的問題是,我不能讓Listfragment與上下文:
ListFragment.this.adapter.getFilter().filter(cs);
以下錯誤被拋出:
類型QuestsListFragment沒有外圍實例是 範圍
任何想法如何解決這個問題?
確定這裏的整個代碼。我剛開始使用的碎片......
FragmentActivity:
public class XXX extends FragmentActivity implements ListFragmentItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx_activity);
}
@Override
protected void onStart() {
super.onStart();
EditText editText = (EditText) findViewById(R.id.edittext)
editText .addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// THIS DOESN'T WORK
XXXListFragment.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
@Override
public void onListFragmentItemClick(int position) {
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment prevFrag = fragmentManager.findFragmentByTag("xxx.details");
if (prevFrag != null)
fragmentTransaction.remove(prevFrag);
XXXDetailsFragment fragment = new XXXDetailsFragment();
Bundle b = new Bundle();
b.putInt("position", position);
fragment.setArguments(b);
fragmentTransaction.add(R.id.xxx_detail_fragment_container, fragment,
"xxx.details");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
...
}
}
...
}
ListFragment:
public class XXXListFragment extends ListFragment {
ListFragmentItemClickListener ifaceItemClickListener;
public interface ListFragmentItemClickListener {
void onListFragmentItemClick(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
ifaceItemClickListener = (ListFragmentItemClickListener) activity;
} catch (Exception e) {
Toast.makeText(activity.getBaseContext(), "Exception", Toast.LENGTH_SHORT).show();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Spanned[] xxx = { Html.fromHtml(getString(R.string.xxx)), ... };
ArrayAdapter<Spanned> adapter = new ArrayAdapter<Spanned>(inflater.getContext(),
R.layout.fragment_list_item_appearance, xxx);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
ifaceItemClickListener.onListFragmentItemClick(position);
}
}
的ListFragment在FragmentActivity佈局還說:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...
<EditText
android:id="@+id/edittext"
style="@style/red_edittext"
android:hint="@string/exittext_input_search" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/xxx_list_fragment"
android:name="com.xxx.XXXListFragment"
android:layout_width="350dp"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/xxx_detail_fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</FrameLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
添加'ListFragment'的代碼在哪裏?所有你需要做的就是持有對ListFragment的引用,並在其中有一個public方法,你可以直接調用它。 – Squonk
我把整個代碼。請看看。 – user1337
面對同樣的問題,如果任何人解決了它,請分享 – subash