gpt4 book ai didi

android - 在 Activity 中动态查看和隐藏微调器

转载 作者:行者123 更新时间:2023-11-30 02:17:24 25 4
gpt4 key购买 nike

我想动态生成微调器。我想生成的 Spinner 的最大数量是 7。但我想根据我从 json 响应中获得的数据集生成 Spinner。

目前我在下面显示了代码,用于确定要生成多少个微调器。根据下面的代码,MealDealCatCode 包含 L1、L2。所以我想获得 2 的值并仅显示 7 个微调器中的 2 个(隐藏剩余的 5 个微调器)。

如何实现我的任务。任何帮助将不胜感激。

@Override
public void onTaskCompleted(JSONArray responseJson) {

try {
List<String> MainMenuDescription1 = new ArrayList<String>();
List<String> MainMenuDescription2 = new ArrayList<String>();
List<String> MainMenuDescription3 = new ArrayList<String>();
List<String> MainMenuDescription4 = new ArrayList<String>();
List<String> MainMenuDescription5 = new ArrayList<String>();
List<String> MainMenuDescription6 = new ArrayList<String>();
List<String> MainMenuDescription7 = new ArrayList<String>();
List<String> MealDealCatCode = new ArrayList<String>();
int uniqueItemIdCount = 0;
for (int i = 0; i < responseJson.length(); ++i) {
JSONObject object = responseJson.getJSONObject(i);

JSONArray subMenuArray = object
.getJSONArray("MealDealItemEntity");

for (int j = 0; j < subMenuArray.length(); ++j) {
JSONObject subMenuObject = subMenuArray.getJSONObject(j);

Log.i("MealDealCatCode ",
subMenuObject.getString("MealDealCatCode"));
MealDealCatCode.add(subMenuObject
.getString("MealDealCatCode"));
MealDealCatCode = Utils
.removeDuplicatesFromList(MealDealCatCode);

uniqueItemIdCount = MealDealCatCode.size();

if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L1")) {
Log.i("MainMenuDescription",
subMenuObject.getString("MainMenuDescription"));
MainMenuDescription1.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L2")) {

MainMenuDescription2.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L3")) {

MainMenuDescription3.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L4")) {

MainMenuDescription4.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L5")) {

MainMenuDescription5.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L6")) {

MainMenuDescription6.add(subMenuObject
.getString("MainMenuDescription"));

}
if (subMenuObject.getString("MealDealCatCode").trim()
.equals("L7")) {

MainMenuDescription7.add(subMenuObject
.getString("MainMenuDescription"));
}
}

item1SP = (Spinner) findViewById(R.id.sp_item1);
item2SP = (Spinner) findViewById(R.id.sp_item2);
item3SP = (Spinner) findViewById(R.id.sp_item3);
item4SP = (Spinner) findViewById(R.id.sp_item4);
item5SP = (Spinner) findViewById(R.id.sp_item5);
item6SP = (Spinner) findViewById(R.id.sp_item6);
item7SP = (Spinner) findViewById(R.id.sp_item7);

item1SP.setVisibility(View.GONE);
item2SP.setVisibility(View.GONE);
item3SP.setVisibility(View.GONE);
item4SP.setVisibility(View.GONE);
item5SP.setVisibility(View.GONE);
item6SP.setVisibility(View.GONE);
item7SP.setVisibility(View.GONE);




item1SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription1 = Utils
.removeDuplicatesFromList(MainMenuDescription1);
ArrayAdapter<String> dataAdapterOne = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription1);
dataAdapterOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item1SP.setAdapter(dataAdapterOne);
item1SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterOne,
R.layout.contact_spinner_row_nothing_selected, this));

item2SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription2 = Utils
.removeDuplicatesFromList(MainMenuDescription2);
ArrayAdapter<String> dataAdapterTwo = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription2);
dataAdapterTwo
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item2SP.setAdapter(dataAdapterTwo);
item2SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterTwo,
R.layout.contact_spinner_row_nothing_selected, this));

item3SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription3 = Utils
.removeDuplicatesFromList(MainMenuDescription3);
ArrayAdapter<String> dataAdapterThree = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription3);
dataAdapterThree
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item3SP.setAdapter(dataAdapterThree);
item3SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterThree,
R.layout.contact_spinner_row_nothing_selected, this));

item4SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription4 = Utils
.removeDuplicatesFromList(MainMenuDescription4);
ArrayAdapter<String> dataAdapterFour = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription4);
dataAdapterFour
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item4SP.setAdapter(dataAdapterFour);
item4SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterFour,
R.layout.contact_spinner_row_nothing_selected, this));

item5SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription5 = Utils
.removeDuplicatesFromList(MainMenuDescription5);
ArrayAdapter<String> dataAdapterFive = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription5);
dataAdapterFive
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item5SP.setAdapter(dataAdapterFive);
item5SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterFive,
R.layout.contact_spinner_row_nothing_selected, this));

item6SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription6 = Utils
.removeDuplicatesFromList(MainMenuDescription6);
ArrayAdapter<String> dataAdapterSix = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription6);
dataAdapterSix
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item6SP.setAdapter(dataAdapterSix);
item6SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterSix,
R.layout.contact_spinner_row_nothing_selected, this));

item7SP.setOnItemSelectedListener(new MyOnItemSelectedListener());
MainMenuDescription7 = Utils
.removeDuplicatesFromList(MainMenuDescription7);
ArrayAdapter<String> dataAdapterSeven = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,
MainMenuDescription7);
dataAdapterSeven
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
item7SP.setAdapter(dataAdapterSeven);
item7SP.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterSeven,
R.layout.contact_spinner_row_nothing_selected, this));
}
} catch (JSONException e) {
e.printStackTrace();
}

}

最佳答案

我能够通过以下答案实现我想要的,

                for (int a = 0; a < uniqueItemIdCount; a++) {
if (uniqueItemIdCount == 1) {
item1SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 2) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 3) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 4) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 5) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 6) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
item6SP.setVisibility(View.VISIBLE);
}

if (uniqueItemIdCount == 7) {
item1SP.setVisibility(View.VISIBLE);
item2SP.setVisibility(View.VISIBLE);
item3SP.setVisibility(View.VISIBLE);
item4SP.setVisibility(View.VISIBLE);
item5SP.setVisibility(View.VISIBLE);
item6SP.setVisibility(View.VISIBLE);
item7SP.setVisibility(View.VISIBLE);
}
}

关于android - 在 Activity 中动态查看和隐藏微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com