I'm at the beginning with android and I'm trying to do a ListView with images on the left and some textviews on the right. I wanna use Picasso to show images, because I think is the easyest way to load images from the web. But I have some problems and I don't know how to do it. Below my code: Thank you so much !
InterestPoint.java:
package com.example.findmyclients;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
public class InterestPoint {
public String nome;
public String categoria;
public String apiedi;
public String inauto;
}
ListviewActivity.java:
package com.example.findmyclients;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListviewActivity extends Activity{
private ListView lista;
private ListView listaNOGPS;
private List<InterestPoint> listaInteressPoint;
private List<InterestPoint> listaInteressPointNOGPS;
private InterestPoint ip;
private CustomListAdapter adapter;
private CustomListAdapter adapterNOGPS;
public String [][]ArrayTime = new String[1000][7]; //La matrice che conterrà tutti i dati relativi alla lista
public String [][]Dati_history = new String[100][10];
public String [][]Dati_restaurant = new String[100][10];
public String [][]Dati_hotel = new String[100][10];
public String [][]Dati_souvenir = new String[100][10];
public String pathdirectory = "/sdcard/PredappioLiving/";
String[]file_names={"markers_history.xml", "markers_hotel.xml", "markers_restaurant.xml", "markers_souvenir.xml"};
String[]Array_Cat={"Monumenti","Alloggi","Ristoranti","Negozi"};
int nome_luogo = 0;
int categoria = 1;
int auto = 2;
int piedi = 3;
int latitude = 4;
int longitude = 5;
int url_anteprima_immagine=6;
int gps_on= 0;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final Activity thiz = this;
//Get ListView Object from xml
lista = (ListView) thiz.findViewById(R.id.lista);
listaNOGPS = (ListView) thiz.findViewById(R.id.lista);
GPSTracker gpstrack = new GPSTracker(this);
if (gpstrack.canGetLocation() ) {
gps_on = 1;
}
/* - - - Costruzione della matrice dall'xml - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - -*/
int file_choice=-1;
int n = 0; //mi serve per riempire bene tutta la matrice
for (int i = 0; i<file_names.length; i++)
{
String file_path = pathdirectory+file_names[i];
int indice_riempimento = n;
int returnhistory = file_names[i].compareTo("markers_history.xml");
int returnhotel = file_names[i].compareTo("markers_hotel.xml");
int returnrestaurant = file_names[i].compareTo("markers_restaurant.xml");
int returnsouvenir = file_names[i].compareTo("markers_souvenir.xml");
if (returnhistory == 0){
file_choice = 0;
}
if (returnhotel == 0){
file_choice = 1;
}
if (returnrestaurant == 0){
file_choice = 2;
}
if (returnsouvenir == 0){
file_choice = 3;
}
// Inizio lettura da XML e popolazione della matrice
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream(file_path));
doc.getDocumentElement().normalize();
NodeList nodi = doc.getElementsByTagName("marker");
for (int c = 0; c<nodi.getLength(); c++)
{
Element item = (Element) nodi.item(c);
String nome = item.getAttribute("name");
String destLat = item.getAttribute("lat");
String destLong = item.getAttribute("long");
String urlimmagine = item.getAttribute("immagine");
Double lat = Double.valueOf(destLat);
Double lon = Double.valueOf(destLong);
GPSTracker gpsTracker = new GPSTracker(this);
String stringMyLatitude = String.valueOf(gpsTracker.latitude);
String stringMyLongitude = String.valueOf(gpsTracker.longitude);
double currentLat = Double.parseDouble(stringMyLatitude);
double currentLong = Double.parseDouble(stringMyLongitude);
final float[]distanzadouble = new float[3];
Location.distanceBetween(currentLat, currentLong, lat, lon, distanzadouble);
float metri = distanzadouble[0];
float km = Math.round((double)metri/1000);
int minuti_persona = (int)Math.round(metri/125); //125 metri al minuto -> velocità media di 2,5 m/s
int minuti_auto = (int)Math.round(km/0.7); //700 metri al minuto -> velocità media di 42 km/h
String string_min_a_piedi;
String string_min_in_auto;
if(minuti_persona <=0) // Stampa tempo per coprire la distanza
{
string_min_a_piedi="meno di un minuto";
}else
{
string_min_a_piedi=String.valueOf(minuti_persona);
}
if(minuti_auto <= 0)
{
string_min_in_auto="meno di un minuto";
}else
{
string_min_in_auto= String.valueOf(minuti_auto);
}
ArrayTime[c+n][nome_luogo] = nome;
ArrayTime[c+n][categoria] = Array_Cat[file_choice];
ArrayTime[c+n][auto] = string_min_in_auto;
ArrayTime[c+n][piedi] = string_min_a_piedi;
ArrayTime[c+n][url_anteprima_immagine] = urlimmagine;
}
n = n+nodi.getLength();
}catch(Exception e)
{
e.printStackTrace();
}
}
//Arrays.sort(ArrayTime, new ColumnComparator(0));
//Stampa dell'array sulla lista - - -
/*
for(int i = 0; i<6; i++)
{
ip = new InterestPoint();
ip.nome = "pippo"+i;
ip.categoria ="cat pippo"+i;
ip.apiedi = "25";
ip.inauto = "10";
listaInteressPoint.add(ip);
}
*/
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if (gps_on == 1) {
listaInteressPoint = new ArrayList<InterestPoint>();
for(int i = 0; i<n;i++)
{
ip = new InterestPoint();
ip.nome = ArrayTime[i][nome_luogo];
ip.categoria = "Categoria: "+ArrayTime[i][categoria];
ip.inauto = "In auto: " +ArrayTime[i][auto]+ " minuti";
ip.apiedi = "A piedi: " +ArrayTime[i][piedi]+ " minuti";
listaInteressPoint.add(ip);
}
//Ordinamento
Collections.sort(listaInteressPoint, new Comparator<InterestPoint>(){
@Override
public int compare(InterestPoint elem1, InterestPoint elem2) {
return elem1.inauto.compareTo(elem2.inauto);
}
});
}else
{
listaInteressPointNOGPS = new ArrayList<InterestPoint>();
for(int i = 0; i<n;i++)
{
ip = new InterestPoint();
ip.nome = ArrayTime[i][nome_luogo];
ip.categoria = "Categoria: "+ArrayTime[i][categoria];
ip.inauto = "In auto: Attiva la posizione per la distanza";
ip.apiedi = "A piedi: in auto e a piedi";
listaInteressPointNOGPS.add(ip);
}
//Ordinamento
Collections.sort(listaInteressPointNOGPS, new Comparator<InterestPoint>(){
@Override
public int compare(InterestPoint elem1, InterestPoint elem2) {
return elem1.nome.compareTo(elem2.nome);
}
});
}
//ListView Item Click Listener
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (gps_on == 1){
//listaInteressPoint = new ArrayList<InterestPoint>();
//ListView Clicked item index
int itemPosition = position;
InterestPoint item = adapter.getItem(position);
Intent piu_info = new Intent(thiz, MoreInfoActivity.class);
piu_info.putExtra("nome", item.nome);
startActivity(piu_info);
//ListView clicked item value
//int itemValue = (int) lista.getItemAtPosition(position);
//Show Alert
//Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem :"+ item.nome, Toast.LENGTH_LONG).show();
}else
{
//ListView Clicked item index
//listaInteressPointNOGPS = new ArrayList<InterestPoint>();
int itemPosition = position;
InterestPoint item = adapterNOGPS.getItem(position);
Intent piu_info = new Intent(thiz, MoreInfoActivity.class);
piu_info.putExtra("nome", item.nome);
startActivity(piu_info);
//ListView clicked item value
//int itemValue = (int) lista.getItemAtPosition(position);
//Show Alert
//Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem :"+ item.nome, Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
//final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
adapter = new CustomListAdapter(getApplicationContext(), R.layout.list_item, listaInteressPoint);
adapterNOGPS = new CustomListAdapter(getApplicationContext(), R.layout.list_item, listaInteressPointNOGPS);
if (gps_on == 1 ) {
lista.setAdapter(adapter);
}else
{
listaNOGPS.setAdapter(adapterNOGPS);
}
}
public String GetURLperLista(String nome){
String url_luogo="";
for (int i = 0; i<ArrayTime.length; i++)
{
if (nome.equals(ArrayTime[i][nome_luogo]))
{
url_luogo = ArrayTime[i][url_anteprima_immagine];
}
}
return url_luogo;
}
}
CustomListAdapter.java:
package com.example.findmyclients;
import com.example.findmyclients.AppController;
import com.example.findmyclients.Movie;
import com.squareup.picasso.Picasso;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class CustomListAdapter extends ArrayAdapter<InterestPoint> {
private Context context;
private int textViewResourceId;
private LayoutInflater inflater;
private List<InterestPoint> listaPunti;
public CustomListAdapter(Context context, int textViewResourceId,
List<InterestPoint> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.listaPunti = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(textViewResourceId, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.nomeView = (TextView) v.findViewById(R.id.label_titolo);
holder.categoriaView = (TextView) v.findViewById(R.id.label_categoria);
holder.inautoView = (TextView) v.findViewById(R.id.label_in_auto);
holder.apiediView = (TextView) v.findViewById(R.id.label_a_piedi);
holder.immagine = (ImageView) v.findViewById(R.id.immagine_luogo);
// associate the holder with the view for later lookup
v.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) v.getTag();
}
// no local variables with findViewById here
InterestPoint item = getItem(position);
holder.nomeView.setText(item.nome);
holder.categoriaView.setText(item.categoria);
holder.inautoView.setText(item.inauto);
holder.apiediView.setText(item.apiedi);
ListviewActivity lvA = new ListviewActivity();
String url = lvA.GetURLperLista(item.nome);
try{
Picasso.with(context).load(url).into(holder.immagine);
}catch(Exception e)
{
e.printStackTrace();
}
return v;
// use holder.nameText where you were
// using the local variable nameText before
}
// somewhere else in your class definition
static class ViewHolder {
TextView nomeView;
TextView categoriaView;
TextView inautoView;
TextView apiediView;
ImageView immagine;
}
}
0 comments:
Post a Comment