ListView statique et dynamique
j'ai une listview dynamique (jai utilisé XML parser pour parser les données à partir de web service )
et maintenant je veux ajouter 4 item à cette liste view dynamique
ArrayList<String> maListe_principale = new ArrayList<String>();
maListe_principale.add(" Accueil" );
maListe_principale.add(" item1" );
maListe_principale.add(" item2" );
maListe_principale.add(" item3" );
maListe_principale.add(" item4" );
voila mon code :
la premiere classe contient les getters et setters
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.example.fadda;
public class ItemStructure {
private String name ;
private String description ;
private String thumbnail ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
} |
cette classe contient adapter :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.example.fadda;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter<ItemStructure> {
//mapData is an hashmap of hotelId vs arraylist,
private ArrayList<ItemStructure> reservationdata;
private Context ctx;
URI url = null;
private LayoutInflater mInflater;
public ListAdapter(Context context, int textViewResourceId,
ArrayList<ItemStructure> reservationdata) {
super(context, textViewResourceId, reservationdata);
this.reservationdata = reservationdata;
this.ctx = context;
this.mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.item_activity, null);
}
final ItemStructure o = reservationdata.get(position);
if (o != null) {
TextView Title = (TextView) v.findViewById(R.id.TextView01);
Title.setText(o.getName());
}
return v;
}
} |
cette classe contient l'activity principale :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package com.example.fadda;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Object TestAsyncTask;
private ListAdapter htlAdapt = null;
private ListView htlListView = null;
private String name;
private ItemStructure reservationdata = new ItemStructure();
static ArrayList<ItemStructure> Content = new ArrayList<ItemStructure>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
setContentView(R.layout.activity_main);
// TextView hotelname=(TextView)findViewById(R.id.slist);
htlListView = (ListView) findViewById(R.id.list);
htlAdapt = new ListAdapter(this, R.layout.item_activity, Content);
htlListView.setAdapter(htlAdapt);
getURL("http://www.example.net/blabla.xml");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void getURL(String url) {
TestAsyncTask test = new TestAsyncTask();
test.setContext(this);
test.execute(url);
}
class TestAsyncTask extends
AsyncTask<String, Void, ArrayList<ItemStructure>> {
// private String Content;
ArrayList<ItemStructure> Contents = null;
private String Error = null;
private ProgressDialog Dialog;
private Context ctx;
public void setContext(Context ctx) {
this.ctx = ctx;
}
protected void onPreExecute() {
Dialog = new ProgressDialog(ctx);
Dialog.setMessage("Loading Data...");
Dialog.show();
}
protected ArrayList<ItemStructure> doInBackground(String... urls) {
try {
Log.i("INFOOOOOO", "++++++++1");
Contents = URLHelper.executeRequest();
Log.i("INFOOOOOO", "++++++++" + Contents.size());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Contents;
}
protected void onPostExecute(ArrayList<ItemStructure> content) {
if (Error != null) {
Toast.makeText(ctx, "Pls Try Again " + Error,
Toast.LENGTH_LONG).show();
} else {
updateView(content);
}
Dialog.dismiss();
}
}
private void updateView(ArrayList<ItemStructure> content) {
this.Content.clear();
this.Content.addAll(content);
this.htlAdapt.notifyDataSetChanged();
}
public String getName1() {
return name;
}
public void setTitle1(String name) {
this.name = name;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.example.fadda;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class URLHelper {
/** Called when the activity is first created. */
@SuppressWarnings("unchecked")
public static ArrayList<ItemStructure> executeRequest( ) throws ClientProtocolException, IOException
{
//AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpClient client = new DefaultHttpClient();
String response= "";
ArrayList<ItemStructure> resarrHotelList= null;
ArrayList <NameValuePair> params = new ArrayList<NameValuePair>();
String url1 = "http://www.example.net/blabla.xml";
HttpGet getUrl = new HttpGet(url1);
HttpResponse httpResponse = client.execute(getUrl);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
XMLparsing fdParser = new XMLparsing(instream);
resarrHotelList = (ArrayList<ItemStructure>) fdParser.xmlParse();
instream.close();
}
if(resarrHotelList!=null)
Log.i("INFOOOO++++", "LIST"+resarrHotelList.size());
//client.close();
return resarrHotelList;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.example.fadda;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XMLhandler extends DefaultHandler {
//private ChannelList channelList = new ChannelList();
private StringBuilder builder;
public static List<ItemStructure> channelList;
private ItemStructure chList;
// private RoomRate currentMessage=new RoomRate();
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String string = new String(ch, start, length).replaceAll("[\r\t\n]", "");
builder.append(string);
}
@Override
public void endElement(String uri, String localName, String title)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, title);
if (chList != null){
if (localName.equalsIgnoreCase("name"))
{
chList.setName(builder.toString());
Log.i("111CITYYYYYYY", "++++++++"+chList.getName());
}
else if (localName.equalsIgnoreCase("description")){
chList.setDescription(builder.toString());
}
else if (localName.equalsIgnoreCase("thumbnail")){
chList.setThumbnail(builder.toString());
}
else if (localName.equalsIgnoreCase("channel")){
channelList.add(chList);
}
builder.setLength(0);
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
channelList = new ArrayList<ItemStructure>();
builder = new StringBuilder();
}
@Override
public void startElement(String uri, String localName, String title,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, title, attributes);
if (localName.equalsIgnoreCase("channel")){
this.chList = new ItemStructure();
builder.setLength(0);
}
}
public List<ItemStructure> getChannelList() {
return channelList;
}
public void setChannelList(List<ItemStructure> channelList) {
this.channelList = channelList;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.example.fadda;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class XMLparsing {
private InputStream xmlInputStream;
public XMLparsing(InputStream xmlInputStream){
this.xmlInputStream = xmlInputStream;
}
public List<ItemStructure> xmlParse() throws IOException
{
SAXParserFactory factory = SAXParserFactory.newInstance();
List<ItemStructure> channellist=null;
try {
SAXParser parser = factory.newSAXParser();
XMLhandler handler = new XMLhandler();
parser.parse(this.getInputStream(),handler);
return handler.getChannelList();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return channellist;
}
private InputStream getInputStream() {
// TODO Auto-generated method stub
return xmlInputStream;
}
} |