![](/img/trans.png)
[英]How to dynamically set text size of all items in a list view based on database value in Android?
[英]How to set value in text view located inside Toolbar dynamically(Android)
我有單獨的工具欄 xml 文件。下面是我們的代碼
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http:// schemas .android.com/ apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="@color/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="65dip"
android:layout_height="65dip"
android:id="@+id/showevents"
android:layout_gravity="right"
android:textSize="12sp"
android:background="@drawable/cart"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:textColor="#000000"/>
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/showevents"
android:layout_alignRight="@id/showevents"
android:text="11"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold"
android:background="@drawable/badge_circle"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
在這個里面我做了一個按鈕和 textview。我想從我們的活動類中輸入文本視圖的值。 我的活動課如下:
public class ProductDetail extends AppCompatActivity {
ImageView productImage;
TextView productTitle,productPrice,productDescription,quantity,textOne;
Button addToCart,showevents;
NumberPicker numberPicker;
public static final String URLS ="http://192.168.1.8/wordpress/upmeapi/class-woocommerce.php?function=get_product_by_id_api";
RequestObject requestObject;
int count;
public static final String URLL = "http://192.168.1.8/wordpress/upmeapi/class-woocommerce.php?function=add_cart_api";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_product_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
/* final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);*/
textOne = (TextView)findViewById(R.id.textOne);
showevents = (Button)findViewById(R.id.showevents);
showevents.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ProductDetail.this,ViewCart.class);
startActivity(intent);
}
});
productImage = (ImageView)findViewById(R.id.ProductImageView);
productTitle = (TextView)findViewById(R.id.ProductTitle);
productPrice = (TextView)findViewById(R.id.ProductPrice);
productDescription = (TextView)findViewById(R.id.ProductDetails);
quantity = (TextView)findViewById(R.id.product_quantity);
addToCart = (Button)findViewById(R.id.AddToCart);
final long productId = getIntent().getExtras().getLong(
"productId");
System.out.println(productId);
numberPicker = (NumberPicker)findViewById(R.id.TextQuantity);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(10);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
//Display the newly selected number from picker
count = 0;
count = newVal;
}
});
addToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ProductHelper productHelper = new ProductHelper();
productHelper.setProductId(productId);
productHelper.setQuantity(count);
ObjectMapper objectMapper = new ObjectMapper();
String req = objectMapper.writeValueAsString(productHelper);
requestObject = ExceptionRequest.generateRequest(req);
requestObject.setUrl(URLL);
new AddToCartList().execute(requestObject);
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
private class AddToCartList extends AsyncTask<RequestObject, Void, JSONObject> {
@Override
protected JSONObject doInBackground(RequestObject... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(arg0[0], ServiceHandler.POST);
// List<Products> result = new ArrayList<Products>();
Log.d("Response: ", "> " + jsonStr);
JSONObject product = new JSONObject();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
product = jsonObj.getJSONObject("rsBody");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return product;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
try {
if (result!=null)
{
String status = result.getString("status");
int totalCartItem = result.getInt("totalCartItem");
textOne.setText(totalCartItem);
Toast.makeText(ProductDetail.this, status, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return;
}
}
}
在我們的 activity.xml 文件中,我包含這樣的工具欄
<include layout="@layout/toolbar"/>
但是當我在textOne
變量中設置值時,我收到了這樣的錯誤
E/AndroidRuntime: android.content.res.Resources$NotFoundException: String resource ID #0xa
11-04 18:48:54.643 19770-19770/app.com.myfirstshoppingcart E/AndroidRuntime: at android.content.res.Resources.getText(Resources.java:312)
11-04 18:48:54.643 19770-19770/app.com.myfirstshoppingcart E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:4417)
如何設置值?
用
int totalCartItem = result.getInt("totalCartItem");
textOne.setText(String.valueOf(totalCartItem));
代替
int totalCartItem = result.getInt("totalCartItem");
textOne.setText(totalCartItem);
TextView覆蓋了setText(int resid)
,該方法將嘗試獲取指定整數的資源。 如果未找到資源 ID,則結果為Resources$NotFoundException
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.