gpt4 book ai didi

android - 从 onListItemClick 获取解析对象 ID

转载 作者:太空狗 更新时间:2023-10-29 12:41:08 26 4
gpt4 key购买 nike

我目前正在开发一个应用程序,该应用程序包含登录时的好友列表和用户配置文件。当用户点击 friend 列表中的 friend 时,我希望他们被引导到这个 friend 的个人资料页面。为此,我一直在尝试从 ListView 中单击的用户中提取特定对象 ID,然后将其传递到下一个 Activity ,在该 Activity 中我使用它为所有用户运行查询其他属性(名称,电子邮件、家乡等)。我已经想出如何通过 Intent 传递数据,但是我在从单击的列表项中获取用户 ID 时遇到了各种麻烦。我的 onItemCLick 代码如下。

```

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

```

我也尝试过类似的方法,但没有用。

```

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

}

```

在这两种情况下,当我在 ListView 中单击用户时,应用都会崩溃。我怎样才能成功地从用户点击的对象 ID 中提取并传递它?

编辑:这是 logcat 错误。


进程: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( native 方法)
在 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( native 方法)

编辑:这里是 Activity 的完整代码,包括查询和列表适配器。 包 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

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

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

API:https://parse.com/docs/android/api/com/parse/ParseQueryAdapter.html .

此外,我建议使用 EventBus 将 ParseObject 传递给下一个 Activity https://github.com/greenrobot/EventBus .

然后你可以这样做:

适配器:

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

FriendsProfileActivity 中的任何地方:

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

如果您继续在 parse.com 中使用子类,那么任何子类都可以通过这种方式通过 EventBus 发送。

示例代码 - 未经测试但应该接近您想要的

ParseQueryAdapter:

R.layout.adapter_item_friend 的布局 - 可以自定义,但这里没什么特别的

<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 的常规 fragment :

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>

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

fragment 代码的可选添加:

/* 
*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();
}

关于android - 从 onListItemClick 获取解析对象 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574243/

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