使用ListView从一个片段移动到另一个活动

时间:2016-01-09 08:40:32

标签: android listview android-fragments android-intent

当我点击menu_item时,我正在使用导航抽屉从片段移动到另一个。 在我的一个片段中,我有一个列表视图。 当点击其中一个列表视图项时,我必须移动到另一个ACTIVITY而不是FRAGMENT 这里是Activity以及片段的代码。 我已经使用了intent但仍然在意图参数上给出了错误。

“一个”片段    包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);
  }
  }

4 个答案:

答案 0 :(得分:1)

在片段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;

答案 1 :(得分: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;
}

答案 2 :(得分:0)

使用

  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;

答案 3 :(得分:0)

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;