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

时间:2014-11-30 00:48:32

标签: android listview parse-platform android-adapter onitemclicklistener

我无法弄清楚如何从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;
	}

}

Onclick听众

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

1 个答案:

答案 0 :(得分:1)

getClass不是你想要的听众。我想你想把这个项目投射到班级Store

而不是这个 -

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

你想要类似的东西 -

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