Question

I have the damnest problem. i have created an onclick listener in my activity. It is suppose to delete a listview item from my listview. But it does nothing when clicked. The app runs with no errors in logcat. its like the onclicklistener is not even in the code.

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MyEvents extends ActionBarActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myevents);

    //TextView tv = (TextView)findViewById(R.id.contentlist);
    ListView dbListView = (ListView)findViewById(R.id.myEventsListView);
    final SQLiteAdapter info = new SQLiteAdapter(this);
    //long lg = 1;
    final Cursor myCursor;
    info.open();
    myCursor = info.getAllItems();
    startManagingCursor(myCursor);
    ListAdapter adapter = new SimpleCursorAdapter(MyEvents.this,
            R.layout.single_myevent, myCursor, new String[] { "title", "description", "time", "_id" }, new int[] { R.id.myEventstitle, R.id.myEventsDesc, R.id.myEventsTime, R.id.myeventid }, 0);
dbListView.setAdapter(adapter);

    info.close();
    dbListView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        String eventid = (String)view.findViewById(R.id.myeventid).toString();
        long myrow = Long.parseLong(eventid);
        Log.d("onclick", "clicked " + eventid);
        //SQLiteAdapter sqlAd = new SQLiteAdapter(this);
        info.open();
        info.deleteEntry(myrow);
        Toast.makeText(MyEvents.this, "deleted" + eventid, Toast.LENGTH_LONG).show();
        info.close();
        Intent restarteventpage = new Intent(MyEvents.this, MyEvents.class);
        startActivity(restarteventpage);
    }

});
}
}

The answer was like 'rIHaN JiTHiN' said in the comments above. I had a button and some textviews in my single row xml. once I deleted the button I could click the row. But how do I prevent this? I had future plans for that button.

But now I recieve errors in logcat when I click a row , it says :

Numberformatexception: Invalid long: "android.widget.TextView...app:id/eventid long.invalidlong

But I need this textview, it stores the id of the row which I am using to identify the row so I can delete it from my sqlite database. am I writting this code wrong? or is there another way?

Was it helpful?

Solution 2

Solved my Invalid long problem :

instead of just doing this :

String eventid = (String)view.findViewById(R.id.myeventid).toString();
    long myrow = Long.parseLong(eventid);

I added the Textview first, then get the text into a string:

TextView eventid = (TextView)view.findViewById(R.id.myeventid);
        String stringeventid = eventid.getText().toString();
        long myrow = Long.parseLong(stringeventid);

Oh boy im really getting good at this! :)

OTHER TIPS

What if you try moving dbListView.setAdapter(adapter); below your dbListView.setOnItemClickListener(); ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top