使用ListView从一个片段移动到另一个活动 [英] Moving from One Fragment to another activity Using ListView

查看:72
本文介绍了使用ListView从一个片段移动到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航抽屉,当单击其中的menu_item时,它会从片段移动到另一个. 在我的片段之一中,我有一个列表视图. 单击列表视图项之一时,我必须移至另一个活动而不是片段 这是Activity和fragment的代码. 我已经使用了intent,但仍然在intent的参数上给出了错误.

I am using navigation drawer which moves from fragment to another when on clicked on the menu_item in it. In one of my fragments i have a listview. I have to move to another ACTIVITY and not FRAGMENT when clicked on one of the list view item here is the code of both the things the Activity as well as fragment. I have used intent but still it gives error on the parameters of intent.

一个"片段 包com.navigate2;

"One" Fragment package 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"活动

"AndroidClass" Activity

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

推荐答案

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

这篇关于使用ListView从一个片段移动到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆