【问题标题】:Insert Value of Checkbox (ListView) into Database将复选框(ListView)的值插入数据库
【发布时间】:2016-09-08 14:55:41
【问题描述】:

所以这已经困扰了我一个星期,我还没有找到答案。
基本上我有这个 MainActivity,它包含主要代码和适用于列表视图的片段。

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>

在strings.XML中声明的listview r的项目

如您所见,我正在使用 CHOICE_MODE_MULTIPLE 在列表视图中创建复选框。现在我被困在如何获得这个复选框的价值上。我打算使用 Volley 通过单击按钮将值发送到我的数据库。我看到的大多数答案都是使用 isChecked 之类的,但我真的不明白如何在我的数组适配器中实现它。

【问题讨论】:

  • 你在 ischeckd 中能理解什么?

标签: java android listview checkbox


【解决方案1】:

您可以自己实现自定义数组适配器,也可以使用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);
    }
});

【讨论】:

  • 是的,我只是将按钮添加为一个通用示例以接收点击事件,然后更新数据库中的选中项目。在此示例中,它启动了一个新活动,但作为替代方案,您可以从 argList 获取项目并更新您的数据库。
猜你喜欢
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多