ListView(由Parse.com填充)| OnItemClicked获取objectId

时间:2015-03-16 06:04:23

标签: android listview parse-platform

我使用ParseQueryAdapter从Parse.com填充listView。 listView使用我的解析类中的每个对象填充一些选定的数据。我想要的是,当点击listItem时,我应该得到被点击项目的对象ID,这样我就可以使用该objectId在另一个活动中加载该对象的更多细节。

我的Parse Adapter:

@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;
    }

正如你所看到的,我试图做的是,我在一个在ListItem上看不见的textView中加载了objectId。

我的ListActivity:

@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。 ?????????????????????????????????????????????????? ????????????

2 个答案:

答案 0 :(得分:0)

自定义并尝试以下代码来实现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

            }

        }

答案 1 :(得分:0)

如何获得objectId

考虑使用getObjectId()方法。

  

对象ID的访问者。一旦对象id被分配   对象保存到服务器。

您获得null的原因可能是因为您使用的ParseObject从未存储在服务器上。也许你正在访问本地存储的对象?

  

我想获得仅被点击的对象的objectId   在列表视图中。

ParseObject存储在可以编入索引的List中,具体取决于点击的项目。