ListView不会响应

时间:2014-02-02 22:56:03

标签: android listview contextmenu onitemclick

我使用ListView对象设置主要活动,但是,ListView不会响应触摸,onItemClick和onContextItemSelected无法访问,我已经设置了setOnItemClickListener和registerForContextMenu,我没有看到我的错误,这里是我的代码:

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

它必须与列表的布局有关,插入一个简单的列表工作

2 个答案:

答案 0 :(得分:0)

OnItemClick事件现在被RatingBar拦截。

将onTouchEvent侦听器添加到RatingBar并返回false以向系统说明RatingBar不处理此事件。

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

编辑:上面的答案是为RatingBar的子类化。

但您已经onTouchEvent只有return false而不是真。

答案 1 :(得分:0)

问题是适配器布局中有一个Scroll视图对象,将其删除,问题将得到解决