【问题标题】:string variable value in doinbackgrounddoinbackground 中的字符串变量值
【发布时间】:2015-07-19 08:34:43
【问题描述】:

我正在使用解析数据填充列表视图,并且正在查询数据。我只想显示等于来自另一个活动的字符串的行。

我得到了字符串,因为我有一个显示字符串的 toast,但是 query.whereEqualTo 中的字符串是无效的。

如何在 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();
    }
}

}

提前致谢,

【问题讨论】:

    标签: android listview parse-platform android-asynctask


    【解决方案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 
    

    【讨论】:

    • 谢谢,yshahak,它工作得很好,但我也需要查询字符串“artista”。我尝试在查询中使用此字符串和参数 [1] 添加新的 RemoteDataTask,但它不起作用。
    • 可变参数的美丽之处在于您可以引入许多您想要的变量。请参阅我编辑的答案。
    • 关心 eaplin 为什么这应该比 OP 的代码更好?
    • 我可以,但是 AsyncTask 的文档会比我做得更好。您可以更具体地询问,我很乐意尝试回答:developer.android.com/reference/android/os/AsyncTask.html
    • yshahak,编辑答案 año 像以前一样获得价值,但艺术家继续无效。抱歉,不知道怎么解决,谢谢
    【解决方案2】:

    我不太确定,为什么它在您提供的代码中是无效的,但是由于异步任务在另一个线程上执行,您不能保证您的活动实例(包括其内容)在任务运行时仍然有效。

    可以肯定的是,将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;
      }
      ...
    }
    

    还要确保您的 Activity 能够优雅地处理方向变化。见this link

    【讨论】:

    • asynctask 类是一个实例类。如果活动不存在,它本身就不能存在。 (实际上,在某些情况下可能会导致活动泄露)
    • 你说得对,我的解释很糟糕,因为我的措辞很糟糕(充其量)。我对 valid 的意思是,当 doInBackground 被称为 Activity 中的 onDestroy 实现时,它可能已经离开了,但它曾经的辉煌只是一个外壳。问题中没有指出这样的onDestroy 实现,所以很遗憾,我的推理也很糟糕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多