ListView(Populated By Parse.com) |OnItemClicked 获取objectId
ListView(Populated By Parse.com) |OnItemClicked to get objectId
我正在使用 ParseQueryAdapter 从 Parse.com 填充一个 listView。 listView 填充了来自我的解析 class 的每个对象的一些选定数据。我想要的是,当一个 listItem 被点击时,我应该得到被点击项目的对象 ID,这样我就可以使用那个 objectId 在另一个 activity.
中加载该对象的更多细节
我的解析适配器:
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_feed_item, null);
}
super.getItemView(object, v, parent);
// Add and download the image
ParseImageView propImage = (ParseImageView) v.findViewById(R.id.propThumbnail);
ParseFile imageFile = object.getParseFile("Image1");
if (imageFile != null) {
propImage.setParseFile(imageFile);
propImage.loadInBackground();
}
// Add the title view
TextView priceTextView = (TextView) v.findViewById(R.id.priceField);
priceTextView.setText("Total Price:"+object.getString("TotalPrice"));
TextView superTextView = (TextView) v.findViewById(R.id.superAreaFieldList);
superTextView.setText("Super Area:"+object.getString("SuperArea"));
TextView priceUnitTextView = (TextView) v.findViewById(R.id.priceUnitFieldList);
priceUnitTextView.setText("@"+ object.getString("PriceUnit"));
TextView locationTextView = (TextView) v.findViewById(R.id.locationList);
locationTextView.setText("Locality:"+object.getString("Locality"));
TextView cityTextView = (TextView) v.findViewById(R.id.cityList);
cityTextView.setText("City:"+object.getString("City"));
TextView bhkTextView = (TextView) v.findViewById(R.id.bhkInfoList);
bhkTextView.setText(object.getString("BedRooms")+"BHK");
TextView projNameTextView = (TextView) v.findViewById(R.id.projName);
projNameTextView.setText("By:"+object.getString("ProjName"));
TextView objIdTextView =(TextView) v.findViewById(R.id.listObjId);
objIdTextView.setText(object.getString("objectId"));
// Add a reminder of how long this item has been outstanding
/*TextView timestampView = (TextView) v.findViewById(R.id.timestamp);
timestampView.setText(object.getCreatedAt().toString());*/
return v;
}
如您所见,我尝试做的是,我将 objectId 加载到 ListItem 上不可见的 textView 中。
我的列表Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rent_listing_feed);
// Initialize main ParseQueryAdapter
mainAdapter = new ParseQueryAdapter<ParseObject>(this, "PropertyDetails");
mainAdapter.setTextKey("TotalPrice");
mainAdapter.setTextKey("SuperArea");
mainAdapter.setTextKey("PriceUnit");
mainAdapter.setTextKey("Locality");
mainAdapter.setTextKey("City");
mainAdapter.setTextKey("BedRooms");
mainAdapter.setTextKey("ProjName");
mainAdapter.setImageKey("Image1");
mainAdapter.setTextKey("objectId");
// Initialize the subclass of ParseQueryAdapter
rexovoAdapter = new CustomAdapter(this);
// Initialize ListView and set initial view to mainAdapter
listView = (ListView) findViewById(R.id.listFeedFromParse);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
if (listView.getAdapter() == mainAdapter) {
listView.setAdapter(rexovoAdapter);
rexovoAdapter.loadObjects();
} else {
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ParseObject parseObject = rexovoAdapter.getItem(position);
// parseObject.get("objectId");
Intent intent=new Intent(RentListingFeed.this, RentListingListItemActivity.class);
intent.putExtra("ObjectId",parseObject.getString("objectId"));
startActivity(intent);
}
});
}
}
然后我尝试获取objectId,通过Intent发送给下一个Activity。但是,在下一个 Activity 中没有收到 ObjectId。
???????????????????????????????????????? ??????????????
自定义尝试以下代码实现listview行的onclick
public class Myadapter extends BaseAdapter implements OnClickListener,OnBufferingUpdateListener{
String currentid=null;
@Override
public View getView(final int position, View convertView, final ViewGroup arg2) {
// TODO Auto-generated method stub
View vi = convertView;
crntposition=position;
if(convertView==null){
Context context=arg2.getContext();
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.songlistrow, null);
TextView objIdTextView =(TextView) vi.findViewById(R.id.listObjId);
objIdTextView.setText(object.getString("objectId"));
objIdTextView.setTag(crntposition+"%"+object.getString("objectId"));
}
// your code
vi.setOnClickListener(new OnItemClickListener(crntposition,objIdTextView.setTag));
return vi;
}
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position,String tag) {
mPosition = position;
currentid=tag.substring(tag.indexOf("%"),tag.length()-1);//here you will getthe objectid
//System.out.println("pos"+mPosition);
}
@Override
public void onClick(View arg0) {
System.out.println("current position"+mPosition);// you can use the position here I have just printed it
}
}
如何获得objectId
?
考虑使用 getObjectId()
方法。
Accessor to the object id. An object id is assigned as soon as an
object is saved to the server.
您得到空值的原因可能是因为您正在使用的 ParseObject
从未存储在服务器上。也许您正在访问本地存储的对象?
I want to get the objectId of only that object which is being clicked
in the listview.
将您的 ParseObject
存储在 List
中,您可以根据单击的项目对其进行索引。
我正在使用 ParseQueryAdapter 从 Parse.com 填充一个 listView。 listView 填充了来自我的解析 class 的每个对象的一些选定数据。我想要的是,当一个 listItem 被点击时,我应该得到被点击项目的对象 ID,这样我就可以使用那个 objectId 在另一个 activity.
中加载该对象的更多细节我的解析适配器:
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_feed_item, null);
}
super.getItemView(object, v, parent);
// Add and download the image
ParseImageView propImage = (ParseImageView) v.findViewById(R.id.propThumbnail);
ParseFile imageFile = object.getParseFile("Image1");
if (imageFile != null) {
propImage.setParseFile(imageFile);
propImage.loadInBackground();
}
// Add the title view
TextView priceTextView = (TextView) v.findViewById(R.id.priceField);
priceTextView.setText("Total Price:"+object.getString("TotalPrice"));
TextView superTextView = (TextView) v.findViewById(R.id.superAreaFieldList);
superTextView.setText("Super Area:"+object.getString("SuperArea"));
TextView priceUnitTextView = (TextView) v.findViewById(R.id.priceUnitFieldList);
priceUnitTextView.setText("@"+ object.getString("PriceUnit"));
TextView locationTextView = (TextView) v.findViewById(R.id.locationList);
locationTextView.setText("Locality:"+object.getString("Locality"));
TextView cityTextView = (TextView) v.findViewById(R.id.cityList);
cityTextView.setText("City:"+object.getString("City"));
TextView bhkTextView = (TextView) v.findViewById(R.id.bhkInfoList);
bhkTextView.setText(object.getString("BedRooms")+"BHK");
TextView projNameTextView = (TextView) v.findViewById(R.id.projName);
projNameTextView.setText("By:"+object.getString("ProjName"));
TextView objIdTextView =(TextView) v.findViewById(R.id.listObjId);
objIdTextView.setText(object.getString("objectId"));
// Add a reminder of how long this item has been outstanding
/*TextView timestampView = (TextView) v.findViewById(R.id.timestamp);
timestampView.setText(object.getCreatedAt().toString());*/
return v;
}
如您所见,我尝试做的是,我将 objectId 加载到 ListItem 上不可见的 textView 中。
我的列表Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rent_listing_feed);
// Initialize main ParseQueryAdapter
mainAdapter = new ParseQueryAdapter<ParseObject>(this, "PropertyDetails");
mainAdapter.setTextKey("TotalPrice");
mainAdapter.setTextKey("SuperArea");
mainAdapter.setTextKey("PriceUnit");
mainAdapter.setTextKey("Locality");
mainAdapter.setTextKey("City");
mainAdapter.setTextKey("BedRooms");
mainAdapter.setTextKey("ProjName");
mainAdapter.setImageKey("Image1");
mainAdapter.setTextKey("objectId");
// Initialize the subclass of ParseQueryAdapter
rexovoAdapter = new CustomAdapter(this);
// Initialize ListView and set initial view to mainAdapter
listView = (ListView) findViewById(R.id.listFeedFromParse);
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
if (listView.getAdapter() == mainAdapter) {
listView.setAdapter(rexovoAdapter);
rexovoAdapter.loadObjects();
} else {
listView.setAdapter(mainAdapter);
mainAdapter.loadObjects();
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ParseObject parseObject = rexovoAdapter.getItem(position);
// parseObject.get("objectId");
Intent intent=new Intent(RentListingFeed.this, RentListingListItemActivity.class);
intent.putExtra("ObjectId",parseObject.getString("objectId"));
startActivity(intent);
}
});
}
}
然后我尝试获取objectId,通过Intent发送给下一个Activity。但是,在下一个 Activity 中没有收到 ObjectId。 ???????????????????????????????????????? ??????????????
自定义尝试以下代码实现listview行的onclick
public class Myadapter extends BaseAdapter implements OnClickListener,OnBufferingUpdateListener{
String currentid=null;
@Override
public View getView(final int position, View convertView, final ViewGroup arg2) {
// TODO Auto-generated method stub
View vi = convertView;
crntposition=position;
if(convertView==null){
Context context=arg2.getContext();
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.songlistrow, null);
TextView objIdTextView =(TextView) vi.findViewById(R.id.listObjId);
objIdTextView.setText(object.getString("objectId"));
objIdTextView.setTag(crntposition+"%"+object.getString("objectId"));
}
// your code
vi.setOnClickListener(new OnItemClickListener(crntposition,objIdTextView.setTag));
return vi;
}
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position,String tag) {
mPosition = position;
currentid=tag.substring(tag.indexOf("%"),tag.length()-1);//here you will getthe objectid
//System.out.println("pos"+mPosition);
}
@Override
public void onClick(View arg0) {
System.out.println("current position"+mPosition);// you can use the position here I have just printed it
}
}
如何获得objectId
?
考虑使用 getObjectId()
方法。
Accessor to the object id. An object id is assigned as soon as an object is saved to the server.
您得到空值的原因可能是因为您正在使用的 ParseObject
从未存储在服务器上。也许您正在访问本地存储的对象?
I want to get the objectId of only that object which is being clicked in the listview.
将您的 ParseObject
存储在 List
中,您可以根据单击的项目对其进行索引。