My code is populating just first 2 textview

  • Replies:1
Cris Marchi
  • Forum posts: 1

Jan 5, 2013, 11:40:41 AM via Website

Hi guys.. I need some help please...
Can someone tell me what is wrong with this code? I have a Listview but just first 2 textviews are populated... why??

adapter
1public class CustomAdapter extends ArrayAdapter<Avviso> {
2 Context context;
3
4 public CustomAdapter(Context context, int textViewResourceId, List<Avviso> objects) {
5 super(context, textViewResourceId, objects);
6 }
7
8 @Override
9 public View getView(int position, View convertView, ViewGroup parent) {
10 return getViewOptimize(position, convertView, parent);
11 }
12
13 public View getViewOptimize(int position, View convertView, ViewGroup parent) {
14 ViewHolder viewHolder = null;
15 if (convertView == null) {
16 LayoutInflater inflater = (LayoutInflater) getContext()
17 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
18 convertView = inflater.inflate(R.layout.rowcustom, null);
19 viewHolder = new ViewHolder();
20 viewHolder.testoavviso = (TextView)convertView.findViewById(R.id.textViewtitolo);
21 convertView.setTag(viewHolder);
22 } else {
23 viewHolder = (ViewHolder) convertView.getTag();
24 }
25 Avviso avviso = getItem(position);
26 viewHolder.testoavviso.setText(avviso.getTestoavviso());
27 return convertView;
28 }
29
30 private class ViewHolder {
31 public TextView testoavviso;
32
33 }
34}
java class
1public class Avvisi extends Activity {
2 // blog url
3 static final String BLOG_URL =
4 "siteurl";]
5 ListView listView;
6
7 @Override
8 public void onCreate(Bundle savedInstanceState) {
9 // set layout view
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.activity_avvisi);
12
13 try {
14 getlink task = new getlink();
15 task.execute("Process started!");
16
17 //la stringa verra passata al metodo doInBackground del nostro AsyncTask
18 //per controllare lo stato del task, possiamo chiamare task.getStatus()
19
20 }
21 catch (Exception ex) {
22 ((TextView)findViewById(R.id.avviso1)).setText("Error");
23 }
24 }
25
26 private class getlink extends AsyncTask<String, String, List<Avviso>> {
27 // I tre tipi da dichiarare sono relativamente (in questo caso usiamo solo stringhe)
28 // il parametro in entrata di doInBackground (quando chiamiamo execute passiamo una stringa)
29 // il paramentro in entrata di onProgressUpdate (dati che vogliamo mostrare all' utente -opzionale,potremmo usare variabili globali-)
30 // il parametro in entrata di onPostExecure (una volta finito il task, potremmo voler mostrare un risultato all'utente)
31 // nel caso non volessimo usare parametri di alcun genere tipizziamo con Void,Void,Void (passeremo poi dei null come parametri)
32 ListView listView = (ListView)findViewById(R.id.listavvisi);
33 List<Avviso> avvisi = new LinkedList<Avviso>();
34 @Override
35 protected List<Avviso> doInBackground(String... arg) {
36
37 // stringa passata a onProgressUpdate
38
39 String result = "";
40 Document doc = null;
41 try {
42 doc = Jsoup.connect(BLOG_URL).get();
43 } catch (IOException e) {
44 // TODO Auto-generated catch block
45 e.printStackTrace();
46 }
47 Elements links = doc.select("td.AVVISI-FORMAT-DEFAULT > a[href]");
48 int j=0;
49 int numavvisi = links.size();
50 for (Element link : links) {
51 ;
52 if((j %2!=0) && (j<60))
53 {
54 String testonews = link.text();
55 String urlavviso= link.attr("abs:href");
56 int lunghezzanews = testonews.length();
57
58 if(lunghezzanews<80){
59
60 avvisi.add(new Avviso(testonews));
61 }
62
63 else{
64
65 avvisi.add(new Avviso(testonews.substring(0,80)));
66 }
67 }
68 else
69
70 {
71 }
72 j++;
73 }
74
75 return avvisi;
76
77 }
78
79
80 @Override
81 protected void onProgressUpdate(final String... values){
82
83 }
84
85 protected void onPostExecute(List<Avviso> avvisi) {
86 CustomAdapter adapter = new CustomAdapter(Avvisi.this, R.layout.rowcustom, avvisi);
87 listView.setAdapter(adapter);
88 }
89
90}
91
92
93}

Reply
Ashish Tripathi
  • Forum posts: 211

Feb 4, 2013, 12:36:33 PM via Website

Remove the bracket from if condition where you are checking the converview is null or not. All the data will populate.


if not work let me know

Reply