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 7501913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:41:32+00:00 2026-05-29T20:41:32+00:00

I’m trying to implement OnListItemClick but can’t seem to get it working. There are

  • 0

I’m trying to implement OnListItemClick but can’t seem to get it working. There are no errors coming up, no force closes etc. Below is the code I have:

package com.odhranlynch.filmCollection;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends Activity {
/** Called when the activity is first created. */
private final String databaseFilename = "/films.db"; //filename
private final String url = "www.somesite.com"; //download database from here
ArrayList<FilmRecord> films = new ArrayList<FilmRecord>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DownloadFromUrl(url, databaseFilename);

    ListView listView = (ListView) findViewById(R.id.lstFilms);
    listView.setAdapter(new FilmItemAdapter(this, android.R.layout.simple_list_item_1, films));
}


protected void onListItemClick(ListView l, View v, int position, long id)
{
    //ListItem clicked.
    Log.d("ListView", String.valueOf(l.getSelectedItemPosition()));
}

public void DownloadFromUrl(String DownloadUrl, String fileName) {  
    try {
        File root = android.os.Environment.getExternalStorageDirectory();               

        File dir = new File (root.getAbsolutePath() + "/Android/data/com.odhranlynch.filmcollection");
        if(dir.exists()==false) {
             dir.mkdirs();
        }

        URL url = new URL(DownloadUrl); //you can write here any link
        File file = new File(dir, fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager", "download begining");
        Log.d("DownloadManager", "download url:" + url);
        Log.d("DownloadManager", "downloaded file name:" + fileName);

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
           baf.append((byte) current);
        }


        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

        //Download completed, run populateFields method.
        populateFields();

} catch (IOException e) {
    Log.d("DownloadManager", "Error: " + e);
    }
}

public class FilmItemAdapter extends ArrayAdapter<FilmRecord> {
    private ArrayList<FilmRecord> films;

    public FilmItemAdapter(Context context, int textViewResourceId, ArrayList<FilmRecord> films) {
        super(context, textViewResourceId, films);
        this.films = films;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.listitem, null);
        }

        FilmRecord film = films.get(position);
        if (film != null) {
            TextView filmTitle = (TextView) v.findViewById(R.id.username);
            TextView filmShowingDate = (TextView) v.findViewById(R.id.email);

            if (filmTitle != null) {
                filmTitle.setText(film.filmTitle);
            }

            if(filmShowingDate != null) {
                filmShowingDate.setText("Showing on " + film.filmShowingDate );
            }
        }
        return v;
    }
}

public class FilmRecord {
    public String filmTitle;
    public String filmShowingDate;

    public FilmRecord(String username, String email) {
        this.filmTitle = username;
        this.filmShowingDate = email;
    }
}

public void populateFields() {
    //Populate the views with database content.


    DataBaseHelper myDbHelper = new DataBaseHelper(this);;
    myDbHelper = new DataBaseHelper(this);

    //Load "productDatabase" database (see assets to left).
    {
        try {
            myDbHelper.createDataBase();} 
        catch (IOException ioe) {
            throw new Error("Unable to create database");}

        try {
            myDbHelper.openDataBase();}
        catch(SQLException sqle){
            throw sqle;}
    }

  //---Get all records from database---
    myDbHelper.openDataBase();
    Cursor cursorPosition = myDbHelper.getAllTitles();


    if (cursorPosition.moveToFirst())
    {
    do {
    AddRecordToArray(cursorPosition);
    } while (cursorPosition.moveToNext());
    }
    myDbHelper.close();
    }

    public void AddRecordToArray(Cursor cursorPosition)
    {
        //Add new record to array (which gets data from database using cursorPosition (for current row) and .getstring( for column number).
        FilmRecord film1 = new FilmRecord(cursorPosition.getString(1), cursorPosition.getString(4));
        films.add(film1);
    }
}

I would be really appreciative if someone would be kind enough to help me out on this one 🙂

  • 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-05-29T20:41:35+00:00Added an answer on May 29, 2026 at 8:41 pm

    You are not setting the listener to the list. You need something like this:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DownloadFromUrl(url, databaseFilename);
    
        ListView listView = (ListView) findViewById(R.id.lstFilms);
        listView.setAdapter(new FilmItemAdapter(this, android.R.layout.simple_list_item_1, films));
        listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
                    Log.d("ListView", String.valueOf(pos));
                }
        });
    }
    

    You may also want to add some methods to your adapter implementation to get access to a specific element of your array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.