Android的ListActivity排点击 [英] Android ListActivity row click
问题描述
我有一个显示有关信息的玩家的活动。这部分工作正常。 (我用了一个适配器)。但我应该在哪里把code单击某一行,当检测?
PlayersActivity.java
包com.democratandchronicle.billstrainingcamp;进口android.os.Bundle;公共类PlayersActivity扩展ListActivity
{ @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_players);
PlayersAdapter适配器=新PlayersAdapter(本);
getListView()setAdapter(适配器)。
PlayersFetchTask loadPlayersTask =新PlayersFetchTask(适配器);
loadPlayersTask.execute();
}
}
PlayersAdapter.java
包com.democratandchronicle.billstrainingcamp;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;
进口android.widget.TextView;公共类PlayersAdapter延伸BaseAdapter { 私人上下文的背景下;
私人LayoutInflater layoutInflater;
私人JSONArray项=新JSONArray();
私人DrawableManager DM;
公共PlayersAdapter(上下文的背景下){
超();
this.context =背景;
this.layoutInflater =(LayoutInflater)this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.dm =新DrawableManager();
} @覆盖
公众诠释的getCount(){
返回this.entries.length();
} @覆盖
公共对象的getItem(INT位置){
尝试{
返回this.entries.getJSONObject(位置);
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
返回null;
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ RelativeLayout的playerRowView; 如果(convertView == NULL)
{
playerRowView =(RelativeLayout的)this.layoutInflater.inflate(R.layout.player_row,父母,假);
}
其他
{
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); 尝试
{
JSONObject的数据行= this.entries.getJSONObject(位置);
playerNameText.setText(dataRow.getString(名字)++ dataRow.getString(姓氏));
playerPositionText.setText(dataRow.getString(位置));
如果(!dataRow.getString(照片)。等于())
{
this.dm.fetchDrawableOnThread(dataRow.getString(照片),playerImageView);
} }
赶上(JSONException E)
{
// TODO自动生成catch块
e.printStackTrace();
}
返回playerRowView;
} 公共无效upDateEntries(JSONArray项)
{
this.entries =条目;
this.notifyDataSetChanged();
}}
应该在哪里我把code单击某一行,当检测?
块引用>要随时随地检测一下就行,请使用
OnItemClickListener
在你的活动。要检测每行不同区域点击,使用
OnClickListeners
内您的适配器getView()
。此外,如果您使用ViewHolder方法,你可以创建一个稍快适配器。该谷歌对话武装您的UI 讨论这和其他良好做法详细信息。
加入
我会怎么做这在我的活动?
块引用>ListActivities有OnListItemClick内置,回调有一个稍微不同的名称:<一href=\"https://developer.android.com/reference/android/app/ListActivity.html#onListItemClick%28android.widget.ListView,%20android.view.View,%20int,%20long%29\"相对=nofollow>
onListItemClick()
。使用它像这样:@覆盖
保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
Toast.makeText(这一点,点击了排+位置,Toast.LENGTH_SHORT).show();
}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'sgetView()
.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(); }
这篇关于Android的ListActivity排点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!