如何去列表视图适配器项单击侦听器在Android中的片段?

问题描述:

我想从列表视图适配器项目视图单击侦听器片段与额外的细节,我想在该片段中使用我去..我使用此列表视图适配器类。我想从列表视图适配器itemview点击侦听器片段带有额外的细节,我想在该片段中使用我去..我使用此列表视图适配器类。如何去列表视图适配器项单击侦听器在Android中的片段?

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

所以我如何去片段而不是活动。 我想在这里放入额外的细节。所以,我怎么去?

+4

你的片段具有listview,你想从适配器的细节片段?在这种情况下,您可以使用接口作为您的片段的回调 – Raghunandan

+1

检查此https://*.com/questions/40786356/passing-argument-from-a-fragment-to-its-container-activity/40787805#40787805 – Raghavendra

+0

使用回调函数(您可以传递给构造函数)将数据传回给Activity,然后处理逻辑从那里切换片段。 – MatPag

使用下面的代码去列表视图适配器项目点击监听片段

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

试试这个

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

和使用捆绑将数据传递给片段类 How to pass Bundle from Fragment to Fragment

+0

给我错误的第一行.. Fragment fragment = new ControlFragment(); FragmentManager fragmentManager =((Activity)context).getFragmentManager();给我错误像不兼容的类型..需要:android.app.fragment找到:com.example.lenovo.controlapp.controlframgent fragmentManager.beginTransaction()。replace(R.id.mainFrame,fragment).commit(); –

+0

请更改您的片段导入无论其android.app.Fragment或android.support.v4.app.Fragment –

+0

https://*.com/questions/15109017/difference-between-android-app-fragment-and-android-支持-V4-APP-片段 –