从一个片段移动到另一个活动使用的ListView(Moving from One Fragment

2019-11-04 03:44发布

我使用的导航抽屉,从片段移动到另一个上时,在它的MENU_ITEM点击。 在我的片段之一,我有一个列表视图。 我要搬到另一个活动和这里的列表视图项的一个点击时既是活动的事情以及片段的代码不片段。 我已经使用意图但仍给出意向的参数错误。

“一个”片段包com.navigate2;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


 /**
  * A simple {@link Fragment} subclass.
  */
  public class One extends Fragment {


  public One() {
      // Required empty public constructor
  }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);

    ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
    listView.setBackgroundColor(Color.WHITE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position)
            {
                case 0:
                    Intent intent = new Intent(One.this,AndroidClass.class);
                    startActivity(intent);
            }
        }
    });
}


}

“AndroidClass”活动

   package com.navigate2;

  import android.os.Bundle;
  import android.os.PersistableBundle;
  import android.support.v7.app.AppCompatActivity;

  /**
  * Created by Nathani Aliakbar on 06-01-2016.
  */
  public class AndroidClass extends AppCompatActivity
  {
   @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.textview);
  }
  }

Answer 1:

改变onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_one, container, false);

ListView listView = (ListView)   rootview.findViewById(R.id.mobile_list2);

listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position)
        {
            case 0:
                Intent intent = new Intent(One.this,AndroidClass.class);
                startActivity(intent);
        }
    }
});

 return rootview;
}


Answer 2:

里面onCreateView要添加onItemCliclListener之前立即返回片段:

return inflater.inflate(R.layout.fragment_one, container, false);

尝试这样的:

View view = inflater.inflate(R.layout.fragment_one, container, false);
ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position)
        {
            case 0:
                Intent intent = new Intent(One.this,AndroidClass.class);
                startActivity(intent);
        }
    }
});
return view;


Answer 3:

采用

  View view = inflater.inflate(R.layout.fragment_one, container, false);
  ListView listView = (ListView)listView.findViewById(R.id.mobile_list2);
 Intent intent = new Intent(getActivity(),AndroidClass.class);
                    startActivity(intent);


return view;


Answer 4:

View view = inflater.inflate(R.layout.fragment_one, container, false);
ListView listView = (ListView)view.findViewById(R.id.mobile_list2);
listView.setBackgroundColor(Color.WHITE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch (position)
    {
        case 0:
            Intent intent = new Intent(One.this,AndroidClass.class);
            startActivity(intent);
    }
}
});
return view;


文章来源: Moving from One Fragment to another activity Using ListView