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

时间:2013-08-26 04:16:16

标签: 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");





  }

    }
}

listview fragment dialog getView android

3 个答案:

答案 0 :(得分:14)

我还努力从适配器访问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");
      }
}

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

pagerAdapter = new YourPagerAdapter(getActivity());
yourViewPager.setAdapter(pagerAdapter);

答案 1 :(得分:0)

在创建适配器的视图中创建LazyAdapter传递或者父片段或ACtivity,然后在onclick方法中使用它来调用片段或活动上的函数来显示对话框片段,请记住来自适配器类你应该只通过函数调用向UI组件发送消息来做一些有趣的事情,比如显示对话框片段

答案 2 :(得分:0)

I was trying to open a dialogfragment from a button located in a custom listview for an app that shows a list of events. I solved this by changing my adapter constructor.

In my adapter class:

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

then I was able to do this:

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

And i changed my adapter call in my higher up activity (AllEventsActivity):

    FragmentManager fm = getFragmentManager();
    mAdapter = new EventAdapter(this, mEvents, fm);