2015-11-16 35 views
0

由於某種原因,即使我擁有正確的偵聽器代碼,在列表視圖中點擊某行也不起作用。列表模板中只有文本視圖。我知道當列表視圖模板中有一個按鈕時,有關於分配監聽器的討論。見here在Listview中點擊行甚至無法在Android中運行SetOnItemClicklistener

這裏是我的代碼:

ScheduleActivity:

public class ScheduleActivity extends AppCompatActivity { 

    private String url; 
    JSONObject data = null; 
    Toolbar toolbar; 
    Intent intent; 
    String userId; 
    int eventId; 

    ListView scheduleListView; 
    ScheduleAdapter scheduleAdapter; 
    ArrayList<Schedule> scheduleList = new ArrayList<>(); 
    DBManager dbManager = new DBManager(ScheduleActivity.this); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.schedule_layout); 
     Log.d("Test", ">>>ScheduleActivity<<<"); 
     Bundle extras = getIntent().getExtras(); 
     userId = extras.getString("userId"); 
     eventId = extras.getInt("eventId"); 

     toolbar = (Toolbar) findViewById(R.id.schedule_toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setTitle("Schedules"); 

     scheduleList = dbManager.getSchedules(eventId); 

     scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, scheduleList); 
     scheduleListView = (ListView) findViewById(R.id.scheduleListView); 
     scheduleListView.setAdapter(scheduleAdapter); 

     scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.w("Test", scheduleList.get(position).toString()); 
       intent = new Intent(ScheduleActivity.this, UpdateScheduleActivity.class); 
       intent.putExtra("userId", userId); 
       intent.putExtra("eventId", eventId); 
       startActivity(intent); 
      } 
     }); 

     scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.w("Test", "Long click works"); 
       return true; 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.schedule_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     if (id == R.id.new_schedule) { 
      intent = new Intent(ScheduleActivity.this, CreateScheduleActivity.class); 
      intent.putExtra("eventId", eventId); 
      startActivity(intent); 
     }else if(id == R.id.rankings){ 
      Log.d("Test", "Rankings Activity clicked!"); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

ScheduleAdapter:

public class ScheduleAdapter extends ArrayAdapter<Schedule>{ 

    public ScheduleAdapter(Context context, List<Schedule> schedule) { 
     super(context, R.layout.schedule_list_row, schedule); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater eventInflater = LayoutInflater.from(getContext()); 

     if (convertView == null) { 
      convertView = eventInflater.inflate(R.layout.schedule_list_row, parent, false); 
     } 

     Schedule singleSchedule = getItem(position); 

     //TODO implement ViewHolder pattern 
     TextView club1Code = (TextView) convertView.findViewById(R.id.club1TextView); 
     TextView club2Code= (TextView) convertView.findViewById(R.id.club2TextView); 
     TextView club1Score = (TextView) convertView.findViewById(R.id.club1ScoreTextView); 
     TextView club2Score = (TextView) convertView.findViewById(R.id.club2ScoreTextView); 
     TextView club1SpiritScore = (TextView) convertView.findViewById(R.id.club1SpiritScoreTextView); 
     TextView club2SpiritScore = (TextView) convertView.findViewById(R.id.club2SpiritScoreTextView); 
     TextView time = (TextView) convertView.findViewById(R.id.timeTextView); 
     TextView day = (TextView) convertView.findViewById(R.id.dayTextView); 

     club1Code.setText(singleSchedule.getClub1Id()); 
     club2Code.setText(singleSchedule.getClub2Id()); 
     day.setText("Day " + singleSchedule.getDay()); 
     time.setText(singleSchedule.getStartTime() + " - " + singleSchedule.getEndTime()); 
     club1Score.setText(Integer.toString(singleSchedule.getClub1Score())); 
     club2Score.setText(Integer.toString(singleSchedule.getClub2Score())); 
     club1SpiritScore.setText(Integer.toString(singleSchedule.getClub1SpiritScore())); 
     club2SpiritScore.setText(Integer.toString(singleSchedule.getClub2SpiritScore())); 

     return convertView; 
    } 
} 

活動佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.kennanseno.ultimate_scoreboard_app.Activity.ScheduleActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/schedule_toolbar" 
     android:minHeight="?attr/actionBarSize" 
     android:background="#2196F3" 
     android:title="@string/event_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 
    </android.support.v7.widget.Toolbar> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/scheduleListView" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/schedule_toolbar" /> 
</RelativeLayout> 

行模板:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club1_name_text" 
     android:id="@+id/club1TextView" 
     android:textAlignment="textStart" 
     android:layout_above="@+id/club1SpiritScoreTextView" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club2_name_text" 
     android:id="@+id/club2TextView" 
     android:layout_gravity="right" 
     android:textAlignment="textEnd" 
     android:layout_below="@+id/dayTextView" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/score_divider" 
     android:id="@+id/score_divider" 
     android:textSize="50sp" 
     android:layout_below="@+id/timeTextView" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club1_score_text" 
     android:id="@+id/club1ScoreTextView" 
     android:textSize="50sp" 
     android:layout_marginEnd="22dp" 
     android:layout_below="@+id/timeTextView" 
     android:layout_toStartOf="@+id/score_divider" 
     android:textAlignment="center" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club2_score_text" 
     android:id="@+id/club2ScoreTextView" 
     android:textSize="50sp" 
     android:layout_marginStart="22dp" 
     android:layout_below="@+id/timeTextView" 
     android:layout_toEndOf="@+id/score_divider" 
     android:textAlignment="center" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/start_time_text" 
     android:id="@+id/timeTextView" 
     android:textAlignment="center" 
     android:layout_below="@+id/dayTextView" 
     android:layout_alignEnd="@+id/club2ScoreTextView" 
     android:layout_alignStart="@+id/club1ScoreTextView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club1_spirit_score_text" 
     android:id="@+id/club1SpiritScoreTextView" 
     android:textAlignment="textStart" 
     android:textSize="30sp" 
     android:layout_alignTop="@+id/club2SpiritScoreTextView" 
     android:layout_alignStart="@+id/club1TextView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/club2_spirit_score_text" 
     android:id="@+id/club2SpiritScoreTextView" 
     android:textAlignment="textEnd" 
     android:textSize="30sp" 
     android:layout_below="@+id/club2TextView" 
     android:layout_alignEnd="@+id/club2TextView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/schedule_day_text" 
     android:id="@+id/dayTextView" 
     android:textAlignment="center" 
     android:layout_alignParentTop="true" 
     android:layout_alignEnd="@+id/club2ScoreTextView" 
     android:layout_alignStart="@+id/club1ScoreTextView" /> 

</RelativeLayout> 

回答

0

行模板XML:

android:clickable="true" 

造成問題,因爲RelativeLayout是可點擊與match_parent高寬。

,如果你想獲得點擊事件全行則沒有必要作出父佈局點擊剛剛成立setOnItemClickListener到ListView控件,否則你需要ScheduleAdapter類設置爲onClickListenerRelativeLayout

相關問題