我怎样才能把照片(从绘制文件夹)在ListView的URL从JSON文件 [英] How can I put photos(from drawable folder) in a listview with the url from a json file
本文介绍了我怎样才能把照片(从绘制文件夹)在ListView的URL从JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从几个人的姓名,手机号码和照片制作数据的应用程序。
I'm making an app with data from several people with the names, mobile phone number and photograph.
的 JSON
文件是关于资产
文件夹,这些照片都在绘
文件夹中。
The JSON
file is on the assets
folder, the photos are on the drawable
folder.
下面是code
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文件
{
"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"
}
]
}
能有人帮我这个???
can some one help me with this ???
推荐答案
您使用的适配器,用于该这样。
You use your Adapter for this like this.
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();
}
这篇关于我怎样才能把照片(从绘制文件夹)在ListView的URL从JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文