0

我有一个以 JSON 响应的 API。当我单击一个按钮时,我可以轻松地解析 JSON 并在列表视图中实现它。问题是当我回到上一个活动并再次单击按钮时,我得到的值重复。即以前的值,即使我调用了finish(); 处于 listview 活动的暂停状态。以下是我的代码。

发票类

public class Invoice extends Activity{

public static ArrayList<HashMap<String, String>> ModuleName = new ArrayList<HashMap<String, String>>();

boolean tree = true;
ArrayList<String> KEY = new ArrayList<String>();
ListView mListView;
ProgressBar mProgress;
JSONObject json;
JSONArray Module_list = null;
Invoice_adapter adapter;

    public static String KEY_SUCCESS = "success";
    public static String KEY_ERROR = "error";
    public static String KEY_ERROR_MSG = "error_msg";
    public static String KEY_MODULELIST_MODULEHEADER =  "moduleHeader";
    public static String KEY_MODULELIST_MODULELIST =  "moduleList";
    public static String KEY_MODULELIST_DATAFOUND =  "datafound";


    public static String KEY_INVOICE_INVOICENO =  "";
    public static String KEY_INVOICE_SUBJECT =  "";
    public static String KEY_INVOICE_SALESORDER =  "";
    public static String KEY_INVOICE_STATUS =  "";
    public static String KEY_INVOICE_TOTAL =  "";
    public static String KEY_INVOICE_ASSIGNEDTO =  "";
    public static String KEY_INVOICE_RECORDID =  "";
    public static String KEY_INVOICE_ACTION =  "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_module_data);

    mListView = (ListView) findViewById(R.id.module_list);
    mProgress = (ProgressBar) findViewById(R.id.progressBar1);


    try {
        json = new ParseDATA().execute().get();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ExecutionException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS); 
            if(Integer.parseInt(res) == 1){

                String data_found = json.getString(KEY_MODULELIST_DATAFOUND);

                if (data_found.equals("Data Found!")){




                JSONArray array = json.getJSONArray(KEY_MODULELIST_MODULELIST);

                int count1 = array.length();
                String Count1 = Integer.toString(count1);
                Log.d("Count_array", Count1);

                JSONObject keyarray = array.getJSONObject(0);
            @SuppressWarnings("rawtypes")
            Iterator temp = keyarray.keys();
                    while (temp.hasNext()) {
                                String curentkey = (String) temp.next();
                                KEY.add(curentkey);


                    }


                    Collections.sort(KEY, String.CASE_INSENSITIVE_ORDER);

                    KEY_INVOICE_ACTION =  KEY.get(0);
                     KEY_INVOICE_ASSIGNEDTO =  KEY.get(1);
                     KEY_INVOICE_INVOICENO =  KEY.get(2);
                     KEY_INVOICE_RECORDID =  KEY.get(3);
                     KEY_INVOICE_SALESORDER =  KEY.get(4);
                     KEY_INVOICE_STATUS =  KEY.get(5);
                     KEY_INVOICE_SUBJECT =  KEY.get(6);
                     KEY_INVOICE_TOTAL =  KEY.get(7);



                    Log.d("Parsing Json class", "  ---- KEYS---- " + KEY);


                Module_list = json.getJSONArray(KEY_MODULELIST_MODULELIST);

                int count = Module_list.length();
                String Count = Integer.toString(count);
                Log.d("Count_Module_LIST", Count);

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

                    // Storing each json item in variable
                    String mAction = c.getString(KEY_INVOICE_ACTION);
                    String mAssignedTo = c.getString(KEY_INVOICE_ASSIGNEDTO);
                    String mInvoice = c.getString(KEY_INVOICE_INVOICENO);
                    String mRecordid = c.getString(KEY_INVOICE_RECORDID);
                    String mSalesorder = c.getString(KEY_INVOICE_SALESORDER);
                    String mStatus = c.getString(KEY_INVOICE_STATUS);
                    String mSubject = c.getString(KEY_INVOICE_SUBJECT);
                    String mTotal = c.getString(KEY_INVOICE_TOTAL);  
                    // creating new HashMap

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

                    // adding each child node to HashMap key => value
                    map.put(KEY_INVOICE_ACTION, mAction);
                    map.put(KEY_INVOICE_ASSIGNEDTO, mAssignedTo);
                    map.put(KEY_INVOICE_INVOICENO, mInvoice);
                    map.put(KEY_INVOICE_RECORDID, mRecordid);
                    map.put(KEY_INVOICE_SALESORDER, mSalesorder);
                    map.put(KEY_INVOICE_STATUS, mStatus);
                    map.put(KEY_INVOICE_SUBJECT, mSubject);
                    map.put(KEY_INVOICE_TOTAL, mTotal);


                    // adding HashList to ArrayList


                    ModuleName.add(map);                  

                }
            }else 
            {
                Toast.makeText(getApplicationContext(), "No Data", Toast.LENGTH_LONG).show();
                return;
            }
            }

            mProgress.setVisibility(View.INVISIBLE);                
            adapter = new Invoice_adapter(Invoice.this, ModuleName);
            mListView.setAdapter(adapter);
            mListView.setVisibility(View.VISIBLE);
            //mListView.invalidateViews();

        }
            else 
            {

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

                // adding each child node to HashMap key => value
                map.put(KEY_ERROR, "Error");
                ModuleName.add(map);
                //JSONObject json_user = json.getJSONObject("user");
                //error_message = json_user.getString(KEY_ERROR_MSG);
            }


    }catch (JSONException e) {

        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Something went wrong.Please try again.!!", Toast.LENGTH_LONG).show();

    }


}



 class ParseDATA extends AsyncTask<Void, String,JSONObject  >

    {


        ProgressDialog dialog = new ProgressDialog(Invoice.this);




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

            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setTitle("Loading");
            dialog.setIndeterminate(true);
            dialog.setMessage("Please wait");
            dialog.setCancelable(false);
            dialog.show();

        }



        @Override
        protected JSONObject doInBackground(
                Void... params) {
            // TODO Auto-generated method stub

            UserFunctions userFunction = new UserFunctions();

            JSONObject json = userFunction.getModuleList("1", "20", "Invoice");

            return json;
        }


        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub  

            dialog.dismiss();

        }
    }



@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Toast.makeText(getApplicationContext(), "PAUSE STATE", Toast.LENGTH_LONG).show();
    finish();
}}

适配器类

public class Invoice_adapter extends BaseAdapter {


private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;


public Invoice_adapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   }


public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return data.get(position);

}

public long getItemId(int position) {
    return position;
}



public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;

    if(convertView==null)

        vi = inflater.inflate(R.layout.activity_module_data_detail, null);

    TextView title = (TextView)vi.findViewById(R.id.textView1); // category title name
    //ImageView moduleImage = (ImageView) vi.findViewById(R.id.recipe_image);
    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    Log.i("list", ""+data.get(position));
    //Log.i("list", data); 

    String ff = song.get(Invoice.KEY_INVOICE_SUBJECT);

   title.setText(ff);
   //moduleImage.setImageResource(mModuleImages[position]);

    return vi;


}}

请帮忙.. !!我不知道我做错了什么。提前致谢。 当我去应用程序并重新单击按钮时,即使我完成了这个活动,值也会添加两次。json只响应一个项目,但现在它绑定了两个

4

1 回答 1

3

问题在于staticArrayList 的“”关键字

public static ArrayList<HashMap<String, String>> ModuleName = new ArrayList<HashMap<String, String>>();

静态变量仅在加载类时初始化,这意味着该值将持续到应用程序结束。

在加载新数据之前删除数组列表的静态关键字或清除数组列表。

ModuleName.clear();
于 2013-10-28T12:40:03.557 回答