2

我在片段内创建了一个 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 的事情,但在片段内。

4

1 回答 1

0

您可以只获取对Fragment父级的引用Activity

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

或使用传入ListView或从他们View那里获取Context

    case 0:  
        Intent newActivity = new Intent(l.getContext(), FirstChoice.class);     
        startActivityForResult(newActivity, 0);
        break;
于 2013-03-15T03:42:32.467 回答