我正在开发一个项目,我使用php mysql将android应用程序与数据库连接。我的要求是,布局中有一个微调器,我想在运行时初始化值(值来自db)。
我一直在试用这段代码
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.example.festipedia_logo.details1.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.util.Log;
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 mainpagetry extends Activity {
Button button1;
Button button4;
ArrayAdapter<String> adapter;
String[] city;
String loc;
Spinner spinner;
//String loc;
private static String url_all_products =new Global().getcon()+"loc.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";
private static final String TAG_LOCATION = "location";
private ProgressDialog pDialog;
//String category,location;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
EditText b;int flag=0;
ArrayList<HashMap<String, String>> productsList;
// products JSONArray
JSONArray products = null;
//ListView l;
//Spinner spinner;
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.actionbar);
getActionBar().setBackgroundDrawable(d);
setContentView(R.layout.mainmenu);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
spinner = (Spinner)this.findViewById(R.id.spinner1);
/*city = new String[] { "Mumbai", "Chennai",
"Dubai"};
/*adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,city
);
*/
// spinner.setAdapter(adapter);
// 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();*/
String s=spinner.getSelectedItem().toString();
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("loc", 0);
i.putExtra("category", "Technical");
i.putExtra("location", s);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//new CreateNewProduct().execute();
}
});
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();*/
String s=spinner.getSelectedItem().toString();
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("loc", 0);
i.putExtra("category", "Cultural");
i.putExtra("location", s);
// 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();*/
String s=spinner.getSelectedItem().toString();
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
i.putExtra("loc", 0);
i.putExtra("category", "Sports");
i.putExtra("location", s);
// 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(),
MainActivity.class);
i.putExtra("loc", 1);
// 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(),
MainActivity.class);
i.putExtra("loc", 2);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
//new CreateNewProduct().execute();
}
});
}
public void onBackPressed() {
finish();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(mainpagetry.this);
pDialog.setMessage("Loading festivals. 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>();
//Log.d("test",category);
//Log.d("test1",location);
// 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);
String location = c.getString(TAG_LOCATION);
//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);
map.put(TAG_LOCATION, location);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
//t1.setText("We currently do not have any events for this category at the chosen location");
//flag=1;
}
} 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
* */
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,TAG_LOCATION
);
spinner.setAdapter(adapter);
}
});
}
}
}
但我无法使用从db获得的值初始化微调器。
错误说明:The constructor ArrayAdapter<String>(new Runnable(){}, int, String) is undefined
我也试过在void run方法中使用它:
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter
(
mainpagetry.this, productsList,
R.layout.list_item, new String[]
{
TAG_LOCATION
},
new int[] {
R.id.name
});
spinner.setAdapter(adapter);
}
这里说明:The method setAdapter(SpinnerAdapter) in the type Spinner is not applicable for the arguments (ListAdapter)
请帮忙!!!
编辑:
错误的屏幕截图
数组适配器错误 - The constructor ArrayAdapter<String>(mainpagetry, int, String) is undefined
答案 0 :(得分:1)
您应该像这样构建适配器:
adapter = new ArrayAdapter<String>
(mainpagetry.this,
android.R.layout.simple_spinner_dropdown_item);
注意:原始代码中的this
(Runnable)已更改为this
外部类的实例(您的Activity)。
此外,删除最后一个参数以匹配其中一个ArrayAdapter
构造函数,或者将其作为数组或字符串列表,如下所示:
adapter = new ArrayAdapter<String>
(mainpagetry.this,
android.R.layout.simple_spinner_dropdown_item
Arrays.asList("item1", "item2", "item3"));
答案 1 :(得分:0)
没有ArrayAdapter
构造函数将Runnable
或通用T
(在您的情况下,String
)作为参数。请参阅JavadDoc。
而不是String
,您可能希望使用String
数组或 List
。
另外,正如@kiruwka所述,第一个参数必须是Context
,因此您必须使用mainpagetry.this
或其他Context
代替。