我试图动态添加和删除TableLayout中的行.
布局在xml文件中定义.
我能够成功删除一行,但是当我调用相应的addView命令时没有任何反应.
table = (TableLayout)findViewById(R.id.table);
row = (TableRow)findViewById(R.id.row);
table.removeView(row);
table.addView(row);
Run Code Online (Sandbox Code Playgroud)
这会导致一行被删除,但不会再次添加.
编辑:事实证明,它毕竟是添加,只是在屏幕的底部,而不是在与删除它相同的位置.
我可以通过指定索引将其添加到正确的位置:
table.addView(row,4); // 4 happens to the the row
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何确定行的索引,似乎没有一种方法来实现这一点.有谁知道怎么做?(即如果我不知道指数是4,我怎么能算出来)
编辑:包含XML.这只是有问题的行,它上面和下面还有其他行
<TableRow android:id="@+id/row">
<TextView android:id="@+id/field1"
android:text="testing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:textStyle="bold"
android:textSize="18dip"
/>
<TextView android:id="@+id/field2"
android:padding="3dip"
android:text="test"
android:textSize="18dip"
android:gravity="right"
/>
</TableRow>
Run Code Online (Sandbox Code Playgroud) 当我使用我的脚手架创建rails应用程序添加新的"产品"时,以下行正确添加了新产品
@product = Product.new(params[:product])
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下URL添加新产品时(尝试从java程序中发布数据).
http://localhost:3000/products?serial=555&value=111
Run Code Online (Sandbox Code Playgroud)
产品未创建,但我可以访问"serial"和"value"值,如下所示:
@product = Product.new
@product.serial=params[:serial]
@product.value=params[:value]
@product.save
Run Code Online (Sandbox Code Playgroud)
为了进一步混淆我,如果我使用rails应用程序添加新产品,那么params[:serial]和params[:value]变量都是空的.
有人可以指出我正确的方向.
谢谢
在Android上使用Apache HttpClient,如何使用HttpPost将数据发送到RESTfull Ruby on Rails应用程序。
这是我的控制器:
# POST /products
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
flash[:notice] = 'Product was successfully created.'
format.html { redirect_to(@product) }
format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我的Java代码。我应该在URL名称中传递数据,还是必须将其设置在其他位置?(也许是httpost.setEntity吗?)最终,我将使用JSON,但现在我只想获取它,以便实际上可以在Rails中调用“ create”方法。Rails正在获取POST,但从不执行“ create”方法中的任何代码
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.100:3000/products/new");
HttpResponse response = httpclient.execute(httppost); …Run Code Online (Sandbox Code Playgroud)