获取解析对象ID从onListItemClick对象、ID、onListItemClick

2023-09-06 15:58:47 作者:不走回头路

我目前工作的一个应用程序,它包含一个朋友列表登录和用户配置文件时。当用户点击好友列表中的朋友,我希望他们被定向到该朋友的个人资料页面。要做到这一点,我一直在试图从被点击列表中的视图,然后传递到哪里我用它来运行所有用户的其他属性的查询(姓名中下一个活动的用户中提取特定对象的ID,电子邮件,家乡等)。我已经找到了如何通过意图传递下去的数据,但我有各种各样的麻烦获取用户ID从列表中单击的项目。我onItemCLick code是如下。

I am currently working on an app which contains a friends list when logged in and user profiles. When the user clicks on a friend in the friends list, I would like them to be directed to a profile page for this friend. To do this, I have been trying to extract the specific object id from the user that is clicked on in the list view and then pass that onto the next activity where I use it to run a query for all the users other attributes (name, email, hometown, etc.). I have figured out how to pass along data through intents but I am having all sorts of trouble obtaining the user id from the list item clicked. My onItemCLick code is below.

```

public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ParseObject item = (ParseObject) l.getAdapter().getItem(position);
    String objectID = item.getObjectId().toString();

    Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
    intent.putExtra("ID", objectID);
    startActivity(intent);

```

我也尝试了这种不利用。

I have also tried something of this sort to not avail.

```

public void onListItemClick(ListView l, View v, int position, long id) {
     super.onListItemClick(l, v, position, id);

     ParseObject item = (ParseObject) l.getAdapter().getItem(position);
     item.saveInBackground(new SaveCallback() {
         @Override
         public void done(ParseException e) {
             if(e == null) {
                 String objectID = item.getObjectId();
                 Log.e(TAG, objectID);

                 Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
                 intent.putExtra("ID", objectID);
                 startActivity(intent);
                }
                else {
                    Log.e(TAG, e.getMessage());
                }
            }
        });

    }

```

在这两种情况下,应用程序崩溃,当我点击列表视图中的用户。我怎样才能成功地拉动对象ID的用户点击,并通过它一起?

In both cases, the app crashes when I click on the user in the list view. How can I succesfully pull the object ID from the user clicked on and pass it along?

编辑:这是错误的logcat

Here is the logcat error.

工艺:com.richluick.ribbit,PID:2194     java.lang.ClassCastException:java.lang.String中不能转换为com.parse.ParseObject             在com.richluick.ribbit.FriendsFragment.onListItemClick(FriendsFragment.java:83)             在android.support.v4.app.ListFragment $ 2.onItemClick(ListFragment.java:58)             在android.widget.AdapterView.performItemClick(AdapterView.java:299)             在android.widget.AbsListView.performItemClick(AbsListView.java:1113)             在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2904)             在android.widget.AbsListView $ 3.run(AbsListView.java:3638)             在android.os.Handler.handleCallback(Handler.java:733)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:136)             在android.app.ActivityThread.main(ActivityThread.java:5017)             在java.lang.reflect.Method.invokeNative(本机方法)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             在dalvik.system.NativeStart.main(本机方法)

Process: com.richluick.ribbit, PID: 2194 java.lang.ClassCastException: java.lang.String cannot be cast to com.parse.ParseObject at com.richluick.ribbit.FriendsFragment.onListItemClick(FriendsFragment.java:83) at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58) at android.widget.AdapterView.performItemClick(AdapterView.java:299) at android.widget.AbsListView.performItemClick(AbsListView.java:1113) at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904) at android.widget.AbsListView$3.run(AbsListView.java:3638) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)

编辑:这里是全code为包括查询和列表适配器活动。     包com.richluick.ribbit;

Here is the full code for the activity including the query and list adapter. package com.richluick.ribbit;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import java.util.List;

public class FriendsFragment extends android.support.v4.app.ListFragment {

    public static final String TAG = FriendsFragment.class.getSimpleName();

    protected List<ParseUser> mFriends;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_friends, container, false);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);

        getActivity().setProgressBarIndeterminateVisibility(true);

        ParseQuery<ParseUser> query =  mFriendsRelation.getQuery();
        query.addAscendingOrder(ParseConstants.KEY_USERNAME);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                getActivity().setProgressBarIndeterminateVisibility(false);

                if (e == null) {
                    mFriends = friends;

                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for (ParseUser user : mFriends) {
                        usernames[i] = user.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),
                    android.R.layout.simple_list_item_1, usernames);
                    setListAdapter(adapter);
                }
                else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
                    builder.setTitle(R.string.error_title)
                            .setMessage(e.getMessage())
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
     super.onListItemClick(l, v, position, id);

        final ParseObject item = (ParseObject) l.getAdapter().getItem(position);
        item.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if(e == null) {
                    String objectID = item.getObjectId();
                    Log.e(TAG, objectID);

                    Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
                    intent.putExtra("ID", objectID);
                    startActivity(intent);
                }
                else {
                    Log.e(TAG, e.getMessage());
                }
            }
        });

}

}

推荐答案

看一看ParseQueryAdapter

Have a look at ParseQueryAdapter

文档: https://parse.com/docs/android_guide#ui-queryadapter

教程: https://parse.com/tutorials/parse-query-adapter

API: https://parse.com/docs/android/ API / COM /解析/ ParseQueryAdapter.html 。

此外,我会建议EventBus到的parseObject传递到下一个活动 https://github.com/greenrobot/EventBus 。

Also I would recommend EventBus to pass the ParseObject to the next Activity https://github.com/greenrobot/EventBus.

然后,你可以这样做:

适配器:

Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
EventBus.getDefault().postSticky(YourParseObject);
startActivity(intent);

在任何地方FriendsProfileActivity:

Anywhere in FriendsProfileActivity:

ParseObject yourParseObject = EventBus.getDefault().getStickyEvent(ParseObject.class);

如果您移动到使用parse.com子类,那么任何的子类可以通过EventBus这种方式发送。

If you move on to using subclasses in parse.com, then any subclass can be sent via the EventBus this way.

例如code - 未经检验的,但应该是接近你想要什么

Example code - untested but should be close to what you want

的ParseQueryAdapter:

有关R.layout.adapter_item_friend布局 - 可定制,但没有花哨这里

The layout for R.layout.adapter_item_friend - can be customized but nothing fancy here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:paddingTop="5dp">


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="item" />

</RelativeLayout>

该适配器:

public class ParseUserFriendsAdapter extends ParseQueryAdapter<ParseUser> {

    private static final String TAG = ParseUserFriendsAdapter.class.getSimpleName();

    public ParseUserFriendsAdapter(Context context) {
        super(context, new ParseQueryAdapter.QueryFactory<ParseUser>() {

            @Override
            public ParseQuery<ParseUser> create() {
                ParseUser currentUser = ParseUser.getCurrentUser();
                ParseRelation<ParseUser> friendsRelation = currentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
                ParseQuery<ParseUser> query =  friendsRelation.getQuery();
                query.addAscendingOrder(ParseConstants.KEY_USERNAME);
                return query;
            }
        });
    }

    @InjectView(R.id.text) TextView text;

    @Override
    public View getItemView(final ParseUser user, View v, ViewGroup parent) {

        if (v == null) {
            v = View.inflate(getContext(), R.layout.adapter_item_friend,
                    null);
        }

        super.getItemView(user, v, parent);

        ButterKnife.inject(this, v);

        text.setText(user.getUsername());


        return v;

    }

}

我使用含有一个ListView常规片段在它的布局:

R.layout.yourfriendslistlayout:

R.layout.yourfriendslistlayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />

</LinearLayout>

的code在片段中的重要部分:

The important parts of the code in the fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");

    mAdapter = new ParseUserFriendsAdapter(getActivity());

    super.onCreate(savedInstanceState);
}

@InjectView(R.id.listview) ListView mListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.yourfriendslistlayout, container, false);

    ButterKnife.inject(this, rootView);

    /** Setting the list adapter for the ListFragment */
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // Now it is easy to get the ParseObject that is clicked
            ParseUser friend = mAdapter.getItem(position);

            Toast.makeText(getActivity(), "clicked " + friend.getObjectId(), Toast.LENGTH_SHORT).show();

            EventBus.getDefault().postSticky(friend);

            Intent intent = new Intent(getActivity(),
                    FriendsProfileActivity.class);
            startActivity(intent);

        }
    });
    return rootView;
}

可选除了片段code:

Optional addition to the fragment code:

/* 
 *This last part automatically sets the load indicator whenever parse performs a query 
 */
private final OnQueryLoadListener<ParseUser> queryListener = new OnQueryLoadListener<ParseUser>() {

    @Override
    public void onLoaded(List<ParseUser> arg0, Exception arg1) {
        getActivity().setProgressBarIndeterminateVisibility(Boolean.FALSE);
    }

    @Override
    public void onLoading() {
        getActivity().setProgressBarIndeterminateVisibility(Boolean.TRUE);
    }
};

@Override
public void onResume() {
    mAdapter.addOnQueryLoadListener(queryListener);
    super.onResume();
}

@Override
public void onPause() {
    mAdapter.removeOnQueryLoadListener(queryListener);
    super.onPause();
}
 
精彩推荐
图片推荐