2014-02-02 3 views
0

Im настраивает основное действие с объектом ListView, однако ListView не реагирует на касание, onItemClick и onContextItemSelected недоступны, я установил setOnItemClickListener и registerForContextMenu, и я не вижу свою ошибку, вот мой код:ListView не отвечает

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 

он должен быть Somthing делать с макетом списка, простой список вставляется рядом с ним работал

ответ

0

Проблема заключалась в том, что существует объект вида прокрутки в макете адаптера, удалите его и проблема будет решена

0

OnItemClick событие теперь перехвачен RatingBar.

Добавить onTouchEvent прослушиватель в ваш RatingBar и вернуть false, чтобы сказать системе, что RatingBar не обрабатывает эти события.

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

Редактировать: над ответом для подкласса RatingBar.

Но у вас уже есть onTouchEvent только return false вместо истинного.

+0

onToutchEvent здесь так, что рейтинг бара обыкновение реагировать на прикосновения, то предположим, быть permmiment, и в любом случае я пробовал это, но listview все еще не отвечает, я полностью удалил рейтинговый балл, но все равно не отвечу –

Смежные вопросы