0

有两个活动,其中第一个活动显示带有一些数据的表格布局(此数据作为来自 Web 应用程序的响应获取),当单击一行时,它启动活动 2,该活动显示其他表格布局。在同一屏幕中有一个按钮,单击该按钮会重新启动活动 1。现在如何防止活动 1 再次访问服务器并获取响应。它应该显示在活动 2 开始之前获取的数据。

这是我的活动之一:

public class CaptureActivity extends Activity {

//private String url = "http";
private String url = "http:";

FetchEmployeeAsyncTask employeeAsyncTask;
SelectedEmployeeAsyncTask selectedEmployeeAsyncTask;
private ArrayList<Employee> employees = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
     employeeAsyncTask = new FetchEmployeeAsyncTask(this);
     selectedEmployeeAsyncTask = new SelectedEmployeeAsyncTask(this);
    employeeAsyncTask.execute(new String[] {url});

    System.out.println("Status "+employeeAsyncTask.getStatus());

    setContentView(R.layout.activity_capture);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_capture, menu);
    return true;
}



public void showEmployees(ArrayList<Employee> employees) {
    // TODO Auto-generated method stub
    System.out.println("Status 2  "+employeeAsyncTask.getStatus());
    TableLayout employeeTable = (TableLayout) findViewById(R.id.main_table);

    TableRow header = new TableRow(this);
    header.setId(100);
    header.setBackgroundColor(Color.rgb(255, 165, 0));
    //header.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    TextView empCodeHeader = new TextView(this);
    empCodeHeader.setId(200);
    empCodeHeader.setText("Employee Code");
    empCodeHeader.setTextSize(16);
    empCodeHeader.setPadding(2,2,2,2);
    empCodeHeader.setWidth(200);
    header.addView(empCodeHeader);

    TextView empNameHeader = new TextView(this);
    empNameHeader.setId(201);
    empNameHeader.setText("Employee Name");
    empNameHeader.setTextSize(16);
    empNameHeader.setPadding(2,2,2,2);
    empNameHeader.setWidth(300);
    header.addView(empNameHeader);

    employeeTable.addView(header);//, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    for(int i=0; i<employees.size();i++){

        final Employee employee = (Employee) employees.get(i);

        int count = 0;

        TableRow empData = new TableRow(this);
        empData.setId(300+count);
        empData.setClickable(true);
        /*empData.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT ));*/
        //tr.setClickable(true);

        final TextView empCode = new TextView(this);
        empCode.setId(300+count);
        empCode.setText(employee.getCode());
        empCode.setTextSize(16);
        empCode.setPadding(2,2, 2, 2);
        empData.addView(empCode);

        final TextView empName = new TextView(this);
        empName.setId(300+count);
        empName.setText(employee.getFirstName()+","+employee.getLastName());
        empName.setTextSize(16);
        empName.setPadding(2, 2, 2, 2);
        empData.addView(empName);

        employeeTable.addView(empData); /*, new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));*/
       count++;

       empData.setOnClickListener(new OnClickListener() {

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

            String employeeUrl = "http://192.168.2.166:8680/capture/clientRequest.do?r=employeeList&code="+employee.getCode()+"&cid=0&mode=full";

            selectedEmployeeAsyncTask.execute(new String[] {employeeUrl});

        }
    });
    }
}


public void display(ArrayList<Employee> employees){

    System.out.println("in here !! ");
    HashMap<String, String> empMap = null;
    for (int i = 0;i<employees.size(); i++){

        final Employee empDetails = (Employee) employees.get(i);

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

        empMap.put("Honorific", empDetails.getHonorific());
        empMap.put("First Name", empDetails.getFirstName());
        empMap.put("Last Name", empDetails.getLastName());
        empMap.put("Call Name", empDetails.getCallName());
        empMap.put("Gender", empDetails.getGender());
        empMap.put("Designation", empDetails.getDesignation());
        empMap.put("Level", empDetails.getLevel());
        empMap.put("Stream", empDetails.getStream());
        empMap.put("Mobile", empDetails.getMobile());
        empMap.put("Phone", empDetails.getPhone());
        empMap.put("Email", empDetails.getEmail());
        empMap.put("DOB", empDetails.getDob());
        empMap.put("DOJ", empDetails.getDoj());
        empMap.put("Present Address", empDetails.getPresentAddress());
        empMap.put("Permanent Address", empDetails.getPermanentAddress());
        empMap.put("Status", empDetails.getStatus());
        empMap.put("Company Id", empDetails.getCompanyId());
        empMap.put("PM User Id", empDetails.getPMUserId());
        empMap.put("PM Code", empDetails.getPmCode());
        empMap.put("PM First Name", empDetails.getPmFirstName());
        empMap.put("PM Last Name", empDetails.getPmLastName());
        empMap.put("PM Call Name", empDetails.getPmCallName());
        empMap.put("PM Email", empDetails.getPmEmail());
        empMap.put("Nationality", empDetails.getNationality());
        empMap.put("Blood Group", empDetails.getBloodGroup());
        empMap.put("Marital Status", empDetails.getMaritalStatus());
        empMap.put("Language", empDetails.getLanguage());
        empMap.put("Guardian Name", empDetails.getGuardianName());
        empMap.put("Guardian Type", empDetails.getGuardianType());
        empMap.put("Desk No", empDetails.getDeskNo());
        empMap.put("Extn No", empDetails.getExtnNo());
        empMap.put("Designation Id", empDetails.getDesignationId());
        empMap.put("Level Id", empDetails.getLevelId());
        empMap.put("Stream Id", empDetails.getStreamId());
        empMap.put("Created By", empDetails.getCreatedBy());
        empMap.put("Date Created", empDetails.getDateCreated());
        empMap.put("Personal Email", empDetails.getPersonalEmail());
        empMap.put("Photo Id", empDetails.getPhotoId());
        empMap.put("Place of Issue", empDetails.getPlaceOfIssue());
        empMap.put("Date of Issue", empDetails.getDateOfIssue());
        empMap.put("Inactive Status Type", empDetails.getInactiveStatusType());
        empMap.put("Date Status", empDetails.getDateStatus());
        empMap.put("Image", empDetails.getImage());

        }

    Intent displaySelectedIntent = new Intent(this,DisplaySelectedActivity.class);
    displaySelectedIntent.putExtra("empDetailsMap", empMap);
    startActivity(displaySelectedIntent);

}

/**
 * @return the employees
 */
public ArrayList<Employee> getEmployees() {
    return employees;
}

/**
 * @param employees the employees to set
 */
public void setEmployees(ArrayList<Employee> employees) {
    this.employees = employees;
}   

}

这是我的活动2:

public class DisplaySelectedActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_selected);
    HashMap<String , String> listemp = (HashMap<String, String>)getIntent().getSerializableExtra("empDetailsMap");

    TableLayout displaySelected = (TableLayout) findViewById(R.id.displaySelected);
    for (Map.Entry entry : listemp.entrySet()) {
        TableRow tr_inner = new TableRow(DisplaySelectedActivity.this);

        if(!entry.getKey().toString().equalsIgnoreCase("Image")){

            TextView key = new TextView(this);
        key.setId(400);
        key.setText(entry.getKey().toString());
        key.setTextSize(15);
        key.setTextColor(Color.RED);
        //key.setBackgroundColor(Color.YELLOW);
        tr_inner.addView(key);

        TextView value = new TextView(this);
        value.setId(500);
        value.setText(entry.getValue().toString());
        value.setTextSize(15);
        tr_inner.addView(value);

        /*Bitmap bm = BitmapFactory.decodeFile(pathName)

        ImageView empImage = (ImageView) findViewById(R.id.empImage);
        empImage.setImageBitmap(bm)
        */
        displaySelected.addView(tr_inner);/*, new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));*/
        }
        else
        {   
                    System.out.println("in else1111");

                     byte[] imageAsBytes = Base64.decode(entry.getValue().toString().getBytes(), 0);

                    //Bitmap bm = BitmapFactory.decodeFile(entry.getValue().toString());

                    ImageView empImage = (ImageView) findViewById(R.id.empImage);
                    empImage.setImageBitmap( BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
}
        }
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_display_selected, menu);
    return true;
}

public void goBack(View view){
    Intent intent = new Intent(this,CaptureActivity.class);
    startActivity(intent);
}}

当在活动 2 中按下按钮时 goBack() 被调用...

4

2 回答 2

0

在你的情况下,你可以把这个检查:

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
if(employees !=null){

 employeeAsyncTask = new FetchEmployeeAsyncTask(this);
 selectedEmployeeAsyncTask = new SelectedEmployeeAsyncTask(this);
 employeeAsyncTask.execute(new String[] {url});

System.out.println("Status "+employeeAsyncTask.getStatus());
}

//和加载数据的函数。setContentView(R.layout.activity_capture);

}

在活动 2 中,您可以将 goBack 函数更改为(如果您维护了堆栈):

public void goBack(View view){finish(); }

或者

如果您想拥有良好的持久性,可以使用本地数据库来保存详细信息。并将它们从 DB 显示到 UI。

于 2013-01-31T07:12:00.073 回答
0

尝试这个

public void goBack(View view){
 finish();

}}

它会解决你的问题....

于 2013-01-31T07:19:05.660 回答