我的listview在屏幕上加载多次相同的数据

时间:2013-10-24 11:33:36

标签: android

我的listview重复数据一段时间快速点击按钮我该怎么办请帮我看看这张图片http://imgur.com/ed5uDtp一段时间之后显示http://imgur.com/jAt4yn7 在listview上显示正确的数据,但有时候点击快速按钮加载重复数据我将如何修复此问题? plaa帮帮我

          public class thirdstep extends Activity implements View.OnClickListener {
 int count = 0;


String id;
String title;
String tmpString, finaldate;
String valll;


ProgressBar prgLoading;
TextView txtAlert;

int IOConnect = 0;


String mVal9;


Button e01;
Button e02;
Button e03;
Button e04;
Button e05;


String SelectMenuAPI;
String url;


String URL;
String URL2, URL3, URL4;
String menu_title;

JSONArray school;


ListView listCategory;

String status;

String School_ID;
String  Menu_ID;


String School_name;
String Meal_groupid;
String _response;
String _response2;
String CategoryAPI;
String SelectMenuAPI2;

TextView menu_nametxt;
thirdstepAdapter cla;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> school_name = new ArrayList<String>();
static ArrayList<String> menu_name = new ArrayList<String>();

static ArrayList<String> dish_name = new ArrayList<String>();
static ArrayList<String> dish_ID = new ArrayList<String>();
static ArrayList<String> day = new ArrayList<String>();

static ArrayList<Long> Vacation_ID = new ArrayList<Long>();
static ArrayList<String> Vacation_name = new ArrayList<String>();
static ArrayList<String> Vacation_Date = new ArrayList<String>();

String mydate;
String mode;
String s2;


ArrayList<String> myList,myList2;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_list2);



    listCategory = (ListView) findViewById(R.id.thirdscreenlist);

    prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
    txtAlert = (TextView) findViewById(R.id.txtAlert);


    e01 = (Button) findViewById(R.id.e01);
    e02 = (Button) findViewById(R.id.e02);
    e03 = (Button) findViewById(R.id.e03);
    e04 = (Button) findViewById(R.id.e04);
    e05 = (Button) findViewById(R.id.e05);

    e01.setOnClickListener(this);
    e02.setOnClickListener(this);

    e03.setOnClickListener(this);
    e04.setOnClickListener(this);
    e05.setOnClickListener(this);


    cla = new thirdstepAdapter(thirdstep.this);

    listCategory.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub


            Intent intent = new Intent(thirdstep.this,  fifthscreen.class);

            startActivity(intent);



        }
    });


    new getDataTask().execute();
}


void clearData() {

    Category_ID.clear();
    school_name.clear();
    menu_name.clear();

    dish_name.clear();
    dish_ID.clear();
    day.clear();

    Vacation_ID.clear();
    Vacation_name.clear();
    Vacation_Date.clear();

 }

 public class getDataTask extends AsyncTask<Void, Void, Void> {

    getDataTask() {
        if (!prgLoading.isShown()) {
            prgLoading.setVisibility(0);
            txtAlert.setVisibility(8);

        }
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        parseJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        prgLoading.setVisibility(8);

        if ((Category_ID.size() > 0) || IOConnect == 0) {

            listCategory.setAdapter(cla);
            cla.notifyDataSetChanged() ;
            listCategory.invalidateViews();



        } else {
            txtAlert.setVisibility(0);
            menu_nametxt.setText("");
            listCategory.setVisibility(View.GONE);
        }
    }
}

public void parseJSONData() {
    clearData();

    SelectMenuAPI="";
    SelectMenuAPI = Utils.Schoolmenu +Menu_ID+"&sid="+School_ID+"&lid=" +  
             SchoolLevelId+"&mealid="+Meal_groupid;

    URL = SelectMenuAPI;
     URL2 = URL.replace(" ", "%20");


    try {

           Log.i("url",""+URL2);

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(URL2);
        HttpResponse response = client.execute(request);

        HttpEntity resEntity = response.getEntity();
          _response=EntityUtils.toString(resEntity);


        JSONObject json5 = new JSONObject(_response);

        status = json5.getString("status");
        if (status.equals("1")) {

        JSONArray school5 = json5.getJSONArray("data");



        }
        }
        else {

        }
        SelectMenuAPI2="";
    SelectMenuAPI2 = Utils.SchoolVacation+mVal9;



        //  clearData();
            URL3 = SelectMenuAPI2;
            URL4 = URL3.replace(" ", "%20");

             Log.i("url",""+URL4);









        JSONObject json2 = new JSONObject(_response);

        status = json2.getString("status");
        if (status.equals("1")) {

            if (Vacation_Date.contains(mydate)) {
                message = "holiday";





            JSONObject json4 = new JSONObject(str2);

                status = json4.getString("status");
                if (status.equals("1")) {

                     school = json4.getJSONArray("data");

                    for (int k = 0; k < school.length(); k++) {
            JSONObject jb = (JSONObject) school .getJSONObject(k);
                        Vacation_ID.add((long) k);

String[] mVal = new String[school.length()];

       if(school.getJSONObject(k).getString("date").equals(mydate))
                        {

mVal[k] = school.getJSONObject(k).getString("title");

mVal3 = mVal[k];
}


}

}

} else {

JSONArray school = json2.getJSONArray("data");

for (int i = 0; i < school.length(); i++) {
JSONObject object = school.getJSONObject(i);


if (object.getString("Schedule").equals("weekly")) {

if (object.getString("day").equals(Todayday)) {
Category_ID.add((long) i);
school_name

         .add(object.getString("school_name"));

           dish_ID.add(object.getString("dish_id"));


          dish_name.add(object.getString("dish_name"));


            menu_name.add(object.getString("menu_title"));

    day.add(object.getString("day"));
    count = count + 1;



    String[] mVal = new String[school.length()];

    for (int k = 0; k < school.length(); k++) {
  mVal[k] = school.getJSONObject(k).getString("menu_title");
 message = "weekly";
 mVal2 = mVal[0];



}

}


if(dish_name != null &&  
          !dish_name.isEmpty()) 
     {
     message = "weekly";
       }

    else {
    message = "error";


    }

    } 

    else {
        message = "error";


    }

        }

            }

        }



        else {
            message = "error";


        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


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

    case R.id.e01:
        // do stuff;


        listCategory.setVisibility(View.GONE);



        new getDataTask().execute();

        break;

    case R.id.e02:
        // do stuff;
        listCategory.setVisibility(View.GONE);

        new getDataTask().execute();

        break;
    case R.id.e03:
        // do stuff;
        listCategory.setVisibility(View.GONE);

        new getDataTask().execute();

        break;

    case R.id.e04:
        // do stuff;
        listCategory.setVisibility(View.GONE);

        new getDataTask().execute();

        break;
    case R.id.e05:
        listCategory.setVisibility(View.GONE);
        // do stuff;

        new getDataTask().execute();

        break;

    }

}


   }

2 个答案:

答案 0 :(得分:0)

试试这个..

只需删除以下行并尝试..

listCategory.invalidateViews();

因为

ListView.invalidateViews()用于告诉ListView使其所有子项视图无效(redraw them)。请注意,不需要与项目相同数量的视图。这是因为ListView会回滚其项目视图,并在您滚动时以智能方式在屏幕上移动它们。

答案 1 :(得分:0)

你正在调用asynctask两次,这是两次所有部件,我的意思是你在填充之前清洗两次并填充数组两次。您应该控制异步任务,以便在最后一个完成之前执行。

1 - 创建一个布尔值

onClicks上的2-Put条件:

if(yourBoolean){
new getDataTask().execute();}
你的asyncTask的onPreExecute中的

3- make yourBoolean = false而onPostExecute再次使yourBoolean = true。