1

I need your help to fix my code.

My intention is if i click the item on ListView it will take me to intent action_view.

The problem is i got force close if i click the item on ListView.

I think the problem is in onItemClick method, can you give the better way to get it done.

Here's my code:

public class HotelList extends ListActivity{
hotelHelper dbHotelHelper;
protected Cursor cursor;
protected ListAdapter adapter;
ListView numberList;    

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hotellist);

    numberList = (ListView)findViewById(android.R.id.list);
    numberList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
                    String selectedItem = (String) getListAdapter().getItem(position);
                    String query = "SELECT lat, long FROM hoteltbl WHERE name = '" + selectedItem + "'";
                    SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
                    Cursor result = dbs.rawQuery(query, null);
                    result.moveToFirst();

                    double lat = result.getDouble(result.getColumnIndex("lat"));
                    double longi = result.getDouble(result.getColumnIndex("long"));

                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat+","+longi));
                    startActivity(intent);

                }                   
            });     


    dbHotelHelper = new hotelHelper(this);
    try{
        dbHotelHelper.createDataBase();         
    }
    catch (Exception ioe){
        Log.e("err","Unable to create database");
    }        
    view();   


    }



private void view() {
    // TODO Auto-generated method stub
    SQLiteDatabase db = dbHotelHelper.getReadableDatabase();
    try{
        cursor = db.rawQuery("SELECT * FROM hoteltbl", null);
        adapter = new SimpleCursorAdapter(
                this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                new String[]{"name"}, 
                new int[] {android.R.id.text1}
                );
        numberList.setAdapter(adapter);     
    }
    catch (Exception e){
        Log.e("error",e.toString());
    }       
}

}

forgiven24
  • 49
  • 2
  • 10

1 Answers1

0

Inside item click you have many ways it might cause exception, Exception will cause your app force close your code is not handled with exceptions

I am listing down the causes it might cause the exception, please check followings

String selectedItem = (String) getListAdapter().getItem(position);

here you might get type conversation issue, you are converting something is not supported by String

SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
Cursor result = dbs.rawQuery(query, null);
result.moveToFirst();
double lat = result.getDouble(result.getColumnIndex("lat"));
double longi = result.getDouble(result.getColumnIndex("long"));
  1. If dbHotelHelper.getReadableDatabase returns null or dbHotelHelper null, in this case your app will force close
  2. In case dbs.rawQuery return null, your app will force close
  3. if result null then you might calling moveToFirst, getDouble and getColumnIndex on null object, in this case also your app will force close

For your info SimpleCursorAdapter constructor was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

Shaahul
  • 538
  • 4
  • 11