我是一名新的Android开发人员,我正在使用Parse作为我的数据库制作应用程序。 Parse为listview提供了自己的适配器,称为'ParseQueryAdapter'。 在我的应用程序中,我想向用户显示AddingFriendsActivity中的用户列表(通过使用ParseQueryAdapter),其中他/她将能够在EditText中搜索PArseUser的用户名并单击Serch按钮以查看用户列表名称和他们的个人资料图片,他们将能够添加到他们的朋友列表。 我的每个ParseUsers都有: 1)JSONArray的关键:“friendsarray” 2)密钥用户名:“用户名” 并且每个都有一个Idqe Id和更多,但它是无关紧要的。 我已经观看并阅读了几乎evrey ParseQueryAdapter的导师,但我仍然无法找出为什么我的AddingFriendsActivity没有运行..如果你发现任何问题,或者你有建议如何解决这个问题,我会很高兴。 这是我的活动代码:
public class AddingFriendsActivity extends ListActivity implements OnClickListener {
ListView listview;
Button btnsearch;
TextView tvname;
ParseQuery<ParseUser> query;//query that finds the searched user (that is not already a friend of the currentUSer)
ParseUser currentUser;//The currentUser
ArrayList<ParseUser> listUsers;//all users
ParseUser clickedUser;//user in the list view that has been clicked
ArrayList<String> listFriendsNames;// list of ONLY USERNAMES of friends from the array friends of the currentUser
QueryFactory<ParseUser> factory;
ParseQueryAdapter<ParseUser> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_adding_friends);
ParseAnalytics.trackAppOpened(getIntent());
listview = (ListView) findViewById(R.id.addingfriends_listview);
currentUser = ParseUser.getCurrentUser();
btnsearch=(Button) findViewById(R.id.addingfriends_btnsearch);
tvname=(Button) findViewById(R.id.addingfriends_etusername);
btnsearch.setOnClickListener(AddingFriendsActivity.this);
JSONArray jArray=currentUser.getJSONArray("friendsarray");// list of friends from the arrayfrinds of the currentUser
if(jArray!=null)
{
ParseUser p = new ParseUser();
listFriendsNames=new ArrayList<String>();
for (int i=0;i<jArray.length();i++)
{
try {
p=(ParseUser) jArray.get(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listFriendsNames.add(p.getUsername());
}
}
refreshTheAdapter();
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
refreshTheAdapter(); }
public void refreshTheAdapter()
{
factory= new ParseQueryAdapter.QueryFactory<ParseUser>()
{
public ParseQuery<ParseUser> create()
{
query =ParseUser.getQuery();
query.whereStartsWith("username",tvname.getText().toString());
if (listFriendsNames!=null)
query.whereNotContainedIn("useranme",listFriendsNames);
query.orderByAscending("useranme");
query.findInBackground(new FindCallback<ParseUser>()
{
public void done(List<ParseUser> objects, ParseException e)
{
if (e == null) listUsers=(ArrayList<ParseUser>) objects;
}
});
return query;
}
};
if (factory!=null)
objectsWereRetrievedSuccessfully();
}
public void objectsWereRetrievedSuccessfully()
{
adapter= new ParseQueryAdapter<ParseUser>(this, factory);
adapter.setTextKey("username");
//adapter.setImageKey("selfphoto");
listview.setAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
clickedUser=listUsers.get(position);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
AlertDialog dialog;
switch (id) {
case 1:
AlertDialog.Builder builder =new AlertDialog.Builder(this);
builder.setTitle("Do you want to add"+clickedUser.getUsername()+"as a new friend?");
builder.setMessage("If you really know this user,add him");
builder.setCancelable(true);
builder.setPositiveButton("Add Friend", new OkOnClickListener());
builder.setNegativeButton("Cancle" , new CancelOnClickListener());
dialog=builder.create();
dialog.show();
break;
default:
break;
}
return super.onCreateDialog(id);
}
public class OkOnClickListener implements DialogInterface.OnClickListener
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
currentUser.addUnique("friendsarray",clickedUser);
listFriendsNames.add(clickedUser.getUsername());
tvname.setText("");
}
}
public class CancelOnClickListener implements DialogInterface.OnClickListener
{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
}
这是我此活动的xml文件:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddingFriendsActivity"
android:orientation="vertical">
<TextView
android:id="@+id/addingfriends_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add New Friends !!"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/addingfriends_etusername"
android:layout_width="188dp"
android:layout_height="match_parent"
android:hint="Search for friends" >
<requestFocus />
</EditText>
<ImageButton
android:id="@+id/addingfriends_btnsearch"
android:layout_width="wrap_content"
android:layout_height="59dp"
/>
</LinearLayout>
<ListView
android:id="@+id/addingfriends_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
</ListView>