2013-05-28 3 views
0

Я работаю над приложением. В действии активность отображает содержимое с сервера и отображает его в виде списка. Я расширяю SherlockActivity. Но контекстное меню не появляется. код идет как этотКонтекстное меню не загружается в SherlockActivity

NewsActivity.java

package com.ministry.ensing119app.news; 

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

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.View; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

import com.actionbarsherlock.app.ActionBar; 
import com.actionbarsherlock.app.SherlockActivity; 
import com.actionbarsherlock.view.MenuInflater; 
import com.actionbarsherlock.view.MenuItem; 
import com.ministry.ensing119app.Contact; 
import com.ministry.ensing119app.HomeScreen; 
import com.ministry.ensing119app.R; 
import com.ministry.ensing119app.bible.BibleActivity; 
import com.ministry.ensing119app.donate.Donate; 
import com.ministry.ensing119app.photos.GetPath; 
import com.ministry.ensing119app.sermonnotes.Notes_list; 

public class NewsActivity extends SherlockActivity { 

public static String url = "http://ensignweb.com/sandbox/app/comment11.php"; 

    // JSON Node names 
    protected static final String TAG_PRODUCTS = "products"; 
    protected static final String TAG_CID = "cid"; 
    public static final String TAG_NAME = "name"; 
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

    // contacts JSONArray 
    JSONArray products = null; 
    ActionBar actionBar; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     actionBar = getSupportActionBar(); 
     setContentView(R.layout.activity_news); 

     ConnectivityManager connectivityManager 
     = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { 
      Log.d("if condition", "if condition is running"); 
      new Message().execute(url); 


//   The service section 
      Intent intent = new Intent(this,UpdateService.class); 
      PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0); 
      AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, pIntent); 
      startService(new Intent(this, UpdateService.class)); 
     } else { 
      Log.d("if condition", "else condition is running"); 
     Toast.makeText(getApplicationContext(), "There is no internet or low. Please check", Toast.LENGTH_LONG).show(); 
     Intent returnIntent = new Intent(NewsActivity.this, HomeScreen.class); 
     startActivity(returnIntent); 
     }   
    } 

    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.news, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onMenuItemSelected(int featureId, MenuItem item) { 
     // TODO Auto-generated method stub 
     switch(item.getItemId()){ 
     case R.id.donate: 
      Intent newsIntent = new Intent(NewsActivity.this, Donate.class); 
      startActivity(newsIntent); 
      break; 

     case R.id.photos: 
      Intent photoIntent = new Intent(NewsActivity.this, GetPath.class); 
      startActivity(photoIntent); 
      break; 

     case R.id.notepad: 
      Intent notePadIntent = new Intent(NewsActivity.this, Notes_list.class); 
      startActivity(notePadIntent); 
      break; 

     case R.id.contact: 
      Intent contactIntent = new Intent(NewsActivity.this,Contact.class); 
      startActivity(contactIntent); 
      break; 

     case R.id.bible: 
      Intent BibleIntent = new Intent(NewsActivity.this, BibleActivity.class); 
      startActivity(BibleIntent); 
      break; 
     } 
     return super.onMenuItemSelected(featureId, item); 
    } 

    class Message extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > { 

     ListView lv = (ListView) findViewById(android.R.id.list); 
     ProgressDialog progress = new ProgressDialog(NewsActivity.this); 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 
       @Override 
      protected ArrayList<HashMap<String, String>> doInBackground(String... params) { 

        Log.d("doInBackgound","backgound is running"); 

        // getting JSON string from URL 
        JSONObject json = jParser.getJSONFromUrl(url); 

        Log.d("path_parsing", "before parsing"); 
        try { 
         // Getting Array of Contacts 
         products = json.getJSONArray(TAG_PRODUCTS); 

         // looping through All Contacts 
         for(int i = products.length()-1; i >=0; i--){ 
          JSONObject c = products.getJSONObject(i); 

          // Storing each json item in variable 
          String cid = c.getString(TAG_CID).toString(); 
          String name = c.getString(TAG_NAME); 
          // creating new HashMap 
          HashMap<String, String> map = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 
          map.put(TAG_CID, cid); 
          map.put(TAG_NAME, name); 

          // adding HashList to ArrayList 
          mylist.add(map); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        Log.d("path_parsing", "after parsing"); 
        return mylist; 

       } 

      @Override 
      protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
       if(progress.isShowing()){ 
        progress.dismiss(); 
       } 
       ListAdapter adapter = new SimpleAdapter(NewsActivity.this, result , R.layout.list_item,new String[] { TAG_NAME,}, new int[] { 
         R.id.name}); 
       lv.setAdapter(adapter); 
       lv.setTextFilterEnabled(true); 
       Log.d("postExecute","Postexecute is running"); 

      } 

      @Override 
      protected void onProgressUpdate(Integer... values) { 
       // TODO Auto-generated method stub 
       super.onProgressUpdate(values); 
      } 

      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
       super.onPreExecute(); 
       progress.setTitle("Progress"); 
       progress.setMessage("Please have patience"); 
       progress.show(); 
      } 
     } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     // TODO Auto-generated method stub 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.add(0, v.getId(), 0, "Share"); 
    } 


} 

Пожалуйста, помогите мне в этом. Я новичок в андроиде.

+0

Измените 'MenuItem' на' com.actionbarsherlock.view.MenuItem' 'onMenuItemSelected()' –

ответ

0

В ваших menu.xml элементах должно быть установлено значение showAsAction, если вы хотите их в панели действий. Обратите внимание: если у вас слишком много предметов, не все будут видны. Те, которые не видны, находятся в меню переполнения или в меню под кнопкой физического меню на вашем телефоне.

0

вы have't использовали это в коде: registerForContextMenu(yourView);

здесь simple example как использовать ContextMenu в андроиде.

Смежные вопросы