如何在Fragment中调用活动方法?

时间:2017-01-19 15:36:01

标签: java android listview

我有以下listView:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //CheckBoxAdapter cb = new CheckBoxAdapter(this.getActivity(),R.layout.item, getResources().getStringArray(R.array.products));
    //lv.setAdapter(cb);
    ArrayAdapter<String> arrayAdapter;
    arrayAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, categories);

    ListView lv = (ListView) this.getActivity().findViewById(R.id.listProduct);
    lv.setAdapter(arrayAdapter);

    lv.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String type = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(getContext(), parent.getItemAtPosition(position)+" is selected", Toast.LENGTH_LONG).show();
                    try {
                        MainActivity.AddSubscription(String.valueOf(parent.getItemAtPosition(position)));
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    );

但目前我不能引用非静态的addSubscription

MainActivity.AddSubscription(String.valueOf(parent.getItemAtPosition(position)));

我需要创建一个非静态侦听器,它可以调用addsubscription,是否可以使用listview?如果没有,我可以使用其他类型的视图吗?

3 个答案:

答案 0 :(得分:0)

如果您的片段放在MainActivity中,请执行以下操作:

((MainActivity)getActivity()).AddSubscription(String.valueOf(parent.getItemAtPosition(position)));

答案 1 :(得分:0)

我不会使用getActivity()并施放它,因为这种方法对我来说似乎很脆弱。

我在没有IDE的情况下输入了这个,所以可能存在拼写错误。 让Activity实现接口SubscriptionHandler

public interface SubscriptionHandler {
    public void addSubscription(String identifier);
}

Actitiy

public MainActivity extends AppCompatActivity implements SubscriptionHandler {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_id);
        fragment.setSubscriptionHandler(this);
   }

   @Override
   public void addSubscription(String identifier){
   //logic
   }

}

然后在Fragment中使用它

 public class MyFragment extends Fragment {

    @Nullable private SubscriptionHandler subscriptionHandler;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter<String> arrayAdapter;
        arrayAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, categories);

        ListView lv = (ListView) this.getActivity().findViewById(R.id.listProduct);
        lv.setAdapter(arrayAdapter);

        lv.setOnItemClickListener(
            new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String type = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(getContext(), parent.getItemAtPosition(position)+" is selected", Toast.LENGTH_LONG).show();
                    try {
                        if(subscriptionManager != null) {
                            subscriptionHandler.addSubscription(String.valueOf(parent.getItemAtPosition(position)));
                        }
                    } catch (TimeoutException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
           }
     );

     public void setSubscriptionHandler (SubscriptionHandler subscriptionHandler) {
        this.subscriptionHandler = subscriptionHandler;
     }
 } 

答案 2 :(得分:0)

在监听器中执行以下操作:

YourFragmentName.this.((MainActivity)getActivity()).AddSubscription(String.valueOf(parent.getItemAtPosition(position)));