我正在使用解析数据填充列表视图,并且我正在查询数据。我想只显示等于来自另一个活动的字符串的行。
我得到了字符串,因为我有一个toast向我显示字符串,但query.whereEqualTo中的字符串是void。
我如何在doInBackground中传递字符串变量的值?
这是我的代码,
公共类ListadoMusica扩展了Activity {
String año; String artista; // Declare Variables ListView listview; List<ParseObject> ob; ProgressDialog mProgressDialog; CustomAdapter adapter; private List<Musica> musica = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from listview_main.xml setContentView(R.layout.activity_listado_musica); año = getIntent().getStringExtra("myaño"); artista = getIntent().getStringExtra("myartista"); Toast.makeText(getApplicationContext(),año, Toast.LENGTH_SHORT).show(); // Execute RemoteDataTask AsyncTask new RemoteDataTask().execute(); } // RemoteDataTask AsyncTask private class RemoteDataTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Create a progressdialog mProgressDialog = new ProgressDialog(ListadoMusica.this); // Set progressdialog title mProgressDialog.setTitle("Cargando..."); // Set progressdialog message mProgressDialog.setMessage("Loading..."); mProgressDialog.setIndeterminate(false); // Show progressdialog mProgressDialog.show(); } @Override protected Void doInBackground(Void... params) { // Create the array musica = new ArrayList<Musica>(); try { // Locate the class table named "Country" in Parse.com ParseQuery<ParseObject> query = new ParseQuery<ParseObject>( "Musica"); // Locate the column named "ranknum" in Parse.com and order list // by ascending //query.orderByAscending("createdAt"); query.whereEqualTo("año", año); query.whereEqualTo("artista", artista); ob = query.find(); for (ParseObject country : ob) { // Locate images in flag column ParseFile image = (ParseFile) country.get("imagen"); Musica map = new Musica(); map.setTitulo((String) country.get("titulo")); map.setArtista((String) country.get("artista")); map.setAño((String) country.get("año")); map.setFoto(image.getUrl()); musica.add(map); } } catch (ParseException e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { // Locate the listview in listview_main.xml listview = (ListView) findViewById(R.id.listview); // Pass the results into ListViewAdapter.java adapter = new CustomAdapter(ListadoMusica.this, musica); // Binds the Adapter to the ListView listview.setAdapter(adapter); // Close the progressdialog mProgressDialog.dismiss(); } }
}
提前致谢,
答案 0 :(得分:1)
修改此行:
new RemoteDataTask().execute(año, artista );
这一行:
private class RemoteDataTask extends AsyncTask<String, Void, Void> {
这一行:
protected Void doInBackground(String... params) {
String año = params[0];
String artista = params[1];
//code
query.whereEqualTo("año", año);
//do whatever you need on artista
答案 1 :(得分:0)
我不完全确定,为什么它在您提供的代码中无效,但是当异步任务在另一个线程上执行时,您不能保证在任务运行时您的活动实例(包括它的内容)仍然有效。 / p>
确保将String año;
设为RemoteDataTask
的实例变量并将其传递给任务构造函数。
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
private String año;
RemoteDataTask(String año) {
super();
this.año = año;
}
...
}
还要确保您的活动正常处理方向更改。见this link