我遇到变量问题:URL_STRING。我似乎无法将另一个变量的值添加到URL_String。由于某种原因它只是空的。非常感谢您的协助。
public class HomeScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ListView listView;
Spinner spinner;
EditText search;
Button bt;
String URL_STRING;
String entity;
String term;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
listView = (ListView) findViewById(R.id.listView);
spinner = (Spinner) findViewById(R.id.spinner);
search = (EditText) findViewById(R.id.search);
term = search.getText().toString();
bt = (Button) findViewById(R.id.searchBtn);
ArrayAdapter<CharSequence> entityAdapter = ArrayAdapter
.createFromResource(this, R.array.Entities,
android.R.layout.simple_spinner_item);
spinner.setAdapter(entityAdapter);
spinner.setOnItemSelectedListener(this);
}
这是我遇到麻烦的地方。 URL_STRING仍为空
public void addURL(View view) {
if(entity == "Select" && term != null){
URL_STRING = String.format(getResources().getString(R.string.url1),term);
SearchDownloader searchDownloader = new SearchDownloader(this);
searchDownloader.execute();
listView.setVisibility(View.VISIBLE);
}else if(term == null){
Toast.makeText(getApplicationContext(), "Please enter a search term",
Toast.LENGTH_LONG).show();
}else{
URL_STRING = String.format(getResources().getString(R.string.url2), term, entity);
SearchDownloader searchDownloader = new SearchDownloader(this);
searchDownloader.execute();
listView.setVisibility(View.VISIBLE);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
entity = String.valueOf(spinner.getSelectedItem());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
entity = null;
}
}
答案 0 :(得分:0)
我不太确定适配器和分配给它的数组。
我已经创建了一些正在寻找的东西,请试试这个,代码可以在Github上找到,如果我可能遗漏了某些内容,请查看它。加载imgae
我正在使用名为library
的{{1}},您可以通过将以下代码添加到picasso
文件中的依赖项块来轻松导入
build.gradle
你可以使用毕加索
compile 'com.squareup.picasso:picasso:2.5.2'
activity_spinner_test_two.xml
public class SpinnerTestTwoActivity extends Activity {
private Spinner url_Spinner;
private Button load_image_Button;
private ImageView url_image_ImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_test_two);
initializeUI();
}
private void initializeUI() {
url_Spinner = (Spinner) findViewById(R.id.SpinnerTestTwoActivity_url_spinner);
load_image_Button = (Button) findViewById(R.id.SpinnerTestTwoActivity_load_image_button);
url_image_ImageView = (ImageView) findViewById(R.id.SpinnerTestTwoActivity_image_from_url_imageView);
String[] url_array = getResources().getStringArray(R.array.url_images);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_spinner_dropdown_item, url_array);
url_Spinner.setAdapter(adapter);
url_Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String image_url = (String)url_Spinner.getItemAtPosition(position);
Picasso.with(getApplicationContext()).load(image_url).into(url_image_ImageView);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
url_images
这是您将在<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:gravity="center"
android:orientation="vertical">
<Spinner
android:id="@+id/SpinnerTestTwoActivity_url_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
<Button
android:id="@+id/SpinnerTestTwoActivity_load_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Load Image"
android:textAllCaps="false" />
<ImageView
android:id="@+id/SpinnerTestTwoActivity_image_from_url_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="4dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
</LinearLayout>
目录中的array
文件中生成的arrays.xml
res/values
输出