我是Android的新手,并且一直在使用一个简单的库存应用程序并且已经遇到了这个问题:
在我的“设置库存”屏幕上,产品列表和当前库存从数据库中获取并显示在以编程方式生成的2列TextView中。
onCreate方法,它从数据库中获取并将其放入activity_set.xml中的空ListView中:
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();
}
}
该应用程序在模拟器中运行良好 - 当您单击一个库存号并弹出该框时,您只需键入桌面键盘并单击确定,在提交之前更新任意数量的值。
实际设备上的问题是当弹出框并点击它时,屏幕上的键盘会弹出,我猜这会被视为离开活动,因为所有更新的值都是重置回他们生成的值,任何建议如何防止这种情况?。
答案 0 :(得分:0)
您应该将更改后的值存储在数组中。为了首先使用另一个函数来捕获点击并将stock数组的定义移到onCreate之外。因此,您可以将代码更改为:
public class SetActivity extends AppCompatActivity{
private static final String TAG = "SetActivity";
ArrayList<Product> stock = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set);
if(stock == null)
stock = Products.retrieveProducts(this);
final ListView mListView = (ListView) findViewById(setList);
ProductEditAdapter adapter = new ProductEditAdapter(this,R.layout.adapter_set_layout, stock);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
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());
getItem(position).setStock(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();
}
}
});
}
}
另一个重要的事情是尝试使用convertView。它可以防止内存泄漏并提高应用程序的性能。
@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);
if(convertView != null){
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;
}