I have an activity that displays information about a player. This part works fine. (I used an adapter). But where should I put the code that detects when a row is clicked?
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();
}
}
Where should I put the code that detects when a row is clicked?
To detect a click anywhere on the row, use an OnItemClickListener
in your Activity.
To detect clicks in different regions on each row, use OnClickListeners
inside your Adapter's getView()
.
Also you can create a slightly faster Adapter if you use the ViewHolder method. The Google Talk TurboCharge Your UI discussions this and other good practices in detail.
Addition
How would I do this in my activity?
ListActivities have an OnListItemClick built-in, the callback has a slightly different name: onListItemClick()
. Use it like so:
@Override
protected void onListItemClick (ListView l, View v, int position, long id) {
Toast.makeText(this, "Clicked row " + position, Toast.LENGTH_SHORT).show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With