当我在recyclerview
中选择一项时,会自动选择多项我在做什么
我只想获取没有重复的联系人并列出recyclerview中的所有联系人并显示两个textview(一个用于数字,第二个用于名称)它完成但问题是
当我点击一个项目时,它会被点击4次(例如,如果我点击一个项目,它会显示两个文本视图,但相同的项目具有相同的名称和数字可见,就像我点击了4个项目和看图片)
adpter class
package "";
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.altu.calllocker.R;
import com.example.altu.calllocker.model.ContactsModel;
import java.util.HashMap;
import java.util.List;
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> {
List<ContactsModel> list;
Context context;
public static HashMap<String, String> hashMapBlock;
public ContactsAdapter(List<ContactsModel> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_item, parent, false);
context = parent.getContext();
hashMapBlock = new HashMap<>();
ContactsViewHolder viewHolder = new ContactsViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ContactsViewHolder holder, final int position) {
holder.txtno.setText(list.get(position).get_id());
holder.txt_contactname.setText(list.get(position).getDisplay_name());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("position", list.get(position).get_id() + " " + list.get(position).getData1() + " " + list.get(position).getDisplay_name());
if (holder.linearLayout.getVisibility() == View.VISIBLE) {
holder.linearLayout.setVisibility(View.GONE);
} else {
holder.linearLayout.setVisibility(View.VISIBLE);
holder.txt_contactName.setText(list.get(position).getDisplay_name());
holder.txt_contact_no.setText(list.get(position).getData1());
}
}
});
holder.switchBlock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
hashMapBlock.put(list.get(position).getData1(), "block");
Log.e("block", list.get(position).getData1());
Toast.makeText(context, "block" + " " + list.get(position).getData1(), Toast.LENGTH_SHORT).show();
} else {
hashMapBlock.remove(list.get(position).getData1());
Log.e("unblock", list.get(position).getData1());
Toast.makeText(context, "unblock" + " " + list.get(position).getData1(), Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ContactsViewHolder extends RecyclerView.ViewHolder {
TextView txt_contactname;
TextView txt_contactName;
TextView txt_contact_no;
LinearLayout linearLayout;
SwitchCompat switchBlock;
TextView txtno;
public ContactsViewHolder(View itemView) {
super(itemView);
this.txtno = (TextView) itemView.findViewById(R.id.txtno);
this.txt_contactname = (TextView) itemView.findViewById(R.id.txt_contactName);
this.txt_contactName = (TextView) itemView.findViewById(R.id.txt_contact_name);
this.txt_contact_no = (TextView) itemView.findViewById(R.id.txt_contact_no);
this.linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
this.switchBlock = (SwitchCompat) itemView.findViewById(R.id.switchButton_block);
}
}
}
MainActivity我将所有联系人发送到recyclerview
package "";
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
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.util.Log;
import android.widget.Toast;
import com.example.altu.calllocker.R;
import com.example.altu.calllocker.adapter.ContactsAdapter;
import com.example.altu.calllocker.customclass.SimpleDividerItemDecoration;
import com.example.altu.calllocker.model.ContactsModel;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerview_contacts;
List<ContactsModel> contactVOList;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindview();
new Thread(new Task()).start();
progressDialog.dismiss();
//adapter = new ContactsAdapter(contactlist, getApplicationContext());
//recyclerview_contacts.setAdapter(adapter);
}
public void bindview() {
recyclerview_contacts = (RecyclerView) findViewById(R.id.recyclerview_contacts);
contactVOList = new ArrayList<>();
recyclerview_contacts.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview_contacts.setLayoutManager(llm);
SimpleDividerItemDecoration itemDecoration = new SimpleDividerItemDecoration(this);
recyclerview_contacts.addItemDecoration(itemDecoration);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading.....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
}
class Task implements Runnable {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
getAllContact();
}
});
}
}
private void getAllContact() {
if (contactVOList.size() > 0) {
contactVOList.clear();
}
// List<ContactsModel> contactVOList = new ArrayList();
ContactsModel contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactsModel();
contactVO.setDisplay_name(name);
contactVO.set_id(id);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setData1(phoneNumber);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emailCursor.close();
contactVOList.add(contactVO);
}
}
ContactsAdapter contactAdapter = new ContactsAdapter(contactVOList, getApplicationContext());
//recyclerview_contacts.setLayoutManager(new LinearLayoutManager(this));
recyclerview_contacts.setAdapter(contactAdapter);
contactAdapter.notifyDataSetChanged();
Log.e("size", String.valueOf(contactVOList.size()));
}
}
}
答案 0 :(得分:1)
您应该在OnclickListener
内设置ViewHolder
并使用getLayoutPosition()
方法点击项目的位置。
public class ContactsViewHolder extends RecyclerView.ViewHolder {
TextView txt_contactname;
TextView txt_contactName;
TextView txt_contact_no;
LinearLayout linearLayout;
SwitchCompat switchBlock;
TextView txtno;
public ContactsViewHolder(View itemView) {
super(itemView);
this.txtno = (TextView) itemView.findViewById(R.id.txtno);
this.txt_contactname = (TextView) itemView.findViewById(R.id.txt_contactName);
this.txt_contactName = (TextView) itemView.findViewById(R.id.txt_contact_name);
this.txt_contact_no = (TextView) itemView.findViewById(R.id.txt_contact_no);
this.linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
this.switchBlock = (SwitchCompat) itemView.findViewById(R.id.switchButton_block);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position =getLayoutPosition();
//Your logic
}
});
}
}