Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7777245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:05:25+00:00 2026-06-01T18:05:25+00:00

I’ve created this code to list pdf file under a specific folder (called pass)

  • 0

I’ve created this code to list pdf file under a specific folder (called “pass”) on sd card, and what I’m struggeling in is displaying a delete function from the context menu.I keep clicking on any file, but it doesn’t show any menu

The list code and the context menu 


   public class PDFListActivity extends ListActivity {
ArrayAdapter<String> adapter;
int clickCounter=0;
ArrayList<String> listItems=new ArrayList<String>();
private File[] imagelist;
String[] pdflist;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView lv=getListView(); 
        registerForContextMenu(lv);
        setContentView(R.layout.mainlistpdf);
      File images=new File(Environment.getExternalStorageDirectory(), "pass");
        imagelist = images.listFiles(new FilenameFilter(){  
                public boolean accept(File dir, String name)  
                {    

                        return ((name.endsWith(".pdf")));  
                }  
        });

        pdflist = new String[imagelist.length];
        for(int i = 0;i<imagelist.length;i++)
        {
                pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, pdflist));
}
@Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo); 
    ListView list=getListView(); 
    registerForContextMenu(list); 
        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "delete");  
        menu.add(0, v.getId(), 0, "wtever");  
    }  

    @Override  
    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="delete"){function1(item.getItemId());}  
        else if(item.getTitle()=="wtever"){function2(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    public void function1(int id){  
        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();  
        File folder = Environment.getExternalStorageDirectory();
        String fileName = folder.getPath() + "/pass/hello.pdf";

        File myFile = new File(fileName);
        if(myFile.exists())
            myFile.delete();

    }  
    public void function2(int id){  
        Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();  
    }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        PackageManager packageManager = getPackageManager();
         Intent testIntent = new Intent(Intent.ACTION_VIEW);
         testIntent.setType("application/pdf");
         List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
         if (list.size() > 0 && imagelist[(int) id].isFile()) {
             Intent intent = new Intent();
             intent.setAction(Intent.ACTION_VIEW);
             Uri uri = Uri.fromFile(imagelist[(int) id].getAbsoluteFile());
             intent.setDataAndType(uri, "application/pdf");
             startActivity(intent);
         }

}

    }  
        My xml file      
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:05:27+00:00Added an answer on June 1, 2026 at 6:05 pm

    I think you missed this function…

      ListView lv=getListView();
     registerForContextMenu(lv);<-- did you register your listview for context menu?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.