将复选框(ListView)的值插入数据库

时间:2016-09-08 04:07:56

标签: java android listview checkbox

所以这一直困扰我一个星期,我还没有找到答案 基本上我有这个MainActivity,它包含主代码和一个适用于listview的片段。

MainActivity.java

public class MainActivity extends AppCompatActivity {

private Handler handler = new Handler();
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private Button bNext;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(false);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new RoomServer(), "Room Server");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

    }

RoomServer.java

public class RoomServer extends ListFragment implements OnItemClickListener{
    public RoomServer() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,       Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rs, container,  false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setChoiceMode(getListView().CHOICE_MODE_MULTIPLE);
        getListView().setTextFilterEnabled(true);
        ArrayAdapter adapter =    ArrayAdapter.createFromResource(getActivity(), R.array.rs_list,   android.R.layout.simple_list_item_checked);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
    }
}

Fragment_rs.xml

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

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

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>

    <Button
        android:id="@+id/bNext_rs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        android:layout_marginRight="39dp"
        android:layout_marginEnd="39dp"
        android:layout_marginBottom="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

activity_main.xml中

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

listview的项目在strings.XML

中声明

正如您所见,我正在使用CHOICE_MODE_MULTIPLE在列表视图中创建复选框。而现在我仍然坚持如何获得此复选框的值。我打算使用Volley通过单击按钮将值发送到我的数据库。我看到的大部分答案都是使用isChecked和东西,但我真的不明白如何在我的数组适配器中实现它。

1 个答案:

答案 0 :(得分:0)

您可以自己实现自定义数组适配器,也可以使用ListView类中的this方法获取所选项目的SparseBooleanArray个索引:

//Submit button example
submitButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Get base list
        String[] items = getActivity().getResources().getStringArray(R.array.rs_list);

        //Get selected items & set intent args
        SparseBooleanArray selectedItems = getListView().getCheckedItemPositions();
        ArrayList<String> argList = new ArrayList<>();
        for (int i = 0; i < selectedItems.size(); i++) {
            argList.add(items[selectedItems.keyAt(i)]);
        }

        //Start activity
        Intent intent = new Intent();
        intent.putStringArrayListExtra("SelectedItems", argList);
        getActivity().startActivity(intent);
    }
});