0

将列表视图从旧值刷新为新值时出现问题。通过几个小时的搜索,我得到了那个adapter.notifyDataSetChanged(); 必须使用。但我无法理解如何使用它,因为每当我使用它时都会出现错误

“未为 ListAdapter 类型定义方法 notifyDataSetChanged()”

我不确定我是否做对了。请帮忙...

这是我的代码

public class Slider extends Activity implements OnClickListener,
    OnDrawerOpenListener, OnDrawerCloseListener {

Button mCloseButton;
Button mOpenButton;
MultiDirectionSlidingDrawer mDrawer;
ListView list;
TextView t1;
String data, query, is;
Spinner d;
String FILENAME = "http://24.php";
ArrayList<String> pos;
ListAdapter adapter;
ArrayList<HashMap<String, String>> mylist;

HashMap<String, String> map;

static String j_id = null;
static Object j_make = null;
static String j_model = null;
static String j_version = null;
static String j_price = null;
static String j_reg_plc = null;

String ID = "car_id";
String MAKE = "make";
String MODEL = "model";
String VERSION = "version";
String PRICE = "expected_price";
String PLACE_REG = "registration_place";
CheckBox cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10;
Spinner sp1;
int flag = 0;
String drive[] = new String[] { "Select Drive", "Two-Wheel Drive",
        "Four-Wheel Drive", "All-Wheel Drive" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.slider);

    t1 = (TextView) findViewById(R.id.noRec);
    list = (ListView) findViewById(R.id.listView1);
    cb1 = (CheckBox) findViewById(R.id.checkBox1);
    cb2 = (CheckBox) findViewById(R.id.checkBox2);
    cb3 = (CheckBox) findViewById(R.id.checkBox3);
    cb4 = (CheckBox) findViewById(R.id.checkBox4);
    cb5 = (CheckBox) findViewById(R.id.checkBox5);
    cb6 = (CheckBox) findViewById(R.id.checkBox6);
    cb7 = (CheckBox) findViewById(R.id.checkBox7);
    cb8 = (CheckBox) findViewById(R.id.checkBox8);
    cb9 = (CheckBox) findViewById(R.id.checkBox9);
    cb10 = (CheckBox) findViewById(R.id.checkBox10);
    d = (Spinner) findViewById(R.id.drive);

    @SuppressWarnings("unchecked")
    ArrayAdapter spdrv = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, drive);
    d.setAdapter(spdrv);

    t1.setVisibility(View.GONE);
    mylist = new ArrayList<HashMap<String, String>>();

    pos = new ArrayList<String>();

    Bundle bundle = getIntent().getExtras();
    String stuff = bundle.getString("stuff");
    Toast.makeText(getApplicationContext(), "Stuff= |" + stuff + "|",
            Toast.LENGTH_LONG).show();

    if (stuff.contains("null")) {

        t1.setVisibility(View.VISIBLE);
        t1.setText("No Records Found");
        Toast.makeText(getApplicationContext(), "No Records Found!",
                Toast.LENGTH_LONG).show();

    } else {

        load_list(stuff);              //Load list with values

    }

    mCloseButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            flag = 1;
            query = "Select make, model, version, expected_price, registration_place FROM `used_cars` where registration_place ="
                    + "'" + j_reg_plc + "'";
            if (cb1.isChecked()) {
                query = query + " AND transmission ='Yes'";
            }
            if (cb2.isChecked()) {
                query = query + " AND ac ='Yes'";
            }
            if (cb3.isChecked()) {
                query = query + " AND car_lockk ='Yes'";
            }
            if (cb4.isChecked()) {
                query = query + " AND sunroof ='Yes'";
            }
            if (cb5.isChecked()) {
                query = query + " AND window ='Yes'";
            }
            if (cb6.isChecked()) {
                query = query + " AND seats ='Yes'";
            }
            if (cb7.isChecked()) {
                query = query + " AND stearing ='Yes'";
            }
            if (cb8.isChecked()) {
                query = query + " AND player ='Yes'";
            }

            if (cb9.isChecked()) {
                query = query + " AND sound_system ='Yes'";
            }
            if (cb10.isChecked()) {
                query = query + " AND wheels ='Yes'";
            }
            query = query + ";";

            Toast.makeText(getApplicationContext(), "QUERY= " + query,
                    Toast.LENGTH_LONG).show();
            System.out.println("QUERY= " + query);
            startDownload();

            mDrawer.animateClose();
        }
    });

    mOpenButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!mDrawer.isOpened())
                mDrawer.animateOpen();

        }
    });

}

private void startDownload() {

    new AppTask().execute(FILENAME);
}

public class AppTask extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Toast.makeText(getApplicationContext(), "res" + result,
                Toast.LENGTH_LONG).show();
        pos = new ArrayList<String>();

        mylist.clear();
        load_list(result);         //Refresh list with new values

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... params) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://animsinc.com/filter.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    5);
            nameValuePairs.add(new BasicNameValuePair("Qry", query));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();
            is = EntityUtils.toString(entity);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

        return is;
    }

}

@Override
public void onContentChanged() {
    super.onContentChanged();
    mCloseButton = (Button) findViewById(R.id.button_close);
    mOpenButton = (Button) findViewById(R.id.button_open);
    mDrawer = (MultiDirectionSlidingDrawer) findViewById(R.id.drawer);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
public void onDrawerClosed() {
    // TODO Auto-generated method stub
    // demo.setVisibility(View.VISIBLE);
}

@Override
public void onDrawerOpened() {
    // TODO Auto-generated method stub
    // demo.setVisibility(View.GONE);
}

public void load_list(String lt) {

    // list.setAdapter(null);

    Toast.makeText(getApplicationContext(), "Inside load_list", 100).show();
    try {

        JSONArray jArray = new JSONArray(lt.toString());

        for (int i = 0; i < jArray.length(); i++) {
            Toast.makeText(getApplicationContext(),
                    "Inside load_list----FOR", 100).show();
            map = new HashMap<String, String>();
            JSONObject jObject = jArray.getJSONObject(i);
            j_id = jObject.getString(ID);
            j_make = jObject.getString(MAKE);
            j_model = jObject.getString(MODEL);
            j_version = jObject.getString(VERSION);
            j_price = jObject.getString(PRICE);
            j_reg_plc = jObject.getString(PLACE_REG);

            data = j_make + "";
            map.put("make", data);

            data = j_model + "";
            map.put("model", data);

            data = j_version + "";
            map.put("version", data);

            data = j_price + "";
            map.put("price", data);

            data = j_reg_plc + "";
            map.put("place", data);

            mylist.add(map);
            pos.add(j_id);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    list = (ListView) findViewById(R.id.listView1);

    String[] from = new String[] { "make", "model", "version", "price",
            "place" };
    int[] to = new int[] { R.id.text1, R.id.text5, R.id.text3, R.id.text4,
            R.id.text2 };
    adapter = new SimpleAdapter(this, mylist, R.layout.text_adaptr, from,
            to);
    Toast.makeText(getApplicationContext(), "B4 list setAdapter", 100)
            .show();

    // mylist.notifyDataSetChanged();

    list.setAdapter(adapter);

    // ---------------------------------

    int[] colors = { 0, 0xff00ffff, 0 }; // red for the example
    list.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    list.setDividerHeight(4);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String data = pos.get(position) + "";

            Toast.makeText(getApplicationContext(), "data: " + data,
                    Toast.LENGTH_LONG).show();

            // Display_Car dc=new Display_Car();
            // dc.get_cid(data);
            Intent myIntent = new Intent(Slider.this, Buy_View.class);
            Bundle bundle = new Bundle();
            // Add your data to bundle
            bundle.putString("stuff", data);
            // Add the bundle to the intent
            myIntent.putExtras(bundle);
            startActivity(myIntent);

        }
    });
    // ---------------------------------
}

}
4

3 回答 3

0

尝试这个:

list.setAdapter(adapter);
adapter.notifyDatasetChanged();

您必须记住notifyDataSetChanged()仅当适配器内的数据发生更改时才有效,并且它会重新呈现列表。在您的情况下,您在外部更改数据,因此您必须更改适配器内部的数据。

在这种情况下,使用类似的东西

((ItemArrayAdapter)lv.getAdapter()).notifyDataSetChanged() instead of adapter.notifyDataSetChanged()
于 2013-05-13T11:52:02.797 回答
0

SimpleAdapter是一个BaseAdapter。改变

ListAdapter adapter;

BaseAdapter adapter;

或者 SimpleAdapter adapter;

于 2013-05-13T11:53:08.770 回答
0

尝试这个,

您可以按如下方式使用 runOnUiThread 方法。如果您不使用 ListActivity,只需修改代码以获取对 ArrayAdapter 的引用。

runOnUiThread(new Runnable() {
public void run() {
    adapter.notifyDataSetChanged();
 }
});
于 2013-05-13T11:48:37.633 回答