gpt4 book ai didi

android - 使用 onItemClick 从 DialogFragment 中的 ListView 获取数据

转载 作者:行者123 更新时间:2023-11-29 15:02:11 25 4
gpt4 key购买 nike

我目前有一个带有 ImageButton 的 Fragment,可以打开 DialogFragment。此 DialogFragment 是使用适配器的自定义列表,其中每个条目都有一个 ImageView 和 TextView。

是否可以将所选项目的ImageView的“src”返回到打开DialogFragment的Fragment?我需要将 ImageButton 的 src 更改为所选图像。

我的主 fragment 调用DialogFragment:

ImageButton starter = (ImageButton) myView.findViewById(R.id.imageStarterItem);

starter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here
ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.show(getFragmentManager(), "fragmentDialog");
}
});

包含 ListView 的我的 DialogFragment 类:

public class ItemPhysStarterFragment extends DialogFragment {

Button btn;
ListView lv;
SearchView sv;
ArrayAdapter<String> adapter;
String[] names={"Bumba's Mask", "Rangda's Mask", "Bluestone Pendant", "Death's Toll",
"Mark of the Vanguard", "Swift Wing", "War Flag", "Watcher's Gift"};
int[] images={R.drawable.bumbas_mask, R.drawable.rangdas_mask, R.drawable.bluestone_pendant, R.drawable.deaths_toll,
R.drawable.mark_of_the_vanguard, R.drawable.swift_wing, R.drawable.war_flag, R.drawable.watchers_gift};

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View myView=inflater.inflate(R.layout.item_list, null);

//SET TITLE DIALOG TITLE
getDialog().setTitle("Starter Items");

//BUTTON,LISTVIEW,SEARCHVIEW INITIALIZATIONS
lv=(ListView) myView.findViewById(R.id.listView1);
sv=(SearchView) myView.findViewById(R.id.searchView1);
btn=(Button) myView.findViewById(R.id.dismiss);

//CREATE AND SET ADAPTER TO LISTVIEW
final Adapter adapter = new Adapter(getActivity(), this.getItems());
lv.setAdapter(adapter);

//SEARCH
sv.setQueryHint("Search starter items..");
sv.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextSubmit(String txt) {
return false;
}
public boolean onQueryTextChange(String txt) {
adapter.getFilter().filter(txt);
return false;
}
});

//BUTTON
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dismiss();
}
});

return myView;
}

public static ItemPhysStarterFragment newInstance(){
ItemPhysStarterFragment f = new ItemPhysStarterFragment();
return f;
}

private ArrayList<Item> getItems() {

ArrayList<Item> items = new ArrayList<Item>();
Item item;

for(int i = 0; i < names.length; i++)
{
item = new Item(names[i], images[i]);
items.add(item);
}

return items;

}


}

最佳答案

先创建一个接口(interface)

public interface OnListItemClickedListener {
void onListItemClick(int resourceId);
}

接口(interface)创建setter

public class ItemPhysStarterFragment extends DialogFragment {
OnListItemClickedListener mListener;

public void setOnListItemSelectedListener(OnListItemClickedListener listener) {
this.mListener = listener;
}
}

现在将监听器设置为您的 fragment

ItemPhysStarterFragment dialog = ItemPhysStarterFragment.newInstance();
dialog.setOnListItemSelectedListener(new OnListItemClickedListener() {
@Override
public void onListItemClick(int resourceId) {
// User your resourceId
}
});
dialog.show(getFragmentManager(), "fragmentDialog");

最后一步,在 ItemPhysStarterFragment fragment 的 ListItemClick() 中触发监听器 -

...public void onListItemClick(int resourceId) {
mListener.onListItemClick(pass your resource id);
}

关于android - 使用 onItemClick 从 DialogFragment 中的 ListView 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42700049/

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