2017-02-04 10 views
0

Это мой parsejson класскак получить изображения из JSON в ListView с помощью залп

public class ParseJSON { 

    public static String[] names;public static String[] lastname; 
    public static String[] dob; 
    public static String[] address;public static String[] contact1; 
    public static String[] contact2; 
    public static String[] email;public static String[] tech; 
    public static String[] skills; 
    public static String[] status; 
    public static String[] appointdate; 
    public static String[] joiningdate; 
    public static String[] e_code; 
    public static String[] e_salary; 
    public static String[] qualification; 
    public static String[] b_name; 
    public static String[] b_branch; 
    public static String[] b_city; 
    public static String[] b_acount_no; 
    public static String[] b_ifsc; 
    public static String[] remarks; 

    public static final String JSON_ARRAY = "info"; 

    public static final String KEY_NAME = "name"; 
    public static final String KEY_LASTNAME = "lastname"; 
    public static final String KEY_DOB = "dob"; 
    public static final String KEY_ADDRESS = "address"; 
    public static final String KEY_CONTACT1 = "contactno1"; 
    public static final String KEY_CONTACT2 = "contactno2"; 
    public static final String KEY_EMAIL = "email"; 
    public static final String KEY_TECHNOLOGY = "technology"; 
    public static final String KEY_SKILLS = "skills"; 
    public static final String KEY_STATUS = "status"; 
    public static final String KEY_APPOINTMENTDATE = "appointmentdate"; 
    public static final String KEY_JOINNINGDATE = "joiningdate"; 
    public static final String KEY_E_CODE = "employeecode"; 
    public static final String KEY_E_SALARY = "employeesalary"; 
    public static final String KEY_QUALIFICATION = "qualification"; 
    public static final String KEY_BANK_NAME = "bankname"; 
    public static final String KEY_BANK_BRANCH = "bankbranch"; 
    public static final String KEY_BANK_CITY = "bankcity"; 
    public static final String KEY_BANK_ACCOUNT_NO = "bankaccountnumber"; 
    public static final String KEY_BANK_IFSC = "bankifsccode"; 
    public static final String KEY_REMARKS = "remarks"; 

    private String json; 
    public ParseJSON(String json){ 
     this.json = json; 
    } 

    public void parseJSON(){ 
     JSONArray users = null; 
     JSONObject jsonObject=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY); 

      names = new String[users.length()]; 
      lastname = new String[users.length()]; 
      dob = new String[users.length()]; 
      address = new String[users.length()]; 
      contact1 = new String[users.length()]; 
      contact2 = new String[users.length()]; 
      email = new String[users.length()]; 
      tech = new String[users.length()]; 
      skills = new String[users.length()]; 
      status = new String[users.length()]; 
      appointdate = new String[users.length()]; 
      joiningdate = new String[users.length()]; 
      e_code = new String[users.length()]; 
      e_salary = new String[users.length()]; 
      qualification = new String[users.length()]; 
      b_name = new String[users.length()]; 
      b_branch = new String[users.length()]; 
      b_city = new String[users.length()]; 
      b_acount_no = new String[users.length()]; 
      b_ifsc = new String[users.length()]; 
      remarks = new String[users.length()]; 

      for(int i=0;i<users.length();i++){ 
       JSONObject jo = users.getJSONObject(i); 

       names[i] = jo.getString(KEY_NAME); 
       lastname[i] = jo.getString(KEY_LASTNAME); 
       dob[i] = jo.getString(KEY_DOB); 
       address[i] = jo.getString(KEY_ADDRESS); 
       status[i] = jo.getString(KEY_STATUS); 
       contact1[i] = jo.getString(KEY_CONTACT1); 
       contact2[i] = jo.getString(KEY_CONTACT2); 
       email[i] = jo.getString(KEY_EMAIL); 
       tech[i] = jo.getString(KEY_TECHNOLOGY); 
       skills[i] = jo.getString(KEY_SKILLS); 
       appointdate[i] = jo.getString(KEY_APPOINTMENTDATE); 
       joiningdate[i] = jo.getString(KEY_JOINNINGDATE); 
       e_code[i] = jo.getString(KEY_E_CODE); 
       e_salary[i] = jo.getString(KEY_E_SALARY); 
       qualification[i] = jo.getString(KEY_QUALIFICATION); 
       b_name[i] = jo.getString(KEY_BANK_NAME); 
       b_branch[i] = jo.getString(KEY_BANK_BRANCH); 
       b_city[i] = jo.getString(KEY_BANK_CITY); 
       b_acount_no[i]jo.getString(KEY_BANK_ACCOUNT_NO); 
       b_ifsc[i] = jo.getString(KEY_BANK_IFSC); 
       remarks[i] = jo.getString(KEY_REMARKS); 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Это мой пользовательский адаптер

public class CustomList extends ArrayAdapter<String>{ 

    private String[] names; 
    private String[] image; 
    private Activity context; 

    public CustomList(Activity context, String[] names) { 
     super(context, R.layout.employ_list_layout, names); 
     this.context = context; 
     this.names = names; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View listViewItem = inflater.inflate(R.layout.employ_list_layout, null, true); 
     TextView textViewName = (TextView) listViewItem.findViewById(R.id.txtVwName); 
     textViewName.setText(" "+names[position]); 

     return listViewItem; 
    } 

} 

И это MainActivity

public class EmployeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ 

    ImageLoader mImageLoader; 
    public static final String JSON_URL = "http://ssvsoft.in/shiftsystem/webservice/userdetails.php"; 
    private ListView listView; 

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

     listView = (ListView) findViewById(R.id.lstVw); 
     // button = (Button)findViewById(R.id.btn); 

     sendRequest(); 
     listView.setOnItemClickListener(this); 

    } 

    private void sendRequest(){ 

     StringRequest stringRequest = new StringRequest(JSON_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         try { 
          JSONObject object = new JSONObject(response); 
          int status = object.getInt("status"); 
          if (status==1){ 
           showJSON(response); 
          }else { 
           Toast.makeText(EmployeActivity.this, "Data is Empty", Toast.LENGTH_SHORT).show(); 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(EmployeActivity.this,"Time out",Toast.LENGTH_LONG).show(); 
        } 
       }); 

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

    private void showJSON(String json){ 
     ParseJSON pj = new ParseJSON(json); 
     pj.parseJSON(); 
     CustomList cl = new CustomList(this,ParseJSON.names); 
     listView.setAdapter(cl); 
    } 


    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


     Intent intent = new Intent(EmployeActivity.this,GetEmployDetail.class); 
     intent.putExtra("position",position); 
     intent.putExtra("name",ParseJSON.names); 
     intent.putExtra("lastname",ParseJSON.lastname); 
     intent.putExtra("dob",ParseJSON.dob); 
     intent.putExtra("status",ParseJSON.status); 
     intent.putExtra("address", ParseJSON.address); 
     intent.putExtra("contact1", ParseJSON.contact1); 
     intent.putExtra("contact2", ParseJSON.contact2); 
     intent.putExtra("email", ParseJSON.email); 
     intent.putExtra("appointdate", ParseJSON.appointdate); 
     intent.putExtra("joiningdate", ParseJSON.joiningdate); 
     intent.putExtra("e_code", ParseJSON.e_code); 
     intent.putExtra("e_salary", ParseJSON.e_salary); 
     intent.putExtra("tech", ParseJSON.tech); 
     intent.putExtra("skills", ParseJSON.skills); 
     intent.putExtra("quali", ParseJSON.qualification); 
     intent.putExtra("bname", ParseJSON.b_name); 
     intent.putExtra("bbranch", ParseJSON.b_branch); 
     intent.putExtra("bcity", ParseJSON.b_city); 
     intent.putExtra("baccount", ParseJSON.b_acount_no); 
     intent.putExtra("bifsc", ParseJSON.b_ifsc); 

     startActivity(intent); 

    } 
    @Override 
    public void onBackPressed() { 
     //Display alert message when back button has been pressed 
     finish(); 
    } 
} 
+0

Как отображаются изображения в json? как веб-URL или база 64? или любой другой? – OBX

+0

«фотография»: «http: \/\/ssvsoft.in \/shiftsystem \/photograph \ /01.jpg" webURL –

+0

попробуйте библиотеку загрузчика изображений, такую ​​как Picasso, Glide –

ответ

1

Отправить Струнный [] URL-адресов для адаптера CustomList. Добавьте изображение в макет CustomList (util_list_layout). Для загрузки изображений с URL-адресов рекомендуется использовать Picasso, GLide, aQuery. Вот переработанный код адаптера CustomList.

private String[] names; 
private String[] image; 
private Activity context; 

public CustomList(Activity context, String[] names, String[] image) 
{ 
    super(context, R.layout.employ_list_layout, names, image); 
    this.context = context; 
    this.names = names; 
    this.image = image; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View listViewItem = inflater.inflate(R.layout.employ_list_layout, null, true); 
    TextView textViewName = (TextView)listViewItem.findViewById(R.id.txtVwName); 
    textViewName.setText(" "+names[position]); 

    ImageView ivName = (ImageView)listViewItem.findViewById(R.id.imgVwName); 
    Picasso.with(context) 
    .load(image[position]) 
    .into(ivName); 
    return listViewItem; 
} 

В вашем build.gradle (приложение) добавьте следующую строку в зависимость {} тег:

compile 'com.squareup.picasso:picasso:2.4.0' 

Затем синхронизации и запустить приложение. Надеюсь это поможет.

+1

Большое вам спасибо –

+0

Как я могу добавить dependecy Picasso, GLide, aQuery? –

+0

Glide более эффективен, чем Picasso, я полагаю –

0
public AdapterContact(Context context, List<User> contactsList) { 
     this.context = context; 
     this.contactsList = contactsList; 

    } 

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.notice_demo, null); 

     TextView en = (TextView) convertView.findViewById(R.id.tv2); 
     ImageView imageView= (ImageView) convertView.findViewById(R.id.notimg); 


     User contacts = contactsList.get(position); 
     en.setText(contacts.not1); 
     Picasso.with(context).load(Server.noticeimagepath) 
       .placeholder(context.getResources().getDrawable(R.drawable.ic_launcher)) 
       .error(context.getResources().getDrawable(R.drawable.abc)).into(imageView); 

     return convertView; 
    } 

Попробуйте это.

Смежные вопросы