Android 如何创建动态控件并将值存储在sqlite中?
我正在做一个android应用程序。在这方面,我想创建类似android coreAndroid 如何创建动态控件并将值存储在sqlite中?,android,sqlite,dynamic-controls,Android,Sqlite,Dynamic Controls,我正在做一个android应用程序。在这方面,我想创建类似android corecontacts应用程序的动态控件(edittext)。在ediitext中输入数据后,我想通过单击名为save的按钮将数据保存到sqlite数据库。由于我是android新手,我不知道如何创建动态控件并存储其行值。如果有人知道,请帮助我 我的代码: package com.xiaochaoyang.dynamicviews; import android.app.Activity; import android
contacts
应用程序的动态控件(edittext)
。在ediitext中输入数据后,我想通过单击名为save
的按钮将数据保存到sqlite数据库。由于我是android新手,我不知道如何创建动态控件并存储其行值。如果有人知道,请帮助我
我的代码:
package com.xiaochaoyang.dynamicviews;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" button
private Button mAddButton;
// There always should be only one empty row, other empty rows will
// be removed.
private View mExclusiveEmptyView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_container);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
// Add some examples
inflateEditRow("Xiaochao");
inflateEditRow("Yang");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
//v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
// Helper for inflating a row
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, null);
final ImageButton deleteButton = (ImageButton) rowView
.findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.editText);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
//mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() );
}
}
我的布局:
Row Cointainer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnAddNewItem"
android:layout_width="21dp"
android:layout_height="wrap_content"
android:background="@drawable/transparent_background"
android:gravity="center_vertical"
android:onClick="onAddNewClicked"
android:layout_gravity="right"
android:paddingLeft="5dp"
android:text="+"
android:textColor="@android:color/black" />
</ScrollView>
</LinearLayout>
Row.xml
<?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="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="true"
android:ems="10"
android:hint="" >
<requestFocus />
</EditText>
<Spinner
android:id="@+id/spinnerCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:entries="@array/categories"
android:gravity="right" />
<ImageButton
android:id="@+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/content_remove"
android:background="@drawable/transparent_background"
android:contentDescription="@string/button_delete_row_description"
android:onClick="onDeleteClicked"/>
</LinearLayout>
Row Cointainer.xml
Row.xml
首先,在SO中发布您的问题时,请显示您迄今为止尝试了什么,如果您的应用程序被强制关闭,请发布日志
要使用控件,请参阅
在这里,您可以找到使用控件的可用方法
检查此链接:
它有助于学习和执行SQLite数据库上的CRUD操作
最后,剩下的就是从控件中获取数据(或输入),然后插入到数据库中
另外,请用谷歌搜索。。点击这个链接。这可能对你有帮助,或者其他。使用edittext创建布局,并将其可见性设置为0。然后设置回可见,你想在哪里?在这种情况下,“编辑文本”区域变为空白,它们的相对视图完成了这一任务place@sundar你能告诉我,你找到解决问题的办法了吗?如果是的话,请输入代码好吗?谢谢