gpt4 book ai didi

java - 如何去 listview 适配器项目单击监听器到 android 中的 fragment ?

转载 作者:太空狗 更新时间:2023-10-29 13:07:19 24 4
gpt4 key购买 nike

我想从 ListView 适配器 itemview 单击监听器转到带有额外详细信息的 fragment ,我想在我去的那个 fragment 中使用这些细节。我使用这个 ListView 适配器类。我想从 listview 适配器 itemview 单击监听器转到带有额外细节的 fragment ,我想在我去的那个 fragment 中使用。我使用这个 ListView 适配器类。

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

所以我怎样才能去 fragment 而不是 Activity 。我想把这个放在 fragment 中,把额外的细节放在一起。那么,我怎么走?

最佳答案

在fragment中使用下面的代码去listview adapter item click listener

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

关于java - 如何去 listview 适配器项目单击监听器到 android 中的 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110833/

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