我正在开发一个Android应用程序。我能够将数据库中的值显示到列表视图中,但我想将它们显示在微调器而不是列表视图中。这是我为listview编写的代码
package com.example.festipedia_logo;
//import com.actionbarsherlock.app.SherlockFragment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.festipedia_logo.details2.LoadAllProducts;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class mainpageact extends SherlockFragmentActivity {
Button button1;
Button button4;
ArrayAdapter<String> adapter1;
// String[] city;
String loc;
Spinner spinner;
String location;
ArrayAdapter<String> adapter;
String[] city;
// Progress Dialog
private ProgressDialog pDialog;
String az;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
EditText b;
// ArrayList<HashMap<String, String>> productsList;
ArrayList<String> productsList;
// url to get all products list
private static String url_all_products = "http://192.168.0.104:8080/festipedia/sports.php";
Button a;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_NAME = "eventname";
// products JSONArray
JSONArray products = null;
ListView l;
// Spinner spinner;
//String loc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
//getSupportActionBar().setIcon(R.drawable.fest);
//getSupportActionBar().setLogo(R.drawable.fest);
Drawable d=getResources().getDrawable(R.drawable.a5);
getActionBar().setBackgroundDrawable(d);
setContentView(R.layout.mainmenu);
spinner = (Spinner)this.findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
productsList = new ArrayList<String>();
// productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
location=spinner.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
// View rootView = inflater.inflate(R.layout.mainmenu, container, false);
//Intent in = getIntent();
//loc = in.getStringExtra("loc");
Button button1 = (Button) findViewById(R.id.button1);
Button button4 = (Button) findViewById(R.id.button4);
Button button2 = (Button) findViewById(R.id.button2);
Button button5 = (Button) findViewById(R.id.button5);
Button button3 = (Button) findViewById(R.id.button3);
//TextView t1= (TextView) findViewById(R.id.getlocation);
//t1.setText(loc);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
/*final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,new home1() , "Fest Content");
ft.commit();*/
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//new CreateNewProduct().execute();
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
/* final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,new testing() , "Fest Content");
ft.commit();*/
Intent i = new Intent(getApplicationContext(),
testing.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//new CreateNewProduct().execute();
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
/* final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,new testing() , "Fest Content");
ft.commit();*/
Intent i = new Intent(getApplicationContext(),
search.class);
startActivity(i);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
/*final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,new home1() , "Fest Content");
ft.commit();*/
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//new CreateNewProduct().execute();
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
/*final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,new home1() , "Fest Content");
ft.commit();*/
Intent i = new Intent(getApplicationContext(),
CopyMainActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("location", location);
// starting new activity and expecting some response back
startActivityForResult(i, 100);
//startActivity(i);
//new CreateNewProduct().execute();
}
});
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(mainpageact.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("location", az));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
// String name = c.getString(TAG_NAME);
// city[i] = name;
String name = c.getString(TAG_NAME);
productsList.add(name);
//l.setFilterText(id);
// creating new HashMap
// HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_NAME, name);
// adding HashList to ArrayList
// productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
// city = new String[] { "Mumbai", "Chennai",
// "Dubai"};
// adapter = new ArrayAdapter<String>(mainpageact.this, R.layout.list_item, productsList);
//ArrayAdapter<CharSequence>
adapter = new ArrayAdapter<String>(mainpageact.this,R.layout.mainmenu, productsList);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
//spinner.setAdapter(adapter);
}
});
}
}
}
答案 0 :(得分:0)
在notifyDataSetChanged()
加载Spinner
后,您似乎没有致电onPostExecute()
。这将告诉您的适配器需要刷新微调器的数据。
所以,基本上试试这个:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
// city = new String[] { "Mumbai", "Chennai",
// "Dubai"};
adapter = new ArrayAdapter<String>(mainpageact.this, productsList,
R.layout.list_item, new String[] {
TAG_NAME},
new int[] { R.id.name });
adapter.notifyDataSetChanged();
}
});
}
注意:当您将spinner
设置为adapter
上的OnCreate()
时,此时适配器为null
。尝试在onPostExecute()
内完成。另外,尽量避免在Thread
内使用AsyncTask
。这似乎没必要,但我可能错了。
答案 1 :(得分:0)
您的代码中存在三个问题。
1)将productsList声明更改为ArrayList(我认为不需要使用HashMap)
所以改变这一行
ArrayList<HashMap<String, String>> productsList;
到
ArrayList<String> productsList;
并将productsList实例化为
productsList = new ArrayList<String>();
2)在doInBackground()中,将响应字符串项直接添加到productsList。
String name = c.getString(TAG_NAME);
productsList.add(name);
3)现在主要问题是,将适配器更改为
adapter = new ArrayAdapter<String>(this,R.layout.list_item, productsList);
spinner.setAdapter(adapter);