在 android 中使用 NumberPicker,但使用来自 SQlite 的字符串
Using NumberPicker in android but with strings from SQlite
是否可以在 android 中创建类似 NumberPicker Widget 的东西,但用从 SQLite 数据库中提取的一系列字符串填充它?
如果只有一个条目,则微调器将有一个字符串,如果有一百个,则将有一百个条目。根据设计,条目不能少于一项,例如,如果没有至少一项,就不能输入 activity。没有硬集上限,但数据将按日期填充,并且由于数据创建方式的性质,该数据集极不可能超过一千个条目。
我想做的是显示保存在 SQLite 数据库中的数据,每个数据集的名称将显示在 NumberPicker 中,然后用户可以快速滚动浏览数据。当数据滚动经过 NumberPicker 中的数据集时,数据将显示在条形图(MPandroid 图表库)中。至少,这是目标。
我已经管理过 SQLite 数据库,并且我已经得到了适用于单个数据集的图表,我只是不知道如何让数据选择端工作。
试试这个代码,这只是一个由 Recycler Adapter 支持的列表 Activity,包含列表 Activity 的 XML 和卡片布局 XML文件
public class ListActivity extends AppCompatActivity {
DBHelper helper;
static List<DBModel>dbList;
RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
addListenerOnButtonAdd();
TextView tvNoData = (TextView)findViewById(R.id.tvNoData);
setTitle("");// This sets the title of the toolbar
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
//topToolBar.setLogo(R.drawable.keyss);// See Notes in MainActivity
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
helper = new DBHelper(this);
dbList = new ArrayList<>();
dbList = helper.getDataFromDB();
mRecyclerView = (RecyclerView)findViewById(R.id.recycleview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Code below defines the adapter
mAdapter = new RecyclerAdapter(this,dbList);
mRecyclerView.setAdapter(mAdapter);
int sz = dbList.size();
if(sz == 0){
tvNoData.setVisibility(View.VISIBLE);
tvNoData.setText("No Data");
}
}
// This method is called from DetailsActivity and notifies Recycler View
that the DB was changed
of DB and Recycler View
public static void removeListRow(int position) {
dbList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, dbList.size());
}
/* this BUTTON is on the ToolBar click to ADD new record */
private void addListenerOnButtonAdd() {
// Navigate to DetailsActivity to ADD new DATA
Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar( tb );
tb.findViewById( R.id.btnAdd ).setOnClickListener( new
View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSP = new Intent(ListActivity.this,
DetailsActivity.class );
Bundle extras = new Bundle();
extras.putString("FROM_LIST_ACTIVITY","true" );
intentSP.putExtras(extras);
startActivity( intentSP );
}
} );
}
public void onBackPressed(){
Intent intent = new Intent( ListActivity.this, MainActivity.class );
startActivity( intent );
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_lightGray"
android:orientation="vertical"
tools:context="com.searchdb.ListActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/color_darkGray"
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="@+id/imageTB"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp"
android:src="@drawable/keyss" />
<TextView
android:text="@string/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toolbar"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginBottom="20dp"
android:id="@+id/tvLA"
android:textStyle="bold"
android:textColor="@color/color_White"
android:textSize="22sp" />
<Button
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:textStyle="bold"
android:focusable="false"
android:textColor="@color/color_White"
android:background="@color/color_Transparent"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvNoData"
android:gravity="center"
android:layout_marginTop="240dp"
android:visibility="invisible"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="@color/color_Red" />
</LinearLayout>
</LinearLayout>
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
static List<DBModel> dbList;
static private Context context;
int sz;
RecyclerAdapter(Context context, List<DBModel> dbList) {
RecyclerAdapter.dbList = new ArrayList<>();
RecyclerAdapter.context = context;
RecyclerAdapter.dbList = dbList;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemLayoutView =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int
position) {
holder.rowid.setText(String.valueOf(dbList.get(position).getRowid()));
holder.station.setText(dbList.get(position).getStation_Name());
System.out.println("RecyclerAdapter BindViewHolder FIRST position
"+position);
}
@Override
public int getItemCount() {
return dbList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView station, rowid;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
rowid = (TextView) itemLayoutView.findViewById(R.id.rvROWID);
station = (TextView) itemLayoutView.findViewById(R.id.rvSTATION);
// Attach a click listener to the entire row view
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intentN = new Intent(context, DetailsActivity.class);
Bundle extras = new Bundle();
extras.putInt("POSITION", getAdapterPosition());
extras.putString("FROM_LIST_ACTIVITY", "false");
intentN.putExtras(extras);
context.startActivity(intentN);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_White">
<TextView
android:id="@+id/rvROWID"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="left|center_vertical"
android:padding="10dp"
android:textAlignment="center"
android:text="Position ID"
android:textColor="@color/color_Black"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"/>
<TextView
android:id="@+id/rvSTATION"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="10dp"
android:gravity="right|center_vertical"
android:text="Station"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/color_Black" />
</RelativeLayout>
</android.support.v7.widget.CardView>
是否可以在 android 中创建类似 NumberPicker Widget 的东西,但用从 SQLite 数据库中提取的一系列字符串填充它?
如果只有一个条目,则微调器将有一个字符串,如果有一百个,则将有一百个条目。根据设计,条目不能少于一项,例如,如果没有至少一项,就不能输入 activity。没有硬集上限,但数据将按日期填充,并且由于数据创建方式的性质,该数据集极不可能超过一千个条目。
我想做的是显示保存在 SQLite 数据库中的数据,每个数据集的名称将显示在 NumberPicker 中,然后用户可以快速滚动浏览数据。当数据滚动经过 NumberPicker 中的数据集时,数据将显示在条形图(MPandroid 图表库)中。至少,这是目标。
我已经管理过 SQLite 数据库,并且我已经得到了适用于单个数据集的图表,我只是不知道如何让数据选择端工作。
试试这个代码,这只是一个由 Recycler Adapter 支持的列表 Activity,包含列表 Activity 的 XML 和卡片布局 XML文件
public class ListActivity extends AppCompatActivity {
DBHelper helper;
static List<DBModel>dbList;
RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
addListenerOnButtonAdd();
TextView tvNoData = (TextView)findViewById(R.id.tvNoData);
setTitle("");// This sets the title of the toolbar
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
//topToolBar.setLogo(R.drawable.keyss);// See Notes in MainActivity
setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
helper = new DBHelper(this);
dbList = new ArrayList<>();
dbList = helper.getDataFromDB();
mRecyclerView = (RecyclerView)findViewById(R.id.recycleview);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// Code below defines the adapter
mAdapter = new RecyclerAdapter(this,dbList);
mRecyclerView.setAdapter(mAdapter);
int sz = dbList.size();
if(sz == 0){
tvNoData.setVisibility(View.VISIBLE);
tvNoData.setText("No Data");
}
}
// This method is called from DetailsActivity and notifies Recycler View
that the DB was changed
of DB and Recycler View
public static void removeListRow(int position) {
dbList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, dbList.size());
}
/* this BUTTON is on the ToolBar click to ADD new record */
private void addListenerOnButtonAdd() {
// Navigate to DetailsActivity to ADD new DATA
Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar( tb );
tb.findViewById( R.id.btnAdd ).setOnClickListener( new
View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSP = new Intent(ListActivity.this,
DetailsActivity.class );
Bundle extras = new Bundle();
extras.putString("FROM_LIST_ACTIVITY","true" );
intentSP.putExtras(extras);
startActivity( intentSP );
}
} );
}
public void onBackPressed(){
Intent intent = new Intent( ListActivity.this, MainActivity.class );
startActivity( intent );
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_lightGray"
android:orientation="vertical"
tools:context="com.searchdb.ListActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/color_darkGray"
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="@+id/imageTB"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp"
android:src="@drawable/keyss" />
<TextView
android:text="@string/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toolbar"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginBottom="20dp"
android:id="@+id/tvLA"
android:textStyle="bold"
android:textColor="@color/color_White"
android:textSize="22sp" />
<Button
android:text="@string/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:layout_marginLeft="100dp"
android:textSize="18sp"
android:textStyle="bold"
android:focusable="false"
android:textColor="@color/color_White"
android:background="@color/color_Transparent"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvNoData"
android:gravity="center"
android:layout_marginTop="240dp"
android:visibility="invisible"
android:textAllCaps="true"
android:textStyle="bold"
android:textSize="30sp"
android:textColor="@color/color_Red" />
</LinearLayout>
</LinearLayout>
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
static List<DBModel> dbList;
static private Context context;
int sz;
RecyclerAdapter(Context context, List<DBModel> dbList) {
RecyclerAdapter.dbList = new ArrayList<>();
RecyclerAdapter.context = context;
RecyclerAdapter.dbList = dbList;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemLayoutView =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int
position) {
holder.rowid.setText(String.valueOf(dbList.get(position).getRowid()));
holder.station.setText(dbList.get(position).getStation_Name());
System.out.println("RecyclerAdapter BindViewHolder FIRST position
"+position);
}
@Override
public int getItemCount() {
return dbList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
public TextView station, rowid;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
rowid = (TextView) itemLayoutView.findViewById(R.id.rvROWID);
station = (TextView) itemLayoutView.findViewById(R.id.rvSTATION);
// Attach a click listener to the entire row view
itemLayoutView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intentN = new Intent(context, DetailsActivity.class);
Bundle extras = new Bundle();
extras.putInt("POSITION", getAdapterPosition());
extras.putString("FROM_LIST_ACTIVITY", "false");
intentN.putExtras(extras);
context.startActivity(intentN);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_White">
<TextView
android:id="@+id/rvROWID"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="left|center_vertical"
android:padding="10dp"
android:textAlignment="center"
android:text="Position ID"
android:textColor="@color/color_Black"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"/>
<TextView
android:id="@+id/rvSTATION"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="10dp"
android:gravity="right|center_vertical"
android:text="Station"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/color_Black" />
</RelativeLayout>
</android.support.v7.widget.CardView>