刷新 TableLayout 以显示插入的新行

Refreshing TableLayout to show new rows inserted

背景:

我在数据库中创建了一个订单 table,我在两个活动中对其进行操作,分别是 CurrentOrdersNewOrders

CurrentOrders activity 显示当前存储在数据库中的订单在TableLayout中。为了向 table 添加新订单,我指定了一个按钮 addOrders,单击该按钮会加载 NewOrders activity.

当前订单:

public class CurrentOrders extends AppCompatActivity {
    
    private TableLayout table;
    populateTable(Cursor records){
           
           TableRow row = new TableRow(this);
           TextView product = new TextView(this);
           TextView price = new TextView(this);        

           product.setText(records.getString(1));        
           price.setText(records.getString(2));          
           row.addView(product);
           row.addView(price);

           table.addView(row);
    } 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_orders);
        
        Cursor cursor = db.query("orders", null, null,
                null, null, null, null);  

        table = findViewById(R.id.tab);             

        while(cursor.moveToNext())                 
           populateTable(cursor);
    }

    public void addOrders(View view){
       startActivity(new Intent(getApplicationContext(), NewOrders.class));        
    } 
}

NewOrders activity 的行为类似于一个表单,其中有一个 TableLayout,其 cells/fields 被指定为 EditText,以便让用户输入他们需要的订单想储存。为了将订单保存在数据库中,我指定了一个提交按钮,单击该按钮时,将 EditText 的值存储在数据库中的订单 table 中,然后返回到父级 activity (CurrentOrders) 显示数据库中的所有订单。

新订单:

public class NewOrders extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_orders);                        
    }

    public void submit(View view){
        
        TableLayout table = findViewById(R.id.newOrders);
        int count = table.getChildCount();
        
        for (int idx = 0; idx < count; idx++){

            TableRow row = (TableRow) table.getChildAt(idx);

            EditText product = (EditText) row.getChildAt(1);
            EditText price = (EditText) row.getChildAt(2);

            String productVal = product.getText().toString();
            double priceVal = Double.parseDouble(price.getText().toString());

            long result = db.insert(productVal, priceVal);           
        }
        Toast.makeText(this, "Saved.", Toast.LENGTH_SHORT).show();
         onBackPressed();
    }
}

问题:

除非我重新加载 CurrentOrders activity,否则我看不到插入的新记录。我想显示所有记录(包括新记录)而无需重新加载 activity.

终于创建了一个简单的解决方案:

首先我们需要修改 CurrentOrderstartActivity 以便它从 NewOrders activity:

接收数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    
    // add newly added orders
    
    if (data != null) {
        String response = data.getStringExtra("newOrders");
        if (requestCode == RequestCode && resultCode == RESULT_OK && !response.isEmpty()){
            String[] orders = response.trim().split(" ");
            for (String order : orders) {
                // get new orders from the database
                Cursor records = db.query("orders", null, "id = ?", 
                                          new String[] {String.valueOf(id)},
                                          null, null, null);
                records.moveToNext();
                populateTable(records);
            }
        }
    }
}

然后在addOrders方法中你需要用下面的代码替换startActivity

startActivityForResult(new Intent(getApplicationContext(), NewOrders.class), 2);

现在来到 NewOrders activity 这里我创建了一个字符串变量来存储新创建订单的 ID:

public class NewOrders extends AppCompatActivity {
    
    // created a string variable
    private String newOrdersId = "";

    // reset of the code

    public void submit(View view){
        
        // rest of the code
        
        for (int idx = 0; idx < count; idx++){
            
            // rest of the code 
            
            // record id's of orders inserted into the database
            newOrderId += result + " "; 
        }
        // reset of the code
    }
}

最后,您需要通过覆盖 onBackPressed 方法将数据发送到 CurrentOrders,如下所示:

@Override
public void onBackPressed(){
    Intent intent = getIntent();
    intent.putExtra("newOrders", newOrdersId);
    setResult(RESULT_OK, intent);
    finish();
}