0

我正在使用 Sherlock 片段制作动作栏。我在操作栏中有 ListView,但我的 listview 可以调用活动..请帮助我谢谢.. :)

在这段代码中有一个使用数组列表的列表视图。我想 mylist 可以调用相同的活动。

所以它可以喜欢操作栏中的菜单。当我们点击列表视图时..他可以打电话给活动谢谢..请帮助我!

请更正我的代码..如何使用列表视图中的每个选项调用活动..谢谢..

AppleFragment.java

package iqbal.apps.visitkuningan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.actionbarsherlock.app.SherlockListFragment;

public class AppleFragment extends SherlockListFragment{

    /** An array of items to display*/
    String apple_versions[] = new String[]{
            "Mountain Lion",
            "Lion",
            "Snow Leopard",
            "Leopard",
            "Tiger",
            "Panther"
    };

    /** An array of images to display*/
    int apple_images[] = new int[]{
            R.drawable.mountainlion,
            R.drawable.lion,
            R.drawable.snowleopard,
            R.drawable.leopard,
            R.drawable.tiger,
            R.drawable.panther
    };

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

        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<5;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", apple_versions[i]);
            hm.put("img", Integer.toString(apple_images[i]  ) );
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "img","txt" };

        // Ids of views in listview_layout
        int[] to = { R.id.img,R.id.txt};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);

     // Getting a reference to listview of main.xml layout file
        ListView listview = AppleFragment.this.getListView();

        listview.setAdapter(adapter);

        // Setting the adapter to the listView
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, 
                                  View view, int position,long id) {
              // ?? please add code in here to call activity from each option in listview.. T_T or correct my code

              }

            });
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}
4

1 回答 1

0

编辑 setOnItemClickListener 并插入一个条件,根据单击的项目检查项目的位置以及开始和活动。

应该看起来像这样

listview.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) {
      Intent n = null; 
      switch (position){
         case 0: 
           n = new Intent(getActivity(), Class0.class);
           break;
         case 1: 
           n = new Intent(getActivity(), Class1.class);
           break;
      }
      if(null!=n)
          startActivity(n);
  }
}); 
于 2013-03-16T08:50:26.510 回答