如何在android中去listview适配器项目点击监听器片段?
Posted
技术标签:
【中文标题】如何在android中去listview适配器项目点击监听器片段?【英文标题】:How to go listview adapter item click listener to fragment in android? 【发布时间】:2018-02-17 01:07:36 【问题描述】:我想从 listview 适配器 itemview 单击侦听器转到带有额外详细信息的片段,我想在我去的那个片段中使用这些详细信息。我使用这个列表视图适配器类。我想从 listview 适配器 itemview 单击侦听器转到带有我想在我去的那个片段中使用的额外细节的片段。我使用这个列表视图适配器类。
public class ListViewAdapter extends BaseAdapter
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist)
this.context = context;
data = arraylist;
@Override
public int getCount()
return data.size();
@Override
public Object getItem(int position)
return null;
@Override
public long getItemId(int position)
return 0;
public View getView(final int position, View convertView, ViewGroup parent)
// Declare Variables
TextView ip;
TextView port;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
ip = (TextView) itemView.findViewById(R.id.ip);
port = (TextView) itemView.findViewById(R.id.port);
// Locate the ImageView in listview_item.xml
// Capture position and set results to the TextViews
ip.setText(resultp.get(AddFragment.ip));
port.setText(resultp.get(AddFragment.port));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener()
@Override
public void onClick(View arg0)
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, MainActivity.class);
// Pass all data rank
intent.putExtra("ip", resultp.get(AddFragment.ip));
// Pass all data country
intent.putExtra("port", resultp.get(AddFragment.port));
intent.putExtra("uname", resultp.get(AddFragment.uname));
intent.putExtra("password", resultp.get(AddFragment.password));
Log.e("uname: ", "> " + resultp.get(AddFragment.uname));
Log.e("password: ", "> " + resultp.get(AddFragment.password));
Log.e("ip: ", "> " + resultp.get(AddFragment.ip));
Log.e("port: ", "> " + resultp.get(AddFragment.port));
// Pass all data population
// Pass all data flag
// Start SingleItemView Class
context.startActivity(intent);
);
return itemView;
所以我如何去片段而不是活动。 我想用这个额外的细节进入片段。那么,我该怎么走?
【问题讨论】:
您的片段有列表视图,您想要片段中适配器的详细信息?在这种情况下,您可以使用接口作为片段的回调 查看***.com/questions/40786356/… 使用回调(您可以将其传递给构造函数)将数据传回您的 Activity,然后处理从那里切换片段的逻辑。 是的...我在片段中的列表视图 举个例子 【参考方案1】:使用下面的代码去片段中的listview适配器项点击监听
relaylist.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id)
HashMap<String, String> o = (HashMap<String, String>)
relaylist.getItemAtPosition(position);
Log.e("ip: ", "> " + o.get("ip"));
Log.e("port: ", "> " + o.get("port"));
Log.e("uname: ", "> " + o.get("uname"));
Log.e("password: ", "> " + o.get("password"));
ControlFragment fragment = new ControlFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
Log.e("ip: ", "> " + o.get("ip"));
Log.e("port: ", "> " + o.get("port"));
Log.e("uname: ", "> " + o.get("uname"));
Log.e("password: ", "> " + o.get("password"));
transaction.replace(R.id.mainFrame, fragment );
Bundle bundle = new Bundle();
bundle.putString("ip", o.get("ip"));
bundle.putString("port", o.get("port"));
bundle.putString("uname", o.get("uname"));
bundle.putString("password", o.get("password"));
fragment.setArguments(bundle);
transaction.commit();
);
【讨论】:
【参考方案2】:试试这个
((Activity) mContext).getFragmentManager();
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Fragment fragment = new CallThisFragment();
FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.main_activity, fragment).commit();
);
并使用 bundle 将数据传递给片段类 How to pass Bundle from Fragment to Fragment
【讨论】:
在第一行给我错误.. Fragment fragment = new ControlFragment(); FragmentManager fragmentManager = ((Activity) context).getFragmentManager();给我类似不兼容类型的错误..required: android.app.fragment found: com.example.lenovo.controlapp.controlframgent fragmentManager.beginTransaction().replace(R.id.mainFrame, fragment).commit(); 请更改您的片段导入,无论是 android.app.Fragment 还是 android.support.v4.app.Fragment ***.com/questions/15109017/…以上是关于如何在android中去listview适配器项目点击监听器片段?的主要内容,如果未能解决你的问题,请参考以下文章
如何修复动态适配器包装器 Android 中的 notifyDataSetChanged/ListView 问题
Android:如何从 listView 和 arrayAdapter 中删除项目