可以在android listview中添加不同的项目

时间:2017-03-26 14:59:34

标签: android listview fragment adapter

我的应用程序中有一个ListView,我从用户电话簿中获取联系人然后将它们显示到ListView中,现在我要做的是根据某些条件将不同的子视图添加到ListView项目中,例如我的应用程序检查每个电话簿联系人它在服务器数据库上的可用性,我想要实现的是为该特定联系人添加语音/视频呼叫按钮,并为数据库中不可用的所有其他联系人添加邀请按钮。

我的条件适用于这三个孩子的ListView项目,语音通话按钮,视频通话按钮和邀请按钮,我的条件是如果用户在数据库中可用,那么我只想显示语音/视频通话按钮并隐藏邀请按钮但是如果用户不可用,那么我想显示邀请按钮并隐藏音频/视频通话按钮

的ListView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/contacts_listview"/>

</RelativeLayout>

ListView项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">

<ImageView
    android:id="@+id/contact_item_icon"
    android:layout_width="50dp"
    android:layout_height="50dp"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:paddingLeft="16dp">

    <TextView
        android:id="@+id/contact_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Feel The Light" />


    <TextView
        android:id="@+id/contact_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home" />



</LinearLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_user_audio_call_dark"/>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_user_video_call_dark"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="Invite"
    android:background="@drawable/selector_button_green_oval"
    android:textColor="@color/white"/>

</LinearLayout>

ContactsFragment

public class ContactsAdapter extends ArrayAdapter<ContactItem> {
private static final String LOG_TAG = ContactsAdapter.class.getSimpleName();

public ContactsAdapter(Activity context, List<ContactItem> contactItems) {
    super(context, 0, contactItems);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ContactItem contactItem = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_list_item, parent, false);
    }

    ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_item_icon);
    contactImage.setImageResource(contactItem.contactImage);

    TextView contactName = (TextView) convertView.findViewById(R.id.contact_item_name);
    contactName.setText(contactItem.contactName);

    TextView contactNumber = (TextView) convertView.findViewById(R.id.contact_item_number);
    contactNumber.setText(contactItem.contactNumber);

    return convertView;
}
}

ContactsFragment

public class ContactsListFragment extends Fragment {

private ContactsAdapter contactsAdapter;

public static ContactsListFragment newInstance() {
    return new ContactsListFragment();
}

public ContactsListFragment() {

}

ArrayList<String> contactName = new ArrayList<>();
ArrayList<String> contactNumber = new ArrayList<>();

ArrayList<ContactItem> contactsAvailable = new ArrayList<>();
ArrayList<ContactItem> contactsInvite = new ArrayList<>();

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    Cursor cursor = getActivity().getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null
    );

    String currentNumber = "";

    while (cursor.moveToNext()) {

        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        number = number.replaceAll(" ", "");

        char c = number.charAt(0);
        char c1 = number.charAt(1);
        String s = Character.toString(c)+Character.toString(c1);

        if (s.equals("00")) {
            number = number.replaceAll("00", "+");
        }

        if (number.length() == 12) {
            number = "+" + number;
        }

        if (number.length() == 13) {
            if (!currentNumber.equals(number)) {
                contactName.add(name);
                contactNumber.add(number);
            }

        }

        currentNumber = number;

    }

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragment_contacts_list, container, false);

    QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
    pagedRequestBuilder.setPage(1);
    pagedRequestBuilder.setPerPage(50);

    QBUsers.getUsersByLogins(contactNumber, pagedRequestBuilder, new QBEntityCallback<ArrayList<QBUser>>() {
        @Override
        public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
            for (int i = 0; i < qbUsers.size(); i++) {
                ContactItem contactItem = new ContactItem(qbUsers.get(i).getFullName(), qbUsers.get(i).getLogin(), R.drawable.ic_launcher);
                contactsAvailable.add(contactItem);

            }

            contactsAdapter = new ContactsAdapter(getActivity(), contactsAvailable);

            ListView listView = (ListView) rootView.findViewById(R.id.contacts_listview_available);
            listView.setAdapter(contactsAdapter);

        }

        @Override
        public void onError(QBResponseException e) {
            Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
        }
    });



    return rootView;

}
}

2 个答案:

答案 0 :(得分:0)

您可以在ContactItem类上添加一个标志,如果它在数据库中可用,则将其设置为true或false,然后根据此标志设置适配器中按钮的可见性。

答案 1 :(得分:0)

试试这个:

  1. boolean课程中添加isAvailableInDB标记ContactItem.java
  2. phonebook获取联系人列表,并逐一检查您的服务器database
  3. 如果contactItem中只有database存在isAvailableInDB true false contactItem contactItem
  4. 之后将ArrayList添加到ContactsAdapter
  5. 创建ListView并将其设置为getView()
  6. 最后,在适配器的contactItem方法中添加一个条件,以检查isAvailableInDB标记true。如果false,请显示语音/视频通话按钮&amp;隐藏邀请按钮,如果<ImageButton android:id="@+id/button_voice_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_user_audio_call_dark"/> <ImageButton android:id="@+id/button_video_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_user_video_call_dark"/> <Button android:id="@+id/button_invite" android:layout_width="wrap_content" android:layout_height="30dp" android:text="Invite" android:background="@drawable/selector_button_green_oval" android:textColor="@color/white"/> ,则显示邀请按钮&amp;隐藏语音/视频通话按钮。
  7. 为XML按钮提供ID:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
    ContactItem contactItem = getItem(position);
    
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_list_item, parent, false);
    }
    
    ImageView contactImage = (ImageView) convertView.findViewById(R.id.contact_item_icon);
    contactImage.setImageResource(contactItem.contactImage);
    
    TextView contactName = (TextView) convertView.findViewById(R.id.contact_item_name);
    contactName.setText(contactItem.contactName);
    
    TextView contactNumber = (TextView) convertView.findViewById(R.id.contact_item_number);
    contactNumber.setText(contactItem.contactNumber);
    
    ImageButton buttonVoiceCall= (ImageButton) convertView.findViewById(R.id.button_voice_call);
    ImageButton buttonVideoCall= (ImageButton) convertView.findViewById(R.id.button_video_call);
    Button buttonVoiceCall= (Button) convertView.findViewById(R.id.button_invite);
    
    if(contactItem.getIsAvailableInDB())
    {
         buttonVoiceCall.setVisibility(View.VISIBLE);
         buttonVideoCall.setVisibility(View.VISIBLE);
         buttonInvite.setVisibility(View.GONE);
    }
    else
    {
         buttonVoiceCall.setVisibility(View.GONE);
         buttonVideoCall.setVisibility(View.GONE);
         buttonInvite.setVisibility(View.VISIBLE);
    }
    return convertView;
    }
    

    在适配器的getView()方法中:

    Build

    希望这会对你有帮助〜