基于单击按钮不显示图像

时间:2016-02-13 12:14:39

标签: java android mysql

当我使用2按钮,1来获取url而第2次将其保存在SharedPreference中时,它工作但是当我尝试在第二个按钮中包含FETCH方法时,它不会将URL保存在SharedPreference中,因此不会发送它到第2页。如何使用1个按钮才能使其工作? 请帮忙。 Tqvm在先进。刚开始学习android所以任何解释都非常感激。

1.MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private ImageView iv;
private Button buttonGet, buttonDisplay;
private EditText adminUsername, imageUrl;
private ProgressDialog loading;

String imagePath;


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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    iv = (ImageView)findViewById(R.id.imageView);

    buttonGet = (Button)findViewById(R.id.buttonGet);
    buttonGet.setOnClickListener(this);

    buttonDisplay = (Button) findViewById(R.id.buttonDisplay);
    buttonDisplay.setOnClickListener(this);

    adminUsername = (EditText) findViewById(R.id.etAdminUserName);
    imageUrl = (EditText) findViewById(R.id.etImageUrl);

}


private void getData() {
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

    String url = Config.DATA_URL+adminUsername.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}



private void showJSON(String response){
    String image="";

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject employeeData = result.getJSONObject(0);
        image = employeeData.getString(Config.KEY_IMAGE);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    imageUrl.setText(image);
    imagePath = image;

}

public void saveSettings(View v){
    //Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_SHORT).show();
    SharedPreferences preferences = getSharedPreferences("UserDetails", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    //editor.putString("passUsername", adminUsername.getText().toString());
    editor.putString("imageUrl", imagePath);

    editor.commit();
    finish();
}



@Override
public void onClick(View v) {

    switch (v.getId()){
        case R.id.buttonGet:

            getData();
            break;
        case R.id.buttonDisplay:
            getData();
            saveSettings(v);
            Intent secondPage = new Intent(this, Main2Activity.class);
            startActivity(secondPage);

            break;
    }
}
}

2.Main2Activity.java

public class Main2Activity extends AppCompatActivity {

ImageView Iv2;
String sendImagePath;
EditText editText;

private Bitmap bitmap;

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

    Iv2 = (ImageView) findViewById(R.id.imageView2);
    editText = (EditText) findViewById(R.id.editText);

    loadSettings();

    bitmap = getBitmapFromUrl(sendImagePath);
    Iv2.setImageBitmap(bitmap);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);


}


private void loadSettings(){
    //Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
    SharedPreferences preferences = getSharedPreferences("UserDetails", MODE_PRIVATE);

    // If value for key not exist then
    // return second param value - In this case "..."
    sendImagePath = preferences.getString("imageUrl", "...");
    editText.setText(sendImagePath);



}

public Bitmap getBitmapFromUrl(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
}

3.Config.java

public class Config {
public static final String DATA_URL = "http://10.0.3.2/folder/getPic.php?username=";
public static final String KEY_IMAGE = "image";
public static final String JSON_ARRAY = "result";
}

1 个答案:

答案 0 :(得分:1)

在Volley得到您的请愿回复之前,您正试图获得imagePath。尝试类似的东西:

    @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
            saveSettings();
            Intent secondPage = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(secondPage);

        }
    },

 .
 .
 . 

    @Override
     public void onClick(View v) {
            getData();
     }
}