gpt4 book ai didi

android - 从 Fragment 获取 Activity 中的 Adapter

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:38 26 4
gpt4 key购买 nike

所以我有一个 FragmentActivity,它有一个 EditText 和一个 ListFragment。在 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 过滤列表项。

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

抛出以下错误:

No enclosing instance of the type QuestsListFragment is accessible in scope

有什么解决办法吗?


好的,这里是整个代码。我刚开始使用 Fragments...

fragment Activity :

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 {
...
}
}

...
}

列表 fragment :

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

}

}

在 FragmentActivity 布局中添加了 ListFragment:

<?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 类中创建一个公共(public)方法 getAdapterOfList(),它将在内部访问适配器并返回它。

关于android - 从 Fragment 获取 Activity 中的 Adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com