如何使用json文件中的url将照片(来自drawable文件夹)放在listview中

时间:2015-06-24 11:58:40

标签: android json listview

我正在制作一个应用程序,其中包含来自多个人的数据,其中包含姓名,手机号码和照片。

JSON文件位于assets文件夹中,照片位于drawable文件夹中。

这是代码

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;


public String loadJSONFromAsset(){
    String json =  null;
    try{
        InputStream is = getAssets().open("lista.json");
        int size = is.available();
        byte[] buffer= new byte[size];
        is.read(buffer);
        is.close();
        json= new String (buffer, "UTF-8");
    } catch (IOException ex){
        ex.printStackTrace();
        return null;
    }
    return json;
}


private static final String TAG_CONTACTS = "pessoas";
private static final String TAG_NAME = "nome";
private static final String TAG_PHONE_MOBILE = "tlm";
private static final String TAG_PHOTO = "photo";


JSONArray contacts = null;


ArrayList<HashMap<String, String>> contactList;

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

    contactList = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

            String name = ((TextView) view.findViewById(R.id.nome))
                    .getText().toString();

            String description = ((TextView) view.findViewById(R.id.tlm))
                    .getText().toString();

            // My problem is here...how can I do this????

            String description = ((TextView) view.findViewById(R.id.photo))
             .getText().toString();



            Intent in = new Intent(getApplicationContext(),
                    SingleContactActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_PHONE_MOBILE, description);
            in.putExtra(TAG_PHOTO, descripphoto);
            startActivity(in);

        }
    });


    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Aguarde um momento...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

        String jsonStr = loadJSONFromAsset();

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_CONTACTS);


                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String nome = c.getString(TAG_NAME);
                    String tlm = c.getString(TAG_PHONE_MOBILE);
                    String photo = c.getString(TAG_PHOTO);


                    HashMap<String, String> contact = new HashMap<String, String>();


                    contact.put(TAG_NAME, nome);
                    contact.put(TAG_PHONE_MOBILE, tlm);
                    contact.put(TAG_PHOTO, photo);


                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Nao se obteve qualquer dado da BD");
        }


        Collections.sort(contactList, new Comparator<HashMap<String, String>>() {
            @Override
            public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {
                return (lhs.get(TAG_NAME).toLowerCase().compareTo(rhs.get(TAG_NAME).toLowerCase()));
            }
        });

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();



        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, contactList,
                R.layout.list_item, new String[] { TAG_NAME,
                TAG_PHONE_MOBILE,TAG_PHOTO }, new int[] { R.id.nome,
                R.id.photo, R.id.tlm,  });

        setListAdapter(adapter);

    }

}

我的list_item.xml

<TextView
    android:id="@+id/nome"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tlm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:textColor="#5d5d5d"
    android:textStyle="bold" />

 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/photo"/>

我的json file

{
  "pessoas":
  [
    {
  "nome":"A",
  "tlm":"911911911",
  "photo":"drawable/a.png"
},
{
  "nome":"B",
  "tlm":"911911911",
  "photo":"drawable/b.png"
},
{
  "nome":"C",
  "tlm":"911911911",
  "photo":"drawable/c.png"
}
]
 }

有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:0)

您可以直接从资源中设置资源,例如

int imageResource = getResources().getIdentifier(@drawable/myresource.png, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

将其绑定到适配器上的列表中!

答案 1 :(得分:0)

您可以像这样使用适配器。

LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    View rowView = mInflater.inflate(R.layout.dashboard_adapter, null);
    TextView textView_area = (TextView) rowView
            .findViewById(R.id.textView_area);

    ImageView big = (ImageView) rowView.findViewById(R.id.big);

    try {
    Picasso.with(context).load(dashboard_ArrayList.get(position).getImagepath()).into(big);
    textView_area.setText(dashboard_ArrayList.get(position).getCat_name());

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

big.png是可绘制文件夹中的图像。

答案 2 :(得分:-1)

为此,您需要编写条件,将照片路径映射到可绘制资源

OR

复制内部或外部SD卡中可绘制的那些