将片段中的ListView链接到不同的活动

时间:2013-03-15 03:14:56

标签: java android listview fragment

我在片段中创建了一个ListView,但我似乎无法将ListView的选择链接到不同的活动。希望有人可以帮助我解决这个问题,当它不在片段内时,我能够执行相同的操作。

下面的代码显示了与其他活动链接的常规ListView:

public class Main extends Activity {

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

    ListView listView2 = (ListView) findViewById(R.id.listView2);
    String[] values = new String[] { "Apple", "Orange", "Banana", "Pineapple", "Kiwi"};


    ArrayAdapter <String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView2.setAdapter(adapter2);

            listView2.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch( position )
                {
                   case 0:  Intent newActivity = new Intent(Main.this, FirstChoice.class);     
                            startActivityForResult(newActivity, 0);
                            break;
                   case 1:  Intent newActivity1 = new Intent(Main.this, Second Choice.class);     
                            startActivityForResult(newActivity1, 1);
                            break;

                            .
                            .
                            .

以下是我目前的片段代码:

public class Tab1 extends ListFragment{

ListView list;
ArrayAdapter <String> adapter;
Context context;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        context = getActivity();
        View view;
        view = inflater.inflate(R.layout.activity_firstchoice, null);

        String [] elements = context.getResources().getStringArray(R.array.thelist);

        list = (ListView) view.findViewById(R.id.listView1);
        adapter = new ArrayAdapter <String> (context, R.layout.activity_ecfs, R.id.section_label, elements);

        list.setAdapter(adapter);

        return view;
}

public void onListItemClick(ListView l, View v, int position, long id) {
    // Do something with the data
    switch( position )
    {
       case 0:  Intent newActivity = new Intent();     
                startActivityForResult(newActivity, 0);
                break;
       case 1:  Intent newActivity1 = new Intent();     
                startActivityForResult(newActivity1, 1);
                break;
       case 2:  Context context2 = getApplication();
                CharSequence text2 = "Not Available";
                int duration2 = Toast.LENGTH_SHORT;

                Toast toast2 = Toast.makeText(context2, text2, duration2);
                toast2.show();
                break;
    }

  }
}

我希望做一些类似于顶级ListView的东西,但是片段里面有一个。

1 个答案:

答案 0 :(得分:0)

您只需获取对Fragment的父Activity的引用。

    case 0:  
        Intent newActivity = new Intent(getActivity(), FirstChoice.class);     
        startActivityForResult(newActivity, 0);
        break;

或者使用传入的ListViewView从中获取Context

    case 0:  
        Intent newActivity = new Intent(l.getContext(), FirstChoice.class);     
        startActivityForResult(newActivity, 0);
        break;