2014-08-17 2 views
0

Как загрузить изображение с помощью picasso library? Я уже пробовал, но он отображает force close на моем экране. Я просто добавил это:Как использовать библиотеку Пикассо

ImageView a = (ImageView)findViewById(R.id.ivImage); 
Picasso.with(Home.this).load(TAG_IMAGE).into(a); 

На мой onPostExecute(), я загрузке мой образ с сервера.

+0

ли находится ваш ImageView на activity_home.xml? – Paritosh

ответ

0

Для этого вам необходимо написать пользовательский адаптер.

Обратитесь к this link, чтобы узнать, как использовать Picasso в пользовательском адаптере.

Чтобы узнать о customAdapter() см this link

+0

Используйте инструкцию Picasso.with() .... – Paritosh

+0

Перейдите по ссылке в обновленном ответе. Проверьте CustomListAdapter.java на странице. – Paritosh

+0

Пожалуйста, спросите, если вы не получаете логику BaseAdapter. – Paritosh

0

Создать новый класс и назовите его, что вы хотите (здесь AlbumList_Adapter.java)

public class AlbumList_Adapter extends BaseAdapter { 

private Activity activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater = null; 
public ImageLoader imageLoader; 
String basePath = "http://example.com/imgFolder/"; 

public AlbumList_Adapter(Activity a, ArrayList<HashMap<String, String>> d) { 
    activity = a; 
    data = d; 
    inflater = (LayoutInflater) activity 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader = new ImageLoader(activity.getApplicationContext()); 
} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi = convertView; 
    if (convertView == null) 
     vi = inflater.inflate(R.layout.list_row_simple, null); 

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem); 
    ImageView imageView= (ImageView) vi.findViewById(R.id.imageView); 

    HashMap<String, String> imgData = new HashMap<String, String>(); 
    imgData = data.get(position); 

    txtListItem.setText(imgData.get("name")); 

    Picasso.with(context) 
     .load(basePath+ imgData.get("image")) 
     .resize(100, 100) 
     .centerCrop() 
     .into(imageView); 

    return vi; 
    } 

} 

В файле code.java объявить адаптер в верхней (до OnCreate(), так что вы можете использовать его в любом месте)

AlbumList_Adapter adapter; 

В коде после разбора JSON

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

      // looping through all nodes 
      for (int i = 0; i < jsonArray.length(); i++) { 

       jsonObject = jsonArray.getJSONObject(i); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap 
       map.put("name", jsonObject.getString("albumName")); 
       map.put("image", jsonObject.getString("albumImage")); 

       // adding HashMap to ArrayList 
       dataList.add(map); 
      } 
adapter = new AlbumList_Adapter(codeActivity.this, dataList); 
list.setAdapter(adapter); 

Обратите внимание, что я использую Hashmap. Вы должны использовать свою структуру данных. (Вы можете использовать Hashmap. Затем вам нужно обновить свой код)

Еще одна вещь, следующая строка в AlbumList_Adapter.java относится к файлу xml, который я создал для строки списка.

vi = inflater.inflate(R.layout.list_row_simple, null); 

Надеюсь, этот ответ поможет вам. Пожалуйста, дайте мне знать, если у вас возникли проблемы с этим.

Счастливый кодирования ...

0

Я думаю, что мой prblem на этой части

protected void onPostExecute(String file_url) { 
pDialog.dismiss(); 

String AB = TAG_IMAGE; 
ImageView a = (ImageView)findViewById(R.id.ivImage); 
Picasso.with(Home.this).load(AB).into(a); 
ListAdapter adapter = new SimpleAdapter(Home.this, mNewsList, 
R.layout.news_list,new String[] 
{ TAG_TITLE,TAG_BODY,TAG_IMAGE,TAG_DATE }, 
new int[] { R.id.tvTitle, R.id.tvBody,R.id.tvDate}); 
newsList.setAdapter(adapter); 
Смежные вопросы