Reputation: 95
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 :)
Upvotes: 2
Views: 3628
Reputation: 1546
The problem is that you are extending from Activity instead Of ListActivity.. If you want to extend from Activity maybe you could change you code.
public class Main extends Activity implements OnItemClickListener
and add on onCreate
listView.setOnItemClickListener(this);
Upvotes: 1
Reputation: 10619
use this code at the end of your onCreate
function :
listView.setOnItemClickListener(new ClickOnList());
use this class for ClickOnList
class ClickOnList implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("ListView", "Position"+arg2);
}
}
Upvotes: 1
Reputation: 57306
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.
Upvotes: 2