Question

Im setting up a main activity with a ListView object, however, the ListView will not respond to touch, onItemClick and onContextItemSelected are not reachable, i have set up setOnItemClickListener and registerForContextMenu, and i dont see my error, here is my code:

public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {

long selectedMovieId;
DbHandler dbhandler;
Movie selectedMovie;
MovieAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn_setting = (Button) findViewById(R.id.page_main_setting);
    Button btn_add = (Button) findViewById(R.id.page_main_add);
    ListView lv = (ListView) findViewById(R.id.list);

    btn_add.setOnClickListener(this);
    btn_setting.setOnClickListener(this);
    lv.setOnItemClickListener(this);
    dbhandler = new DbHandler(this);
    registerForContextMenu(lv);



    Cursor c = dbhandler.queryAll();
    startManagingCursor(c);
    adapter = new MovieAdapter(this, c);

    lv.setAdapter(adapter);


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_options, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.main_options_exit:

        finish();

        return true;

    case R.id.main_option_delete_all:

        dbhandler.deleteAll();

        refresh();

        return true;

    }
    return false;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.main_context, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    Log.d("context menu", "clicked");

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    selectedMovieId = info.id;

    switch (item.getItemId()) {

    case R.id.main_context_edit:



        Intent intent = new Intent(this, AddEditActivity.class);
        intent.putExtra(DbConstants.FROM_CONTEXT, selectedMovie+"");
        startActivity(intent);

        return true;

    case R.id.main_context_delete:

        dbhandler.deleteMovie(selectedMovieId);
        refresh();

        return true;

    }
    return false;
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.page_main_setting:

        openOptionsMenu();

        break;

    case R.id.page_main_add:

        DialogInterface.OnClickListener listenerInternet = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intent = new Intent(getBaseContext(),
                        InternetEditActivity.class);
                startActivity(intent);

            }
        };

        DialogInterface.OnClickListener listenerManual = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intent = new Intent(getBaseContext(),AddEditActivity.class);
                    intent.putExtra(DbConstants.MANUAL, DbConstants.MANUAL);    

                startActivity(intent);

            }
        };

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("Please choose an adding method")
                .setCancelable(false).setNegativeButton("Cancel", null)
                .setNeutralButton("via internet", listenerInternet)
                .setPositiveButton("Manual", listenerManual).create();

        dialog.show();

        break;
    }

}

class MovieAdapter extends CursorAdapter /*implements OnTouchListener*/ {

    public MovieAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // inflate the view:
        return getLayoutInflater().inflate(R.layout.main_list_layout,
                parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // bind the data
        // get the data from the cursor

        String subject =  cursor.getString(cursor.getColumnIndex(DbConstants.DB_SUBJECT));
        String body =  cursor.getString(cursor.getColumnIndex(DbConstants.DB_BODY));
        String internal_location =  cursor.getString(cursor.getColumnIndex(DbConstants.DB_INTERNAL_LOCATION));
        int year =  cursor.getInt(cursor.getColumnIndex(DbConstants.DB_YEAR));
        int status =  cursor.getInt(cursor.getColumnIndex(DbConstants.DB_STATUS));
        int rating =  cursor.getInt(cursor.getColumnIndex(DbConstants.DB_RATING));

        TextView subjectText = (TextView) view.findViewById(R.id.list_main_subject);
        TextView bodyText = (TextView) view.findViewById(R.id.list_main_body);
        TextView yearText = (TextView) view.findViewById(R.id.list_main_year);
        TextView statusText = (TextView) view.findViewById(R.id.list_main_status);
        ImageView image = (ImageView) view.findViewById(R.id.list_main_imgae);

        //RatingBar ratingBar = (RatingBar) view.findViewById(R.id.list_main_ratingBar1);
        //ratingBar.setOnTouchListener(this);


        subjectText.setText(subject);
        bodyText.setText(body);
        yearText.setText(String.valueOf(year));
        //ratingBar.setRating(rating);

        Log.d("status in main", status+"");
        Log.d("rating in main", rating+"");



        if (status==0){
        statusText.setText("watched");
        } else if (status==1){
            statusText.setText("Not watched");
        }

        Log.d("ternal loction", internal_location+"!");

        if (internal_location!=null){
        File imgFile = new  File(internal_location);
        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());


            image.setImageBitmap(myBitmap);
            }
        }
    }

    /*@Override
    public boolean onTouch(View v, MotionEvent event) {

        return true;
    }*/
} //Movie Adapter close     


public void refresh(){

    Cursor newCursor = dbhandler.queryAll();
    Cursor oldCursor = adapter.getCursor();
    adapter.changeCursor(newCursor);

    startManagingCursor(newCursor);
    stopManagingCursor(oldCursor);
    oldCursor.close();
}

@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long id) {

    Log.d("list menu", "clicked");

        Intent intent = new Intent(this, AddEditActivity.class);
        intent.putExtra(DbConstants.FROM_LISTVIEW, id);
        startActivity(intent);

}


}   //Main Activity close

it has to be somthing to do with the layout of the list, a simple list inserted next to it worked

Was it helpful?

Solution 2

The problem was that there was a Scroll view object in the layout of the adapter, remove it and the problem will be fixed

OTHER TIPS

OnItemClick event is now intercepted by RatingBar.

Add onTouchEvent listener to your RatingBar and return false to say to system that RatingBar does not handle this events.

 @Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(MotionEvent event)
    return false;
}

Edit: above answer is for subclassing RatingBar.

But you already have onTouchEvent just return false instead of true.

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