如何使用以前存在的数据重新加载活动

时间:2013-01-31 06:42:56

标签: android android-intent

有两个活动,其中第一个活动显示带有一些数据的tablelayout(此数据作为来自Web应用程序的响应被提取),当单击一行时,它启动活动2,其显示其他tablelayout。在同一个屏幕上有一个按钮,单击该按钮会重新启动活动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()被称为...

2 个答案:

答案 0 :(得分: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){   完(); }

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

答案 1 :(得分:0)

试试这个

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

}}

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