我无法在Android应用中将手机的联系人提取到ListView中

时间:2019-06-14 07:59:02

标签: java android android-listview

我想将我所有的电话联系人提取到应用程序的列表视图中。我在这里所做的是我有一个活动,在该活动中我放置了一个列表视图,并且希望在那里显示我的所有联系人,因此为此创建了一个数据模型和一个适配器,但是每次出现此错误时, 我不知道这是什么错误,

FATAL EXCEPTION: main
Process: com.example.theroos.messageism, PID: 25858
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.theroos.messageism.UserObject

我的FindUserActivity是

FindUserActivity.java

 import android.database.Cursor;
 import android.provider.ContactsContract;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.widget.LinearLayout;
 import java.util.ArrayList;

public class FindUserActivity extends AppCompatActivity {

private RecyclerView mUserList;
private RecyclerView.Adapter mUserListAdapter;
private RecyclerView.LayoutManager mUserListLayoutManager;

ArrayList<UserObject>userList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_user);

    userList = new ArrayList<>();
    initializeRecyclerView();
    getcontactlist();


}

private void getcontactlist(){
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null,null);
    while(phones.moveToNext()){
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        UserObject mContacts = new UserObject(name,phone);
        userList.add(mContacts);
        mUserListAdapter.notifyDataSetChanged();

    }
}private void initializeRecyclerView() {

    mUserList = findViewById(R.id.contacts_listview);
    mUserList.setNestedScrollingEnabled(false);
    mUserList.setHasFixedSize(false);
    mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL,false);
    mUserList.setLayoutManager(mUserListLayoutManager);
    mUserListAdapter = new UserListAdapter(userList);
    mUserList.setAdapter(mUserListAdapter);
}
}

我的数据模型是

UserObject.java

public class UserObject {

private String name;
private String phone;

public UserObject(String name, String phone) {
    this.name = name;
    this.phone = phone;
}

public String getName() { return name;}
public String getPhone() { return phone;}
}

UserListAdapter.java

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserListViewHolder> {


ArrayList<UserObject> userList;

public UserListAdapter(ArrayList<UserObject> userList){
    this.userList = userList;
}

@NonNull
@Override
public UserListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View layoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_user,null,false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutView.setLayoutParams(lp);
    UserListViewHolder rcv = new UserListViewHolder(layoutView);
    return rcv;

}

@Override
public void onBindViewHolder(@NonNull UserListViewHolder holder, int position) {
    holder.mName.setText(userList.get(position).getName());
    holder.mPhone.setText(userList.get(position).getPhone());
}

@Override
public int getItemCount() {
    return userList.size();
}


public class UserListViewHolder extends RecyclerView.ViewHolder {


    TextView mName, mPhone;
    public UserListViewHolder(View View) {
        super(View);
        mName = View.findViewById(R.id.name);
        mPhone = View.findViewById(R.id.phone);
    }
}
}

我尝试了几种方法,但是找不到任何解决方案。请帮忙。

0 个答案:

没有答案