从所选的每个listView项开始一个新活动

时间:2015-09-28 12:06:24

标签: android listview android-fragments android-dialogfragment

我有一个对话框片段listView,在这个列表上是一个图像和一个textview 我想点击列表中的每个项目开始一项新活动!尝试了onItemClickListener,但似乎无法启动新活动,当前onItemClickListener在点击时显示吐司文本。

我尝试删除吐司并添加意图无效。

public class MainActivity extends FragmentActivity {

    Button showBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        final PlayersFragment p = new PlayersFragment();
        showBtn = (Button) findViewById(R.id.button1);
        showBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                showBtn.setVisibility(View.GONE);

                transaction.replace(R.id.fragment_container, p);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (showBtn != null) {
            showBtn.setVisibility(View.VISIBLE);
        }
        super.onBackPressed();
    }
}
public class PlayersFragment extends android.support.v4.app.DialogFragment {

    ListView lv;
    String[] players = {"image1", "image2", "image3", "image4", "image5", "image6", "image7",
            "image8", "image9", "image10", "image11", "image12", "image13", "image14"};

    int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10,
            R.drawable.image11, R.drawable.image12, R.drawable.image13, R.drawable.image14,};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog, container, false);

        //initialize listview
        lv = (ListView) rootView.findViewById(R.id.listView1);

        //create adapter obj and set list view to it
        Adapter adapter = new Adapter(getActivity(), players, images);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
                Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
            }
        });

        return rootView;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用onItemClicklistener作为列表视图:

    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
                if(pos == 0)
                {
                    Intent intent = new Intent(getActivity() , Activity2.class);
                    startActivity(intent);
                }
                else if(pos == 1)
                {
                    Intent intent = new Intent(getActivity() , Activity3.class);
                    startActivity(intent);
                } and so on .......
        }
    });

答案 1 :(得分:0)

阅读start:http://developer.android.com/reference/android/app/Activity.html#StartingActivities

的文档

然后在你的监听器中,通过PlayersFragment.this.getActivity()。startActivity()或类似的东西调用startActivity