包含CheckedTextView多个选择的自定义ListView

时间:2019-02-04 00:44:41

标签: android

关于如何使用包含CheckedTextedView的ListView,我已经浏览了许多答案,但是所有解决方案似乎都非常不同,这使我更加困惑。

我正在寻找一个简单的解决方案,将所有选定的项目添加到我的乘员组ArrayList中,但是在CheckedTextView中实现((CheckedTextView) v).toggle();android:clickable="true" android:focusable="true"之后,我的列表似乎根本不返回任何值。任何帮助或建议,将不胜感激。

我的列表布局:

<CheckedTextView
    android:id="@+id/txt_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center_vertical"
    android:padding="5sp"
    android:checked="false"
    android:clickable="true"
    android:focusable="true"
/>
<TextView
    android:id="@+id/textViewUserAvailability"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="availability" 
    />

我的自定义列表视图类

public class UserList extends ArrayAdapter<User> {

    private Activity context;
     private List<User> userList;

    public UserList(Activity context, List<User> userList)  {
        super (context, R.layout.list_layoutuser, userList);
    this.context = context;
    this.userList = userList;
    }

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

        LayoutInflater inflater1 = context.getLayoutInflater();
        View listViewItem1 = inflater1.inflate(R.layout.list_layoutuser, null, true);
        TextView textViewUserAvailability = (TextView) listViewItem1.findViewById(R.id.textViewUserAvailability);
        CheckedTextView checkedtextViewUserName = (CheckedTextView) listViewItem1.findViewById(R.id.txt_title);

        checkedtextViewUserName.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                ((CheckedTextView) v).toggle();
            }
        });

        User user = userList.get(position);
        checkedtextViewUserName.setText(user.getUserName());
        textViewUserAvailability.setText(user.getUserAvailability());

        return listViewItem1;
    }
}

和我主要班级的一些内容

  public class BookingHome extends AppCompatActivity {

    Button buttonBook;
    User user;


    DatabaseReference dbBooking;
    DatabaseReference databaseUsers;

    ListView listViewUsers;
    List<User> userList;

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

        dbBooking = FirebaseDatabase.getInstance().getReference("Bookings");
        databaseUsers = FirebaseDatabase.getInstance().getReference("users");

       buttonBook = (Button) findViewById(R.id.buttonBook);

        listViewUsers = (ListView) findViewById(R.id.listViewUsers);
        userList = new ArrayList<>();

        buttonBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addBooking();
            }
        });
     }

    @Override
    protected void onStart(){
        super.onStart();

       databaseUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                userList.clear();

                for(DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                    User user = userSnapshot.getValue(User.class);
                    userList.add(user);
                }

                UserList adapter = new UserList(BookingHome.this, userList);
                listViewUsers.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError1) {

            }
        });

        listViewUsers.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listViewUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                user = userList.get(position);
            }
        });
    }

    private void addBooking ()  {
    String boatName = boat.boatName;
    ArrayList<String> crew;
        crew = new ArrayList<String>();
crew.add(user.userName);

    if (!TextUtils.isEmpty(bookingDateString)) {
        String id = dbBooking.push().getKey();

        Booking booking = new Booking(id, coachName, boatName, bookingDate, bookingStartDate, bookingEndDate, crew);
        dbBooking.child(id).setValue(booking);
        Toast.makeText(this, "Boat booked", LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Enter Date", Toast.LENGTH_SHORT).show();
    }


    }
}

0 个答案:

没有答案