gpt4 book ai didi

Android ListActivity 行点击

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:22 27 4
gpt4 key购买 nike

我有一个显示玩家信息的 Activity 。这部分工作正常。 (我使用了适配器)。但是,我应该将检测某行何时被单击的代码放在哪里?

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?

要检测行中任意位置的点击,请使用 OnItemClickListener在您的 Activity 中。

要检测每行不同区域的点击,请使用 OnClickListeners在适配器的 getView() 中。

如果您使用 ViewHolder 方法,您还可以创建一个稍快的适配器。 Google Talk TurboCharge Your UI详细讨论这个和其他良好做法。


加法

How would I do this in my activity?

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();
}

关于Android ListActivity 行点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713539/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com