7

Alright so i have a list(which is also a fragment dialog) that displays a users friends and each item in that list has a button(labeled friends in the picture) and when users click that button id like to display another fragment dialog that displays all the options for interacting with that user(friend request, block, send private message ect...) the problem is that this button and its onClick listener are currently implemented via overriding my listview adapters getView method and to create a fragmentDialog requires access to fragment manager. is there a way to make this work?

EDIT: I cannot post actual code from the project , but ive attached a simplified base adapter w. onClickListener that should make it clear what im trying to do . I cannot access the fragmentManager from a base adapter class to make the dialog fragment possible

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");





  }

    }
}

listview fragment dialog getView android

4

3 回答 3

21

我还努力从适配器访问片段管理器以显示 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");
      }
}

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

pagerAdapter = new YourPagerAdapter(getActivity());
yourViewPager.setAdapter(pagerAdapter);
于 2014-04-09T18:46:57.703 回答
1

我试图从位于自定义列表视图中的按钮打开一个对话框片段,用于显示事件列表的应用程序。我通过更改适配器构造函数解决了这个问题。

在我的适配器类中:

public class EventAdapter extends BaseAdapter {

private Context context;
private ArrayList <Event> mEvents;
private FragmentManager fm;

public EventAdapter(Context context, ArrayList<Event> events, FragmentManager fm) {
    this.context = context;
    this.fm = fm;
    mEvents = events;
}

然后我能够做到这一点:

        holder.saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SignupFragment signupFragment = new SignupFragment();
            signupFragment.show(fm, "Sample Fragment");
            Log.i(TAG, "click on save button");
        }
    });

我在更高的活动(AllEventsActivity)中更改了我的适配器调用:

    FragmentManager fm = getFragmentManager();
    mAdapter = new EventAdapter(this, mEvents, fm);
于 2016-07-11T22:39:04.237 回答
0

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

于 2013-08-26T04:44:44.910 回答