alirezadb

توسعه دهنده اندروید
  • تعداد ارسال ها

    1
  • تاریخ عضویت

  • آخرین بازدید

اعتبار در انجمن

0 Neutral

درباره alirezadb

  • درجه
    Newbie
  1. سلام وقت بخیر من یک پروزه اسلایدر دارم که اطلاعات رو از دیتابیس میگیره فقط یک مشکل دارم الان من در دیتابیس دو تا عکس دارم ولی در اسلایدر یکی رو نمایش میده از دوستان اگر کسی میتونه کمک کنه سورس هارو قرار میدم سورس mainactivity package ir.aiaz.test; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import android.widget.Toast; import com.daimajia.slider.library.Animations.DescriptionAnimation; import com.daimajia.slider.library.SliderLayout; import com.daimajia.slider.library.SliderTypes.BaseSliderView; import com.daimajia.slider.library.SliderTypes.TextSliderView; import com.daimajia.slider.library.Tricks.ViewPagerEx; import com.tricks.readjsonfromurl.R; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; public class MainActivity extends AppCompatActivity implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener{ TextView uid; SliderLayout sliderLayout; HashMap<String,String> Hash_file_maps ; //URL to get JSON Array private static String url = "http://aiaz.ir/kh.php"; //JSON Node Names private static final String TAG_USER = "aiaz"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "image"; private static final String TAG_EMAIL = "onvan"; JSONArray user = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new JSONParse().execute(); } private class JSONParse extends AsyncTask<String, String, JSONObject> { private ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Getting Data ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override protected JSONObject doInBackground(String... args) { JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONObject json = jParser.getJSONFromUrl(url); return json; } @Override protected void onPostExecute(JSONObject json) { pDialog.dismiss(); try { // Getting JSON Array user = json.getJSONArray(TAG_USER); int intStuff = json.length(); if (intStuff != 0) { for (int i = 0; i < json.length(); i++) { JSONObject c = user.getJSONObject(i); Log.e("ALL THE STUFF", c.toString()); // Storing JSON item in a Variable String id = c.getString(TAG_ID); String name2 = c.getString(TAG_NAME); String email = c.getString(TAG_EMAIL); Hash_file_maps = new HashMap<String, String>(); sliderLayout = (SliderLayout)findViewById(R.id.slider); Hash_file_maps.put(id+email ,name2); for(String name : Hash_file_maps.keySet()){ TextSliderView textSliderView = new TextSliderView(MainActivity.this); textSliderView .description(name) .image(Hash_file_maps.get(name)) .setScaleType(BaseSliderView.ScaleType.Fit) .setOnSliderClickListener(MainActivity.this); textSliderView.bundle(new Bundle()); textSliderView.getBundle() .putString("extra",name); sliderLayout.addSlider(textSliderView); } sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion); sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); sliderLayout.setCustomAnimation(new DescriptionAnimation()); sliderLayout.setDuration(3000); sliderLayout.addOnPageChangeListener(MainActivity.this); } } } catch (JSONException e) { e.printStackTrace(); } } } @Override protected void onStop() { sliderLayout.stopAutoCycle(); super.onStop(); } @Override public void onSliderClick(BaseSliderView slider) { Toast.makeText(this,slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show(); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { Log.d("Slider Demo", "Page Changed: " + position); } @Override public void onPageScrollStateChanged(int state) {} } سورس jsonparser package ir.aiaz.test; import android.util.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } سورس activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ir.aiaz.test.MainActivity"> <com.daimajia.slider.library.SliderLayout android:id="@+id/slider" android:layout_width="fill_parent" android:layout_height="320dp" /> </RelativeLayout>