0

我应该把 ArrayAdapter 放在哪里?我是新手。我收到一条错误消息:构造函数 ArrayAdapter(new Runnable(){}, int, int, ArrayList>)。必须编写与带有 SimpleArray 的注释部分等效的代码。

public class SearchEventsActivity extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;
EditText inputSearch;


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

ArrayList<HashMap<String, String>> eventsList;
ListView lv;

// url to get all events list
private static String url_all_events = "http://10.0.2.2/indiJam/search_all_events.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_EVENTS = "events";
private static final String TAG_NAME = "event_name";
private static final String TAG_DATE = "date";
private static final String TAG_LOCATION = "location";
private static final String TAG_CATEGORY = "category";

// products JSONArray
JSONArray events = null;
ArrayAdapter<String> adapter;


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

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> eventsList;






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


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

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SearchEventsActivity.this);
        pDialog.setMessage("Loading events...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All events 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_events, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Events: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // events found
                // Getting Array of Events
                events = json.getJSONArray(TAG_EVENTS);

                // looping through All Products
                for (int i = 0; i < events.length(); i++) {
                    JSONObject c = events.getJSONObject(i);

                    // Storing each json item in variable

                    String name = c.getString(TAG_NAME);
                    String date = c.getString(TAG_DATE);
                    String location = c.getString(TAG_LOCATION);
                    String category = c.getString(TAG_CATEGORY);
                    // URL decoding
                    name = name.replace("+", " ");
                    name = name.replace("%27", "\'");

                    category = category.replace("+", " ");
                    category = category.replace("%27", "\'");

                    location = location.replace("+", " ");
                    location = location.replace("%27", "\'");
                    // Adding headers
                    date = "On: " + date;
                    location = "At: " + location;
                    category = "Under: " + category;

                    // 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_DATE, date);
                    map.put(TAG_LOCATION, location);
                    map.put(TAG_CATEGORY, category);

                    // adding HashList to ArrayList
                    eventsList.add(map);
                }
            } 
            else 
            {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        CreateEventActivity.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
                 * */
                /*
                ListAdapter adapter = new SimpleAdapter(
                        SearchEventsActivity.this, eventsList,
                        R.layout.list_item, new String[] {
                                TAG_NAME,TAG_DATE,TAG_LOCATION,TAG_CATEGORY},
                        new int[] {R.id.name,R.id.date,R.id.location,R.id.category });
                // updating listview
                setListAdapter(adapter);
                */
                //adapter = new ArrayAdapter<String>(SearchEventsActivity.this, R.layout.list_item, R.id.name, eventsList);

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                          R.layout.list_item, R.id.name, eventsList);

                inputSearch = (EditText) findViewById(R.id.inputSearch);
                inputSearch.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        // When user changed the Text
                        ((ArrayAdapter<?>)SearchEventsActivity.this.adapter).getFilter().filter(cs);   
                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                            int arg3) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub                          
                    }
                });

                }
            });

        }

    }
}
4

1 回答 1

1

删除runOnUiThread并使用 ActivityName.this。onPostExecute在 ui 线程上调用。无需使用runOnuiThread.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(ActivityName.this,
                      R.layout.list_item, R.id.name, eventsList);

检查主题 4个步骤@

http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-11-12T15:52:50.327 回答