ArrayAdapter Generic中的错误

时间:2014-07-18 07:34:20

标签: java android

这是我在错误行中看到的 - 此行的多个标记
- ArrayAdapter是原始类型。
- 对泛型类型ArrayAdapter的引用应该参数化 - 构造函数ArrayAdapter(RetrieveActivity.MyAsyncTask,int,ArrayList)是      undefined
我可能知道我的代码有什么问题。我想在列表视图中显示arraylist。但由于错误我无法做到。 TQ

package com.example.m2mai;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


public class RetrieveActivity extends Activity {
    ArrayAdapter mArrayAdapter;
    ArrayList mNameList = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
    }



    public void getStream(View v)
    {   
        new MyAsyncTask().execute();
    }

private class MyAsyncTask extends AsyncTask<String, Void, String>
{
    public ArrayList<String> atList=new ArrayList<String>();
    public ArrayList<String> dataList=new ArrayList<String>();


        protected String doInBackground(String... params) 
        {
            return getData();

        }
        public long getDateTo() 
      {
        EditText toText = (EditText)findViewById(R.id.dateTo);
        String To = toText.getText().toString();

        DateFormat dateFormatTo = new SimpleDateFormat("dd/MM/yyyy");
        Date dateTo = null;
        try {
            dateTo = dateFormatTo.parse(To);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        long timeTo = dateTo.getTime();
        new Timestamp(timeTo);  
        return timeTo/1000;
      }

        protected String getData()
        {
            String toTS = ""+getDateTo();
            String decodedString="";
            String returnMsg="";
            String request = "http://api.carriots.com/devices/defaultDevice@eric3231559.eric3231559/streams/?order=-1&max=5&at_to="+toTS;
            URL url;
            HttpURLConnection connection = null;  
            try {
                url = new URL(request); 
                connection = (HttpURLConnection) url.openConnection();
                //establish the parameters for the http post request
                connection.addRequestProperty("carriots.apikey", "====================");
                connection.addRequestProperty("Content-Type", "application/json");
                connection.setRequestMethod("GET"); 
                //create a buffered reader to interpret the incoming message from the carriots system
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((decodedString = in.readLine()) != null) 
                {
                    returnMsg+=decodedString;
                }
                in.close();
                connection.disconnect();
                JSONObject nodeRoot = new JSONObject(returnMsg); 
                JSONArray res = nodeRoot.getJSONArray("result");

                 for (int i = 0; i < res.length(); i++) 
                    {
                    JSONObject childJSON = res.getJSONObject(i);
                    if (childJSON.get("data")!=null)
                        {
                       String value = childJSON.getString("data");
                       dataList.add(value);
                       JSONObject node=new JSONObject(value);
                       atList.add(node.get("temperature").toString()); 
                        }
                    } 
                 }
                 catch (Exception e) 
            {
                e.printStackTrace(); 
                returnMsg=""+e;
            }
            //Log.d("returnMsg",returnMsg.toString());
            return returnMsg;
        }

        protected void onPostExecute(String result)
            {

//          int a = atList.size();
//          String b = ""+a;
//          Log.d("atList",b);
            for(int i = 0; i < atList.size(); i++)
            {
            ListView mainListView = (ListView) findViewById(R.id.listView1);

            // Create an ArrayAdapter for the ListView
            mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,mNameList); //<---- error here

            // Set the ListView to use the ArrayAdapter
            mainListView.setAdapter(mArrayAdapter);

            mNameList.add(atList.get(i).toString());
            mArrayAdapter.notifyDataSetChanged();
            }
            Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();
            EditText myData1=(EditText)findViewById(R.id.editText1);
            myData1.setText(atList.get(0));

            }
    }

}

3 个答案:

答案 0 :(得分:1)

  

ArrayAdapter是原始类型。

期望您提供适配器将要处理的对象类型。例如,

 ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(... 
  

构造函数ArrayAdapter(RetrieveActivity.MyAsyncTask,int,   的ArrayList)

构造函数的第一个参数是Context对象,而不是AsyncTask的实例(在您的情况下,这是指)

ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(NameOfYoutActivity.this, ...

onPostExecute中,您正在创建适配器的多个实例。我认为这没用“

@Override
protected void onPostExecute(String result) {

        ListView mainListView = (ListView) findViewById(R.id.listView1);
        mArrayAdapter = new ArrayAdapterString> (NameOfYoutActivity.this, android.R.layout.simple_list_item_1,mNameList);

        for(int i = 0; i < atList.size(); i++)  { 
             mArrayAdapter.add(atList.get(i).toString());                  
        }
        Toast.makeText(getApplicationContext(),result, Toast.LENGTH_LONG).show();
        EditText myData1=(EditText)findViewById(R.id.editText1);
        myData1.setText(atList.get(0));

} 

答案 1 :(得分:0)

同时指定指定数组适配器类型的参数以删除此类警告

ArrayAdapter<String>  mArrayAdapter = new ArrayAdapter<String>(RetrieveActivity.this,android.R.layout.simple_list_item_1,mNameList);

同时将arraylist的定义更改为

ArrayList<String> mNameList = new ArrayList<String>();

答案 2 :(得分:0)

// "this" is not application context.
mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mNameList);

您可以使用onCreate()方法创建用户界面,并使用mArrayAdapter.notifyDataSetChanged();进行更新。