嗨我有一个使用android studio的移动程序,我这里有一个edittext和一个动态创建的textview,edittext和textview的数量基于sqlite列数量和单位。并且我还要在确切的输出上添加一个版画屏幕,我想要的是当我滑动片段时,edittext中的值将保存在sqlite中并更新取决于 UNIT 这是动态创建的textview。我已经完成了刷卡和sqlite查询更新,我只是不知道如何保存edittext的逻辑取决于textview值,这是edittext和textview是dnamically创建和循环游标查询
//这里是使用游标创建动态textview和edittext的代码,它位于片段
中public static class PageFragment extends Fragment {
public static final String id = "id";
public static final String cate = "category";
public static final String code = "code";
public static final String desk = "description";
public static final String unit = "unit";
public static final String quan = "Quantity";
TextView tv;
EditText et;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
((TextView) rootView.findViewById(R.id.item_label2)).setText(
getArguments().getString(cate));
((TextView) rootView.findViewById(R.id.item_label3)).setText(
getArguments().getString(code));
((TextView) rootView.findViewById(R.id.item_label4)).setText(
getArguments().getString(desk));
((TextView) rootView.findViewById(R.id.item_label5)).setText(
getArguments().getString(unit));
((TextView) rootView.findViewById(R.id.item_label6)).setText(
getArguments().getString(quan));
((TextView) rootView.findViewById(R.id.item_label1)).setText(
getArguments().getString(id));
TextView code = (TextView)rootView.findViewById(R.id.item_label3);
DBController controller = new DBController(getActivity());
SQLiteDatabase db2 = controller.getWritableDatabase();
Cursor mCursor2 = db2.rawQuery("SELECT Unit,Quantity FROM Countsheet WHERE TRIM(Code) = '"+ code.getText().toString() +"'", null);
while (mCursor2.moveToNext()) {
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.linearLayout2);
// add text view
List<String> data = new ArrayList<String>();
data.add(mCursor2.getString(0));
for (int i = 0; i < data.size(); i++) {
tv = new TextView(getActivity());
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
tv.setTextColor(Color.BLACK);
tv.setText(data.get(i));
tv.setId(R.id.unit);
ll.addView(tv);
et = new EditText(getActivity());
et.setHint("Input Quantity");
et.setTextColor(Color.RED);
et.setId(R.id.quanity);
et.setText(mCursor2.getString(1));
et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
ll.addView(et);
}
// mArrayList.add(mCursor2.getString(mCursor2.getColumnIndex(DBController.FeedReaderContract.FeedEntry.Col_5)));
// Toast.makeText(getActivity(), "" + data, Toast.LENGTH_SHORT).show();
}
return rootView;
}
//这是我更新查询中的代码
public void updateContact(data data) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FeedReaderContract.FeedEntry.Col_6, data.get_Quantity());
// updating row
database.update(TABLE_NAME, values, ""+FeedReaderContract.FeedEntry.Col_3 + "='" + data._Code+ "' AND " + FeedReaderContract.FeedEntry.Col_5 + "='" + data._Unit + "'", null);
data.set_Code(data._Code);
data.set_Unit(data._Unit);
database.close();
}
//这是我的代码,刷卡更新,这是另一个片段
public class PagerFragment extends Fragment {
private List<PageData> data;
private int currentPosition;
private View v;
EditText q;
TextView unit;
TextView code;
private ViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_pager, container, false);
mViewPager = (ViewPager)v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
data = getArguments().getParcelableArrayList("DATA_LIST");
final FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
onswipe();
return v;
}
private void onswipe(){
final DBController controller = new DBController(getActivity());
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
unit = ((TextView)v.findViewById(R.id.unit));
String units = unit.getText().toString();
code = ((TextView) v.findViewById(R.id.item_label3));
String codes = code.getText().toString();
q = ((EditText)v.findViewById(R.id.quanity));
String quan = q.getText().toString();
data data = new data();
data.set_Code(codes);
data.set_Unit(units);
data.set_Quantity(quan);
controller.updateContact(data);
Toast.makeText(getActivity(),"" + codes + quan + units,Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}