如何使用凌空在listview中从json获取图像

用户名

这是我的parsejson类

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();
    }
}
塔欣鲁班

将网址的String []发送到CustomList适配器。将imageView添加到CustomList布局(employ_list_layout)。建议使用Picasso,GLide,aQuery从网址加载图片。这是CustomList Adapter的修订代码。

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(app)中,将以下行添加到dependency {}标签中:

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

然后同步并运行您的应用程序。希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用凌空在android Listview中不能正确打印JSON响应

凌空在android程序中使用

如何使用凌空解析android中的json数组

如何使用凌空解析Android中以“ /”开头的Json?

如何使用凌空解析JSON文件

使用凌空JSON数据在fragmnet中的Recyclerview?

在Android中通过凌空从服务器获取图像

如何使用模型类在listview中获取json数据列表?

如何通过使用Javascript在Json中获取图像值

如何使用凌空下载一定数量的JSON

如何使用水平ListView根据CustomAdapter的getView中的位置获取图像

如何从json响应中获取图像?

如何从listVIew获取图像ID

从ListView中的url获取图像

如何从Internet获取图像并将其粘贴到ListView中?

Flutter:如何限制在“ ListView.builder()”中获取图像?

Cardview使用来自URL的图像和片段中的凌空抽空

如何使用 ArrayAdapter 将获取的 json 数据显示到 listView 中

使用凌空获取Android中的服务器端会话ID

如何直接从mysql数据库中获取图像并使用php以json格式显示它们?

如何使用目标c从json获取collectionview单元格中的图像

如何检查是否有属性(对象)是空在Java中

如何使用凌空与JsonObjectRequest发送JSONArray

如何在Objective C中从JSON获取HTML图像?

使用JSON从Django后端获取并显示reactjs中的图像

从JSON中获取图像URL

如何使用UI Automation从ListView或类似控件中获取文本?

如何使用QtQuick ListView在onCurrentItemChanged中获取模型

如何在Android中使用新图像更新ListView中每个ImageView的图像