gpt4 book ai didi

安卓多微调器

转载 作者:太空狗 更新时间:2023-10-29 14:26:40 26 4
gpt4 key购买 nike

我想开发一个带有三个微调器的 Android 应用程序。这三个旋转器将包含有关汽车的信息,即品牌、型号和引擎。

  1. 因此,在选择第一个微调器(品牌)后,应根据所选品牌添加第二个微调器(型号)的内容。
  2. 接下来,将选择第二个微调器,然后添加第三个微调器(引擎)。

我在 google 和 stackoverflow 上用关键字“Android Multi Spinners”和“Android listener for spinners”搜索了将近一个星期,但我仍然没有找到解决方案。

Here are the links that I got the idea for my code

所以,我有两个版本的源代码,但都不起作用。 (编辑:现在工作)

对于第一个版本

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class AddCarActivity extends Activity {

private Spinner spnBrand, spnModel, spnEngine;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_car);

addItemsOnSpnBrand();
}

public void addItemsOnSpnBrand() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
List<String> list = new ArrayList<String>();

//Get Brand Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnBrand.setAdapter(dataAdapter);

addListenerOnSpnBrandItemSelection();
}

public void addListenerOnSpnBrandItemSelection() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
spnBrand.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
addItemOnSpnModel(parent.getItemAtPosition(pos).toString());
}

public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
}

public void addItemOnSpnModel(String inBrand) {
spnModel = (Spinner) findViewById(R.id.spnModel);
List<String> list = new ArrayList<String>();

//Get Model Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSeries.setAdapter(dataAdapter);

addListenerOnSpnModelItemSelection();
}

public void addListenerOnSpnModelItemSelection() {
spnModel = (Spinner) findViewById(R.id.spnModel);
spnModel.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
addItemOnSpnEngine(parent.getItemAtPosition(pos).toString());
}

public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
}

public void addItemOnSpnEngine(String inModel) {
spnEngine = (Spinner) findViewById(R.id.spnEngine);
List<String> list = new ArrayList<String>();

//Get Engine Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEngine.setAdapter(dataAdapter);
}
}

这是我的第二个版本

public class AddCarActivity extends Activity implements OnItemSelectedListener {

private Spinner spnBrand, spnModel, spnEngine;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_car);

addItemsOnSpnBrand();
}

public void onItemSelected(AdapterView<?> parent, View view, int position,long arg3) {
int id = parent.getId();
switch (id)
{
case R.id.spnBrand:
addItemOnSpnModel(parent.getItemAtPosition(position).toString()); break;
case R.id.spnModel:
addItemOnSpnEngine(parent.getItemAtPosition(position).toString()); break;
}
}

public void onNothingSelected(AdapterView<?> arg0) {
return;
}

public void addItemsOnSpnBrand() {
spnBrand = (Spinner) findViewById(R.id.spnBrand);
List<String> list = new ArrayList<String>();

//Get Brand Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnBrand.setAdapter(dataAdapter);

spnBrand.setOnItemSelectedListener(this);
}

public void addItemOnSpnModel(String inBrand) {
spnModel = (Spinner) findViewById(R.id.spnModel);
List<String> list = new ArrayList<String>();

//Get Model Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnModel.setAdapter(dataAdapter);

spnModel.setOnItemSelectedListener(this);
}

public void addItemOnSpnEngine(String inModel) {
spnEngine = (Spinner) findViewById(R.id.spnEngine);
List<String> list = new ArrayList<String>();

//Get Engine Database and Add to List

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEngine.setAdapter(dataAdapter);
}
}

感谢所有试图帮助我的人。我刚刚在重新测试回答 Hip Hip Array 的代码时发现了问题。我在 addListenerOnSpnModelItemSelection() 中使用了错误的变量。我错误地使用了 spnBrand 而不是 spnModel。现在,这两个版本都可以用了,所以我更正了它们,希望它们能帮助其他尝试在 android 中使用 multi-spinners 的人。

最佳答案

有点啰嗦但试一试

Public class HomePage extends Activity implements OnClickListener, OnItemSelectedListener{

int orientation;
DataBaseHelper myDbHelper;
Cursor c3, c2;
SimpleCursorAdapter adapterData;
int index = 0;
Spinner s[] = new Spinner[5];
TableRow row;

@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myDbHelper = new DataBaseHelper(this);

//-----------Customized Adapter for Specialties----Database binded//
String[] columns2 = {myDbHelper.KEY_ID, myDbHelper.KEY_TITLE};
String table2 = myDbHelper.DB_TABLE_NAME;
c2 = myDbHelper.getHandle().query(table2, columns2, "type = 'SuperTab'", null, myDbHelper.KEY_TITLE, null, myDbHelper.KEY_TITLE);
startManagingCursor(c2);
String[] from = new String[]{myDbHelper.KEY_TITLE};
int[] to = new int[]{android.R.id.text1};
adapterData =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
adapterData.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );


//-----------Customized Adapter for Location-----Database binded--//
String[] columns3 = {myDbHelper.KEY_ID, myDbHelper.KEY_LOCATION};
String table3 = myDbHelper.DB_TABLE_NAME_LOCATION;
c3 = myDbHelper.getHandle().query(table3, columns3, null, null, myDbHelper.KEY_LOCATION, null, null);
startManagingCursor(c3);
String[] from3 = new String[]{myDbHelper.KEY_LOCATION};
int[] to3 = new int[]{android.R.id.text1};
SimpleCursorAdapter adapterData3 =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c3, from3, to3 );
adapterData3.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//--------------------------------------------------------------//

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.specialty_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapterData);

Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(
this, R.array.office_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterData3);

Button search = (Button)this.findViewById(R.id.Search);
search.setOnClickListener(this);

Button searchNearMe = (Button)this.findViewById(R.id.Near_Me);
searchNearMe.setOnClickListener(this);

spinner.setOnItemSelectedListener(this);

orientation = getResources().getConfiguration().orientation;

s[index] = new Spinner(this);
// s[index].setOnItemSelectedListener(this);
row = new TableRow(this);
}

@SuppressWarnings("static-access")

public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
TextView tv = (TextView)this.findViewById(R.id.spinnerText2);


Log.w("ID: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount() + " " + id + " callee " + view);

String sql = "sql" + id ;

c2 = myDbHelper.getHandle().rawQuery(sql, null);
Log.w("ID Adapter: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount());

int thisOrientation = getResources().getConfiguration().orientation;

if (thisOrientation != orientation) {
return;
}

//--------sets hierarchy spinner to corresponding database-----//
String[] from = new String[]{myDbHelper.KEY_TITLE};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapterDataNew =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
adapterDataNew.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Log.w("Adapter Data New", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
//------------------------------------------------------------//

//-----------------Table Row for Landscape mode---------------//

TableLayout tl = (TableLayout)this.findViewById (R.id.Table);
TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tlp.weight = 1;
tlp.setMargins(20, 15, 20, 0);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

//------------------------------------------------------------//

//------------------find Relative Layout by ID----------------//
RelativeLayout relative = (RelativeLayout)this.findViewById(R.id.rlay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.spinner);
p.setMargins(15, 5, 15, 5);
//------------------------------------------------------------//

if (orientation != getResources().getConfiguration().ORIENTATION_LANDSCAPE) {
if (c2.getCount() > 0) {
relative.removeView(s[index]);
relative.addView(s[index], android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
s[index].setVisibility(view.VISIBLE);
s[index].setPromptId(R.string.Sub);
s[index].setAdapter(adapterDataNew);
s[index].setLayoutParams(p);
tv.setPadding(10, 50, 10, 0);

Log.w("Index value ", " " + index);

}
else {
s[index].setVisibility(view.GONE);
relative.removeView(s[index]);
tv.setPadding(0, 0, 0, 0);
}
}
else {
if (c2.getCount() > 0) {
tl.removeView(row);
row.removeView(s[index]);
Log.w("Row Parent " + row.getParent()," tl child " + tl.getChildCount());
tl.addView(row, index + 4 , tlp);
row.addView(s[index]);
Log.w("Row Parent After Add" + row.getParent()," tl child After Add" + tl.getChildCount());
row.setVisibility(view.VISIBLE);
s[index].setVisibility(view.VISIBLE);
s[index].setPromptId(R.string.Sub);
s[index].setAdapter(adapterDataNew);

Log.w("ID: Landscape mode", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
}
else {
s[index].setVisibility(view.GONE);
row.setVisibility(view.GONE);
row.removeView(s[index]);
tl.removeView(row);
}

}

}


public void onNothingSelected(AdapterView<?> arg0) {

}

@Override
public void onClick(View v) {
EditText et1 = (EditText)this.findViewById(R.id.entry);
EditText et2 = (EditText)this.findViewById(R.id.entry2);
String lastName = et1.getText().toString();
String firstName = et2.getText().toString();
if (lastName.length() == 0) {
lastName = "%";
}
if (firstName.length() == 0) {
firstName = "%";
}
Bundle bundle = new Bundle();
bundle.putString("LastName", lastName);
bundle.putString("FirstName", firstName);
switch(v.getId()) {
case R.id.Search:
Intent i = new Intent(this, Search.class);
i.putExtras(bundle);
startActivity(i);
c2.close();
c3.close();
break;
case R.id.Near_Me:
Intent j = new Intent(this, SearchNearMe.class);
startActivity(j);

}
}
}

关于安卓多微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11795271/

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