【发布时间】:2017-09-19 04:15:21
【问题描述】:
我是 android 新手,一直在玩一个简单的盘点应用程序并遇到了这个问题:
在我的“设置库存”屏幕上,产品列表和当前库存是从数据库中获取的,并显示在以编程方式生成的两列 TextView 中。
从数据库中抓取并放入activity_set.xml中的空ListView的onCreate方法:
public class SetActivity extends AppCompatActivity{
private static final String TAG = "SetActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set);
ArrayList<Product> stock = Products.retrieveProducts(this);
final ListView mListView = (ListView) findViewById(setList);
ProductEditAdapter adapter = new ProductEditAdapter(this,R.layout.adapter_set_layout, stock);
mListView.setAdapter(adapter);
}
ProductEditAdapter 类:
public class ProductEditAdapter extends ArrayAdapter<Product> {
private static final String TAG = "ProductEditAdapter";
private Context mContext;
int mResource;
public ProductEditAdapter(Context context, int resource, ArrayList<Product> objects){
super(context,resource,objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
int stock = getItem(position).getStock();
Product product = new Product(name,stock);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView tvName = (TextView) convertView.findViewById(R.id.pie_name);
TextView tvCurrentStock = (TextView) convertView.findViewById(R.id.current_stock);
tvCurrentStock.setText(String.valueOf(stock));
tvName.setText(name + ":");
tvCurrentStock.setTag(name);
return convertView;
}
}
创建它时使用的 adapter_set_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/adapsetLayout"
android:weightSum="100">
<TextView
android:gravity="center"
android:textAlignment="center"
android:text="TextView1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/pie_name"
android:layout_weight="20"
android:textColor="#000000"
android:textStyle="bold"
/>
<TextView
android:gravity="center"
android:textAlignment="center"
android:text="TextView1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/current_stock"
android:layout_weight="80"
android:textColor="#000000"
android:textStyle="bold"
android:onClick="onClick"
android:clickable="true"
/>
</LinearLayout>
当您单击某个商品的库存时,您会看到一个弹出窗口以输入一个新编号,然后您可以单击“确定”更新该 TextView 或单击“取消”更新该文本视图。
然后有一个提交按钮,它解析所有 TextView 并将数据库中每个产品的库存编号更新为新值。
在点击提交之前,更改的 TextView 值是临时的,如果您离开屏幕,它们将恢复为从中获取的值。
SetActivity中的onClick方法:
public void onClick(View v){
if(v.getId()==R.id.submitButton) {
confirmDialog();
}
else if(v.getId()==R.id.current_stock) {
LinearLayout parent = (LinearLayout) v.getParent();
final TextView pie_stock_view = (TextView) parent.findViewById(current_stock);
final TextView pie_name_view = (TextView) parent.findViewById(pie_name);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(pie_name_view.getText().toString());
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pie_stock_view.setText(input.getText().toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
else if(v.getId()==R.id.resetButton) {
resetDialog();
}
}
该应用程序在模拟器中完美运行 - 当您单击股票编号并弹出框时,您只需使用桌面键盘输入并单击确定,在提交之前更新任意数量的值。
实际设备上的问题是当弹出框并单击它时,屏幕键盘会弹出,我猜这算作离开活动,因为所有更新的值都是重置回它们生成的值,有什么建议可以防止这种情况发生吗?
【问题讨论】:
标签: java android listview android-edittext textview