我有3个微调器,它们相互依赖。单击第一个微调器将显示第二个微调器,单击第二个微调器将显示第三个微调器,它们位于一个布局内。在布局的底部有2个按钮。一个用于添加布局,另一个用于获取所有微调器数据。假设如果我单击添加按钮3次,则意味着将添加3个具有3个从属微调器的布局。每个不同的布局都有关闭按钮,如果用户单击该按钮,则那个特定的布局将被删除...我如何在android中实现这一目标...
我已经在sqlite中建立了数据库并也获取了数据,但是无法从动态添加的布局微调器中获取正确的数据。
java
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AddMorePage extends AppCompatActivity {
int addMoreCounter = 0;
LinearLayout linearLayout1;
ArrayList<String> modList;
ArrayList<String> subModList;
ArrayList<String> questionList;
ArrayList<String> moduleIdList;
Spinner spinner;
Spinner spinners;
ArrayList<Spinner> modSpinnerList;
ArrayList<Spinner> subModSpinnerList;
ArrayList<Spinner> qnSpinnerList;
ArrayList<String> moduleNameList;
UserDb userDb;
SQLiteDatabase sqLiteDatabase;
ArrayList<HashMap> moduleMap;
ArrayList<HashMap> subModuleByIdMap;
ArrayList<String> subModuleByIDNameList;
ArrayList<String> subModuleByIdIdList;
ArrayAdapter<String> smoduleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_more_page);
userDb = new UserDb(getApplicationContext());
sqLiteDatabase = userDb.getReadableDatabase();
modList = new ArrayList<>();
subModList = new ArrayList<>();
questionList = new ArrayList<>();
linearLayout1 = findViewById(R.id.normalLayout);
//for spinner list
modSpinnerList = new ArrayList< >();
subModSpinnerList = new ArrayList< >();
qnSpinnerList = new ArrayList< >();
//for data arraylist
moduleNameList = new ArrayList< >();
moduleIdList = new ArrayList< >();
subModuleByIDNameList = new ArrayList< >();
subModuleByIdIdList = new ArrayList< >();
moduleMap = userDb.getModuleData(sqLiteDatabase);
for (HashMap<String, String> moduleM : moduleMap) {
String moduleName = moduleM.get(UserDetail.Module.MODULE_NAME);
String moduleId = moduleM.get(UserDetail.Module.MODULE_ID);
moduleNameList.add(moduleName);
moduleIdList.add(moduleId);
Log.d("MODULENAME", moduleName);
Log.d("MODULEID", moduleId);
}
}
public void add(View view) {
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
70, 70);
params.setMargins(0, 30, 30, 0);
params.gravity = Gravity.END;
modList.clear();
subModList.clear();
questionList.clear();
addMoreCounter++;
Log.d("countervalue", String.valueOf(addMoreCounter));
modList.add("sanjeev1");
modList.add("sanjeev2");
modList.add("sanjeev3");
modList.add("sanjeev4");
subModList.add("sanjeevsub1");
subModList.add("sanjeevsub2");
subModList.add("sanjeevsub3");
subModList.add("sanjeevsub4");
questionList.add("sanjeevqn");
questionList.add("sanjeevqn");
questionList.add("sanjeevqn");
questionList.add("sanjeevqn");
LinearLayout linearLayout = new LinearLayout(AddMorePage.this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// creating LayoutParams
LinearLayout.LayoutParams paramsMain =
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Button btnAddARoom = new Button(AddMorePage.this);
//btnAddARoom.setText("Close");
//btnAddARoom.setBackground(R.drawable.ic_lock);
btnAddARoom.setBackgroundResource(R.drawable.ic_highlight_off);
btnAddARoom.setLayoutParams(params);
spinner = new Spinner(AddMorePage.this);
spinners = new Spinner(AddMorePage.this);
Spinner spinnerq = new Spinner(AddMorePage.this);
// spinner adapter
ArrayAdapter<String> moduleAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, moduleNameList);
// ArrayAdapter<String> smoduleAdapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_spinner_dropdown_item, subModList);
ArrayAdapter<String> qAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, questionList);
spinner.setAdapter(moduleAdapter);
//spinners.setAdapter(smoduleAdapter);
spinnerq.setAdapter(qAdapter);
linearLayout.addView(btnAddARoom);
linearLayout.addView(spinner);
linearLayout.addView(spinners);
linearLayout.addView(spinnerq);
linearLayout.addView(v);
linearLayout1.addView(linearLayout);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String text = moduleNameList.get(i);
Log.d("SPINNERDATA", text);
// Log.d("SPINNERSIZE", String.valueOf(modSpinnerList.get(i)));
subModuleByIDNameList.clear();
subModuleByIdIdList.clear();
// Your code goes gere
String staticSelectedModuleName = adapterView.getItemAtPosition(i).toString();
String staticSelectedModuleId = moduleIdList.get(i);
Constant.GLOBAL_MODULE_ID = staticSelectedModuleId;
Log.d("SELECTDMODULE",staticSelectedModuleName);
Log.d("SELECTEDMODULEID",staticSelectedModuleName);
subModuleByIdMap = userDb.getSubModuleByIdData(staticSelectedModuleId ,sqLiteDatabase);
for (HashMap<String, String> subModIDM : subModuleByIdMap) {
String subModNameById = subModIDM.get(UserDetail.SubModule.SUBMODULE_NAME);
String subModIdById = subModIDM.get(UserDetail.SubModule.SUBMODULE_ID);
subModuleByIDNameList.add(subModNameById);
subModuleByIdIdList.add(subModIdById);
Log.d("SUBMODULENAMEBYID", subModNameById);
Log.d("SUBMODULEIDBYID", subModIdById);
}
}
}
}
答案 0 :(得分:0)
如果要实现动态布局,可以查看回收站视图。您可以创建多个布局项目并可以在回收视图适配器类中单独访问它们。
https://developer.android.com/guide/topics/ui/layout/recyclerview