![](/img/trans.png)
[英]How to add OnclickListener() for a Button within a Inflated View?
[英]How to add an id to an inflated view?
我通過使用布局inflater動態地為我的布局添加視圖,但我正在嘗試為每個添加的新視圖添加一個id,以便我可以使用getter來提取信息。
我也試圖更改每個視圖中添加的新微調器中的數組數據,因為它當前顯示的是默認字符串數組,而不是我從數據庫中讀取的數組
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
getdata2();
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
Log.d(String.valueOf(rowView.getId()), "onAddField: ");
getdata2();
}
private void getdata2() {
StringRequest stringRequest = new StringRequest("http://.../getDataCategories.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(JSON_ARRAY);
catdetails(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void catdetails(JSONArray j) {
for (int i = 0; i < j.length(); i++) {
try {
JSONObject json = j.getJSONObject(i);
arrayList2.add(json.getString(CategoryType_idArray));
} catch (JSONException e) {
e.printStackTrace();
}
}
type2_workout_mySpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arrayList2));
}
您可以在視圖對象中添加id作為標記查看rowView = inflater.inflate(R.layout.field,null); rowView.setTag(1);
將number視為您的視圖ID並將此id作為getTag()獲取。
您還可以使用上面的內容更新陣列數據和微調器更新。 就像使用parentLinearLayout.getchildAt()從parentLinearLayout中獲取一個子項一樣,然后從視圖中獲取微調器並將再次設置的適配器放入微調器中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.