构造函数是未定义的ArrayAdapter

时间:2014-05-17 04:09:24

标签: android android-arrayadapter

我收到此错误:

The constructor AllProductsActivity.MyAdapter(AllProductsActivity, int, ArrayList<HashMap<String,String>>) is undefined

在这一行:

MyAdapter adapter = new MyAdapter(AllProductsActivity.this, R.layout.list_item, productsList);

我最好的猜测是productsList是问题,但在尝试理解代码大约2天后,我还没弄明白。很多代码来自本教程,老实说我不明白hashmap是什么。 - http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

我知道帮助堆栈溢出的人看起来很无能,但我真的可以用正确的方向推进。 HashMap类概述对我来说就是胡言乱语(http://developer.android.com/reference/java/util/HashMap.html

提前感谢您的帮助。

这是我的整个AllProductsActivity:

public class AllProductsActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http://mywebsite/mycameraapp/android_connect/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";

// products JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_products);

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();

    // on selecting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                    .toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    EditProductActivity.class);
            // sending pid to next activity
            in.putExtra(TAG_PID, pid);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);
        }
    });

}

/** CALLED WHEN THE USER CLICKS THE RECORD BUTTON */
public void sendMessage(View view) {
    Intent videoIntent = new Intent(this, PhotoIntentActivity.class);
    startActivity(videoIntent);
}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received 
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllProductsActivity.this);
        pDialog.setMessage("Loading trends...");
        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>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // Check your log cat for JSON reponse
        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 id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    String description = c.getString(TAG_DESCRIPTION);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_DESCRIPTION, description);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } 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
                 * */
                MyAdapter adapter = new MyAdapter(AllProductsActivity.this, R.layout.list_item, productsList);

                // updating listview
                setListAdapter(adapter);

            }

        });

    }

}

}

我实际上在AllProductsActivity文件中有MyAdapter,但是为了便于查看,我将把它单独放在这里:

public class MyAdapter extends ArrayAdapter<MyItem>{

    Context context;
    int resourceId;
    ArrayList<MyItem> items = null;
    LayoutInflater inflater;

    public MyAdapter (Context context, int resourceId, ArrayList<MyItem> items)
    {
        super(context, resourceId, items);

        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if (convertView == null){

            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.video = (VideoView)convertView.findViewById(R.id.videoView);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder)convertView.getTag();
        }

        MyItem item = items.get(position);
        if (item != null)
        {
            // This is where you set up the views.
            holder.name.setText(TAG_NAME);
            Uri myUri = Uri.parse(TAG_DESCRIPTION);
            holder.video.setVideoURI(myUri);
            holder.video.seekTo(1);
            holder.video.setOnTouchListener(
                    new View.OnTouchListener()
                    {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            holder.video.start();
                            holder.video.requestFocus();
                            return false;
                        }
                    }
                );

        }

        return convertView;
    }

    public class ViewHolder
    {
        TextView    name;
        VideoView   video;
    }
}

1 个答案:

答案 0 :(得分:1)

您正在将productsList传递给ArrayList HashMaps

的构造函数
ArrayList<HashMap<String, String>> productsList;

同样删除runOnUiThread因为在{ui线程本身上调用onPostExecute

你的构造函数必须是。

ArrayList<HashMap<String, String>>  items;
public MyAdapter (Context context, int resourceId, ArrayList<HashMap<String, String>> items)
{
    super(context, resourceId, items);
    this.items =items;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

getView()

HashMap<String,String> item = (HashMap<String,String> ) items.get(position);

设置文字

holder.name.setText(item.get(TAG_NAME));

也改为

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {