我有一个应用程序,它使用3个单独的类将信息传递给主活动,它将信息传递给一个数组,然后在输入第一个字母时列出状态。编码似乎都在那里,理论上应该像它应该......但是当我尝试输入一个字母时,我得到的不是显示任何状态列表:
Click here to show what happens on the emulator, weird error
我很确定我需要在状态类中的某处添加一个getName()方法,从我在尝试解决问题时看到的,然后将其调用到主要活动中,但我正在努力到哪里或如何,但我在多类中没有过多经验,并且def不习惯getName()方法。或者,如果有人有另外一个关于如何传递名称而不是整个州级的建议(我认为这是造成问题的原因)我很乐意听到/看到它!
代码如下:
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/button"
android:layout_alignParentEnd="true"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Look for States below"
android:id="@+id/textView"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search States"
android:id="@+id/button"
android:onClick="buttonOnClick"
android:layout_below="@+id/searchBox"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchBox"
android:textSize="35dp"
android:layout_centerHorizontal="true"
android:paddingBottom="15dp"
android:hint="Type a Letter"
android:layout_below="@+id/textView" />
</RelativeLayout>
City.java
public class City {
String name;
int population;
public City () {
}
public City(int pop, String city_name){
name = city_name;
population = pop;
}
public String getName(){
return name;
}
}
capital.java
public class Capital extends City {
public Capital(int pop,String name){
super(pop, name);
}
}
state.java
public class State {
City capital;
String name;
public State (String stateName, String capitalName, int pop){
name = stateName;
Capital cap = new Capital(pop, capitalName);
capital = cap;
}
public boolean startWith(char s) {
return s == name.charAt(0);
}
}
最后是我的mainactivity.java
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Button stateLook;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
EditText searchBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
stateLook = (Button) findViewById(R.id.button);
searchBox = (EditText) findViewById(R.id.searchBox);
};
public void buttonOnClick(View v) {
Button button = (Button) v;
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList states = new ArrayList();
states.add(new State("Alabama", "Montgomery", 205764));
states.add(new State("Alaska", "Juneau", 31275));
states.add(new State("Arizona", "Phoenix", 1445632));
states.add(new State("Arkansas", "Little Rock", 193524));
states.add(new State("California", "Sacramento", 466488));
states.add(new State("Colorado", "Denver", 600158));
states.add(new State("Connecticut", "Hartford", 124775));
states.add(new State("Delaware", "Dover", 36047));
states.add(new State("Florida", "Tallahassee", 181412));
states.add(new State("Georgia", "Atlanta", 420003));
states.add(new State("Hawaii", "Honolulu", 337256));
states.add(new State("Idaho", "Boise", 205671));
states.add(new State("Illinois", "Springfield", 116250));
states.add(new State("India", "Indianapolis", 829718));
states.add(new State("Iowa", "Des Moines", 203433));
states.add(new State("Kansas", "Topeka", 127473));
char s;
s = searchBox.getText().charAt(0);
ArrayList stateArray = new ArrayList();
if (searchBox.getText().length() == 1) {
for (int i = 0; i < states.size(); i++) {
State state = (State) states.get(i);
///char s1 = state.charAt(0);
if (state.startWith(s)) {
stateArray.add(state);
}
}
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stateArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
Toast.makeText(this, "You Selected "+ myText.getText(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
答案 0 :(得分:0)
您看到的错误来自<div><pre>This is a save file for LevelUp.
07
10
20
31
41
59
26
53
54
97</pre><div>
<span></span>
方法。在您的类中重写此项以生成所需的输出。
或更改
toString