2013-02-05 57 views
4

我有一個活動,顯示關於一名球員的信息。這部分工作正常。 (我使用了一個適配器)。但是我應該在哪裏放置檢測行被點擊的代碼?Android ListActivity行點擊

PlayersActivity.java

package com.democratandchronicle.billstrainingcamp; 

import android.os.Bundle; 

public class PlayersActivity extends ListActivity 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_players); 
     PlayersAdapter adapter = new PlayersAdapter(this); 
     getListView().setAdapter(adapter); 
     PlayersFetchTask loadPlayersTask = new PlayersFetchTask(adapter); 
     loadPlayersTask.execute(); 
    } 
} 

PlayersAdapter.java

package com.democratandchronicle.billstrainingcamp; 


import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class PlayersAdapter extends BaseAdapter { 

    private Context context; 
    private LayoutInflater layoutInflater; 
    private JSONArray entries = new JSONArray(); 
    private DrawableManager dm; 
    public PlayersAdapter(Context context) { 
     super(); 
     this.context = context; 
     this.layoutInflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.dm = new DrawableManager(); 
    } 

    @Override 
    public int getCount() { 
     return this.entries.length(); 
    } 

    @Override 
    public Object getItem(int position) { 
     try { 
      return this.entries.getJSONObject(position); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     RelativeLayout playerRowView; 

     if (convertView == null) 
     { 
      playerRowView = (RelativeLayout) this.layoutInflater.inflate(R.layout.player_row, parent, false); 
     } 
     else 
     { 
      playerRowView = (RelativeLayout) convertView; 
     } 

     TextView playerNameText = (TextView) playerRowView.findViewById(R.id.playerName); 
     TextView playerPositionText = (TextView) playerRowView.findViewById(R.id.playerPosition); 
     ImageView playerImageView = (ImageView) playerRowView.findViewById(R.id.playerImage); 

     try 
     { 
      JSONObject dataRow = this.entries.getJSONObject(position); 
      playerNameText.setText(dataRow.getString("firstName") + " "+ dataRow.getString("lastName")); 
      playerPositionText.setText(dataRow.getString("position")); 
      if (!dataRow.getString("photo").equals("")) 
      { 
       this.dm.fetchDrawableOnThread(dataRow.getString("photo"), playerImageView); 
      } 

     } 
     catch (JSONException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return playerRowView; 
    } 

    public void upDateEntries(JSONArray entries) 
    { 
     this.entries = entries; 
     this.notifyDataSetChanged(); 
    } 

} 
+0

在getView方法中.. – smk

+0

方法的名稱是什麼?我試過尋找OnClick或點擊 –

回答

10

我應該放在哪裏單擊某一行時檢測的代碼?

要檢測行上任意位置的點擊,請在活動中使用OnItemClickListener

要檢測每行不同區域的點擊次數,請在適配器的getView()中使用OnClickListeners

如果您使用ViewHolder方法,您也可以創建稍微快一點的適配器。 Google Talk TurboCharge Your UI詳細討論了這一點以及其他良好做法。


加成

如何將我在活動做到這一點?

ListActivities有一個OnListItemClick內置函數,該回調函數有一個稍微不同的名稱:onListItemClick()。像這樣使用它:

@Override 
protected void onListItemClick (ListView l, View v, int position, long id) { 
    Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show(); 
} 
+0

我如何在我的活動中做到這一點?我試着做getListView()。OnItemClickListener,但找不到方法。 –

+0

在你的適配器的getView方法..獲取按鈕或圖像或任何和實現onClickListener。 – smk

+0

明白了,謝謝! –