从自定义基本适配器单击按钮启动对话框片段> getView [IMG INCLUDED]

Chu*_*lly 7 android listview button baseadapter android-dialogfragment

好吧,所以我有一个列表(也是一个片段对话框),显示一个用户朋友,该列表中的每个项目都有一个按钮(在图片中标记为朋友),当用户点击该按钮ID时,显示另一个显示的片段对话框与该用户交互的所有选项(朋友请求,阻止,发送私人消息等)问题是这个按钮及其onClick监听器目前是通过覆盖我的listview适配器getView方法实现的,并创建一个fragmentDialog需要访问权限片段管理器.有没有办法使这项工作?

编辑:我不能发布项目的实际代码,但我已经附加了一个简化的基本适配器w.onClickListener应该清楚我想要做什么.我无法从基本适配器类访问fragmentManager以使对话框片段成为可能

LazyAdapter.java
package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);
         Button requestBtn = (Button)vi.findViewById(R.id.title); // title

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));
        artist.setText(song.get(CustomizedListView.KEY_ARTIST));
        duration.setText(song.get(CustomizedListView.KEY_DURATION));
        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);

        requestBtn.setOnClickListener(new myOnClickListener(position));
        return vi;
    }

        public class myOnClickListener implements OnClickListener{
     private int position;
  private String clicked_uid;
  public myOnClickListener(int position){
      this.position=position;
     }
  @Override
  public void onClick(View v) {


    //THIS IS WHERE IM TRYING TO PUT THE FRAGMENT DIALOG

        FragmentManager fm = TabHostFragmentActivity.this.getSupportFragmentManager();
        FriendsFamiliarsDialog friendsDialog = new FriendsFamiliarsDialog().newInstance(TabHostFragmentActivity.profile_uid,"friends");
        friendsDialog.show(fm, "friendsdialog");





  }

    }
}
Run Code Online (Sandbox Code Playgroud)

listview片段对话框getView android

小智 18

我也很难从适配器访问Fragment Manager以显示DialogFragment.这是代码中的解决方案:

public class YourPagerAdapter extends PagerAdapter{

   private Context context;

   public YourPagerAdapter(Context c) {
          this.context = c;
   }

   @Override
      public void onClick(View v) {
            FragmentActivity activity = (FragmentActivity)(context);
            FragmentManager fm = activity.getSupportFragmentManager();
            YourDialogFragment alertDialog = new YourDialogFragment();
            alertDialog.show(fm, "fragment_alert");
      }
}
Run Code Online (Sandbox Code Playgroud)

诀窍在于您将活动作为上下文接收,并且您只需将其投射以确保它是FragmentActivity - 这是管理者需要的.这是有效的,因为当您在更高级别的文件中调用YourPagerAdapter时,您将活动传递给它:

pagerAdapter = new YourPagerAdapter(getActivity());
yourViewPager.setAdapter(pagerAdapter);
Run Code Online (Sandbox Code Playgroud)


Pul*_*thi 0

当您创建 LazyAdapter 传递到为其创建适配器的视图或父片段或 ACtivity 中,然后在 onclick 方法中使用它来调用片段或活动上的函数以显示对话框片段时,请记住,从适配器类中,您应该只通过函数调用向 UI 组件发送消息以执行一些有趣的操作,例如显示对话框片段