单击listview项时在android中调用特定活动

时间:2014-06-10 23:53:09

标签: android eclipse

我想在点击列表项时调用特定的Activity。在我的ListView click事件处理程序中使用if语句或case并使用String fclass_state变量,我有4个活动要调用。我该怎么做呢?

public class OutletsList  extends ListActivity{

// Progress Dialog
        private ProgressDialog pDialog;


        // testing on Emulator:
        private static final String READ_COMMENTS_URL = "myurl";



        // JSON IDS:
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_OUTLET_NAME = "outlet_name";
        private static final String TAG_POSTS = "posts";
        private static final String TAG_SPARKLING_CLASSIFICATION =     "sparkling_classification";
        private static final String TAG_SPARKLING_CHANNEL = "sparkling_channel";
        private static final String TAG_CLASS = "class";


        // An array of all of our comments
        private JSONArray mOutlets = null;
        // manages all of our comments in a list.
        private ArrayList<HashMap<String, String>> mOutletsList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.outlets_list);


}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    // loading the comments via AsyncTask
    new LoadMathQuestions().execute();
}

/*  public void addComment(View v) {
    Intent i = new Intent(ReadComments.this, AddComment.class);
    startActivity(i);
}
*/
/**
 * Retrieves recent post data from the server.
 */
public void updateJSONdata() {

    // Instantiate the arraylist to contain all the JSON data.
    // we are going to use a bunch of key-value pairs, referring
    // to the json element name, and the content.

    mOutletsList = new ArrayList<HashMap<String, String>>();

    // Instantiating the json parser J parser
    JSONParser jParser = new JSONParser();
    // Feed the beast our comments url, and it spits us
    // back a JSON object. Boo-yeah Jerome.
    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

    //Catcing Exceptions
    try {
        //Checking the amount of data rows.
        mOutlets = json.getJSONArray(TAG_POSTS);

        // looping through the database
        for (int i = 0; i < mOutlets.length(); i++) {
            JSONObject c = mOutlets.getJSONObject(i);

            // gets the content of each tag
            String outlet = c.getString(TAG_OUTLET_NAME);
            String schannel = c.getString(TAG_SPARKLING_CHANNEL);
            String spclassification =    c.getString(TAG_SPARKLING_CLASSIFICATION);
            String cls = c.getString(TAG_CLASS);



            HashMap<String, String> map = new HashMap<String,  String>();

            map.put(TAG_OUTLET_NAME, outlet );
            map.put(TAG_SPARKLING_CHANNEL, schannel);
            map.put(TAG_SPARKLING_CLASSIFICATION, spclassification);
            map.put(TAG_CLASS, cls);


            // adding HashList to ArrayList
            mOutletsList.add(map);

            // JSON data parsing completed by hash mappings
            // list
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

/**
 * Inserts the parsed data into the listview.
 */
private void updateList() {
    // For a ListActivity we need to set the List Adapter, and in order to do
    //that, we need to create a ListAdapter.  This SimpleAdapter,
    //will utilize our updated Hashmapped ArrayList, 
    //use our single_post xml template for each item in our list,
    //and place the appropriate info from the list to the
    //correct GUI id.  Order is important here.
    ListAdapter adapter = new SimpleAdapter(this, mOutletsList,
            R.layout.single_outlet, new String[] { TAG_OUTLET_NAME,   TAG_SPARKLING_CHANNEL, 

TAG_SPARKLING_CLASSIFICATION, TAG_CLASS}, new int[]
                    { R.id.outlet_name,  R.id.sparkling_channel, R.id.sparkling_classification, 

R.id.cls_state});

    // I shouldn't have to comment on this one:
    setListAdapter(adapter);

    // Optional: when the user clicks a list item we 
    //could do something.  However, we will choose
    //to do nothing...
    final ListView lv = getListView();  
    lv.setOnItemClickListener(new OnItemClickListener() {

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


            HashMap<String, String>map = (HashMap<String, String>)parent.getItemAtPosition(position);


            String foutname = map.get(TAG_OUTLET_NAME);
            String fchannel = map.get(TAG_SPARKLING_CHANNEL);
            String fclass = map.get(TAG_SPARKLING_CLASSIFICATION);
            String fclass_state = map.get(TAG_CLASS);



            Intent i = new Intent(OutletsList.this, GdgScoreSheeet.class);
            i.putExtra("outlt", foutname);
            i.putExtra("chnl", fchannel);
            i.putExtra("cls", fclass);
            i.putExtra("clsstate", fclass_state);
            startActivity(i);


    });
}

public class LoadMathQuestions extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(OutletsList.this);
        pDialog.setMessage("Loading outlets please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        updateJSONdata();
        return null;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        updateList();
    }
}
}

2 个答案:

答案 0 :(得分:0)

在你的onClick方法中:

switch(position) {
    // first list item selected
    case 0:    
        Intent i = new Intent(OutletsList.this, GdgScoreSheeet.class);
        i.putExtra("outlt", foutname);
        i.putExtra("chnl", fchannel);
        i.putExtra("cls", fclass);
        i.putExtra("clsstate", fclass_state);
        startActivity(i);
        break;
    // second list item selected
    case 1:
        ...
}

答案 1 :(得分:0)

使用此代码作为示例来替换创建Intent的位置:

Intent i = new Intent();
// Additional Extras

if(fclass_state.equals("GOLD")){
  i.setClass(OutletList.this, GoldActivity.class);
  // additional extras
} else if(fclass_state.equals("SILVER")){
  i.setClass(OutletList.this, SilverActivity.class);
  // additional extras
} else if(fclass_state.equals("BRONZE")){
  i.setClass(OutletList.this, BronzeActivity.class);
  // additional extras
} else {
  i.setClass(OutletList.this, UnassignedActivity.class);
  // additional extras
}