我想从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;
}
所以我怎么能去片段而不是活动。 我想在片段中加入额外的细节。那么,我怎么走?
答案 0 :(得分:1)
试试这个
((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
答案 1 :(得分: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();
}
});