gpt4 book ai didi

Android Parse.com如何从Listview的点击事件中获取名称

转载 作者:行者123 更新时间:2023-11-30 02:32:00 25 4
gpt4 key购买 nike

我无法弄清楚如何从 parse.com 填充的 listView 中获取名称或 ID。我将它设置为使用存储对象,但我无法访问它的任何方法。我需要做什么才能使此功能正常运行?

存储对象

@ParseClassName("Store")
public class Store extends ParseObject{

public Store() {

}

public String getStoreName() {
return getString("storeName");
}

public void setStoreName(String storeName) {
put("storeName", storeName);
}

public ParseFile getPhotoFile() {
return getParseFile("imgStore");
}

public void setPhotoFile(ParseFile file) {
put("imgStore", file);
}
}

适配器

public class MainListAdapter extends ParseQueryAdapter<Store> {

public MainListAdapter(Context context) {
// Specification of which stores to display
super(context, new QueryFactory<Store>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Store");
query.whereExists("storeName");
return query;
}
});
}

@Override
public View getItemView(Store object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.listview_mainpage, null);
}

super.getItemView(object, v, parent);

// Add and download the image
ParseImageView storeImage = (ParseImageView) v.findViewById(R.id.imgStore);
ParseFile imageFile = object.getPhotoFile();
if (imageFile != null) {
storeImage.setParseFile(imageFile);
storeImage.loadInBackground();
}

// Add the title view
TextView titleTextView = (TextView) v.findViewById(R.id.textViewStoreTitle);
titleTextView.setText(object.getStoreName());

return v;
}

}

点击监听

        adapter = new MainListAdapter(getActivity());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String selectedFromList = (listView.getItemAtPosition(position).getClass().toString()); //What do I place here to get access to the storeName or what do I have to do to obtain the correct id/storeName
Intent intent = new Intent(getActivity(), ItemListActivity.class);
intent.putExtra("myString", selectedFromList);
startActivity(intent);
}
});
adapter.loadObjects();

最佳答案

getClass 不是您想要的监听器。我认为您想将该项目转换为 Store 类。

而不是这个——

String selectedFromList = (listView.getItemAtPosition(position).getClass().toString());

你想要这样的东西——

Store selectedStore = (Store) listView.getItemAtPosition(position);
String selectedFromList = selectedStore.getName(); // Or whatever method you need

关于Android Parse.com如何从Listview的点击事件中获取名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27208841/

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