0

我正在开发一个应用程序。在活动中,活动列出来自服务器的内容并将其显示在列表视图中。我正在扩展 SherlockActivity。但是上下文菜单没有出现。代码是这样的

新闻活动.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");
    }


}

请帮助我。我是安卓新手。

4

2 回答 2

0

如果您希望它们在操作栏中,则在您的menu.xml项目中应该设置showAsAction属性。请注意,如果您有太多项目,则并非所有项目都可见。那些不可见的在溢出菜单或手机物理菜单按钮下的菜单中。

于 2013-05-28T08:21:56.880 回答
0

你没有在你的代码中使用它:registerForContextMenu(yourView);

这是如何在android中使用的简单示例。ContextMenu

于 2013-05-28T09:08:19.970 回答