我是Android新手。我正在尝试从Parse获取数据并更新适配器。 我正在从Array Adapter扩展我的适配器。 在我的适配器中,我有一个图像和一个文本字段。
我遇到的问题:我只看到数组适配器中数组中的最后一个元素。
这是我的基本设置:
我的parse方法包含一个内部匿名类findCallBackMethod。 在解析的done方法中, 我从Parse获取数据并创建一个新对象以将其添加到我的数组列表中。 此数组列表显示在我的适配器中。 我能够在完成的方法中看到我的整个数组结果列表。
我搜索了堆栈溢出并遵循了一些关于在这个done方法中创建和设置适配器的建议。
到目前为止尝试过:
我在这里失踪了什么?
感谢任何帮助。
谢谢!
以下是我的代码示例:
public void searchPlayer(String playerFirstName)
{
ParseQuery<ParseObject> query_players = ParseQuery.getQuery("myDataFile");
try {
query_players.whereMatches("FirstName","Patrick");
} catch (NullPointerException e) {
e.printStackTrace(); // change this to meaningful message
}
query_players.findInBackground(new FindCallback<ParseObject>()
{
public String shortName;
public int playerID;
@Override
public void done(List<ParseObject> playerList, com.parse.ParseException e) {
if (e == null)
{
d("TAG", "Retrieved: size = " + playerList.size());
for(int i=0;i<playerList.size();i++)
{
newPlayer = MySingleton.getInstance().newPlayer;
arrayOfPlayers = MySingleton.getInstance().arrayOfPlayers;
// short name is abbreviated name; got it from Parse
// create a single player
String shortName = playerList.get(i).getString("ShortName");
String firstName = playerList.get(i).getString("FirstName");
String lastName = playerList.get(i).getString("LastName");
playerID = playerList.get(i).getInt("PlayerID");
Log.d("Player", "short name: " + shortName);
//newPlayer = new Player();
// if (newPlayer != null) {
newPlayer.setPlayerShortName(shortName);
newPlayer.setPlayerFirstName(firstName);
newPlayer.setPlayerLastName(lastName);
newPlayer.setPlayerID(playerID);
newPlayer.setPhotoUrl(playerList.get(i).getString("PhotoUrl"));
Log.d("Player", "the photo url = : " + newPlayer.getPhotoUrl());
//adding the new player to the array
if (newPlayer != null) {
d("Player", "adding the new player to array list ");
arrayOfPlayers.add(newPlayer);
Log.d("Player", "the new player's last name in the arrayList " + arrayOfPlayers.get(i).getPlayerLastName());
Log.d("Player", "the new player's photo url in the arrayList " + arrayOfPlayers.get(i).getPhotoUrl());
}
//printing the playerlist:
for(Player x:arrayOfPlayers) {
Log.d(TAG,"printing the list of player's last name within the for: " + x.getPlayerLastName());
}
lvPlayerList = (ListView) view.findViewById(R.id.lvPlayerList);
aPlayerListAdapter= new PlayerSearchListArrayAdapter(getActivity(),arrayOfPlayers);
lvPlayerList.setAdapter(aPlayerListAdapter);
aPlayerListAdapter.notifyDataSetChanged();
}// end of for
/* THIS WORKS WITHIN THE INNER CLASS --- TESTED)
Log.d("Player", "the array size " + arrayOfPlayers.size());
Log.d("Player", "the player id from the arrayList " + arrayOfPlayers.get(8).getPlayerID());
Log.d("Player"," the activity in the player fragment: " + getActivity());
*/
} //end of (e==null)
else
{
d("score", "Error: " + e.getMessage());
//playerArrayList = null;
}
}// end of done
}); // end of inner class
我的适配器类:
// Adapter for the list: inflates player_list_item
public class PlayerSearchListArrayAdapter extends ArrayAdapter<Player>{
public static final String TAG = "PLAYER_SEARCH_LIST_ADAPTER";
private ImageView imvPlayer;
private TextView tvPlayerFullName;
List<Player> playerList;
LayoutInflater mInflater;
public PlayerSearchListArrayAdapter(Context context,List<Player> playerList) {
//super(context, 0 , playerList);
super(context,0,playerList);
mInflater = LayoutInflater.from(context);
this.playerList = playerList;
}
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
Log.d(TAG,"inside adapter");
Log.d(TAG,"activity = " + getContext());
// get the data item for position
Player player = getItem(position);
Log.d(TAG,"player list size from within the adapter: = " + playerList.size());
Log.d(TAG,"player's last name = " + player.getPlayerLastName());
// find if the view exists. what that means if we reached the max limit
// of items to view on the screen, then Android starts to recycle the
// previous viewable list items.
// If the view does not exist, we have to create one through the inflater
View v = convertView;
if (v == null)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
// v = inflater.inflate(R.layout.player_list_item,parent,false);
// check if to attach to root
v = inflater.inflate(R.layout.player_list_item,parent,false);
}
// find the ids in the tweet_item.xml
ImageView imvPlayer = (ImageView)v.findViewById(R.id.imvPlayer);
TextView tvPlayerFullName = (TextView)v.findViewById(R.id.tvPlayerFullName);
// populate the items
// ImageUri is the url of the image
String imageUri = player.getPhotoUrl();
Picasso.with(getContext()).load(imageUri).into(imvPlayer);
tvPlayerFullName.setText(player.getPlayerShortName());
return v;
}
}
答案 0 :(得分:0)
在这种情况下我对单身的使用是不正确的。 修复了这个问题。我每次循环迭代时都在创建适配器和数组。 改变这解决了这个问题。