2016-10-28 26 views
-1

了我對獲取數據的代碼,並顯示在列表視圖中第一次加載,列表視圖顯示10個項目,我去點擊更多按鈕,其新增10項列表,顯示20個項目時,它怎麼樣?從服務器添加新項目,在點擊按鈕的ListView

其我的代碼我的問題是點擊更多按鈕,它顯示了10個新項目和老項目錯過了如何解決

public class Main extends ListActivity { 

String[] id,name,content,commentcount,purl,views; 
String tid,tname,tcontent,tcomment,tpurl,tviews; 

private int count; 
private int cpage=1; 
private double apage; 
private String res=""; 
private ImageView more; 
private Bitmap[] img; 

public static String url="http:..."; 
private ProgressDialog pr; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   

    more=(ImageView) findViewById(R.id.main_next_page); 
    new getpost().execute();   

    more.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      if(cpage==apage){     
      }else{     
       cpage++; 
       new getpost().execute();      
      }    
     } 
    });  
}  

@Override 
protected void onListItemClick(ListView l, View v, int position, long idt) {   
    Intent i=new Intent(Main.this,post.class); 
    i.putExtra("id",Integer.parseInt(id[position])); 
    startActivity(i);  
}  

private void makearray(int c){ 

    id=new String[c]; 
    name=new String[c]; 
    content=new String[c]; 
    commentcount=new String[c]; 
    purl=new String[c]; 
    views=new String[c]; 
    img=new Bitmap[c]; 
    Arrays.fill(name, ""); 

    Bitmap temp=BitmapFactory.decodeResource(getResources(), R.drawable.a); 
    Arrays.fill(img, temp);  
} 

/////////////////////////////////get data from server////////////////////////////// 
public class getpost extends AsyncTask{  

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

     pr=ProgressDialog.show(Main.this, "wait..."); 
     more.setVisibility(View.INVISIBLE);   
    } 

    @Override 
    protected Object doInBackground(Object... arg0) { 
     // TODO Auto-generated method stub   

     try{ 
      String data=URLEncoder.encode("page","utf8")+"="+URLEncoder.encode(cpage+"","utf8"); 

      URL link=new URL(url+"get_list_post.php"); 
      URLConnection connect=link.openConnection();     

      //send data 
      connect.setDoOutput(true); 
      OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 

      BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream())); 

      StringBuilder sb=new StringBuilder();    
      String line=null; 

      while((line=reader.readLine()) != null){      
       sb.append(line);      
      }       

      res=sb.toString();    

      for(int y=0;y<4;y++){ 
       if(res.charAt(y)=='☻'){ 
        count=Integer.parseInt(res.substring(0, y)); 
        res=res.substring(y+1); 
        break; 
       } 
      } 

      for(int y=0;y<4;y++){ 
       if(res.charAt(y)=='☻'){ 
        apage=Integer.parseInt(res.substring(0, y)); 
        res=res.substring(y+1); 
        break; 
       } 
      } 
      makearray(count);   

      int f=0,c=0; 

      for(int i=0;i<res.length();i++){ 
       if(res.charAt(i)=='☻'){ 
        String temp=res.substring(f,i); 

        if(c==0){       
         tid=temp;       
        } 
        if(c==1){       
         tname=temp;       
        } 
        if(c==2){       
         tcontent=temp;       
        } 
        if(c==3){       
         tcomment=temp;       
        } 
        if(c==4){       
         tviews=temp;        
        } 
        if(c==5){       
         tpurl=temp;       

         for(int t=0;t<count;t++){ 
          if(name[t].equals("")){ 
           id[t]=tid; 
           name[t]=tname; 
           content[t]=tcontent; 
           commentcount[t]=tcomment; 
           purl[t]=tpurl; 
           views[t]=tviews; 
           new getimage(tpurl,t).execute(); 
           break; 
          } 
         } 
         c=-1; 
        } 

        f=i+1; 
        c+=1; 
       } 
      }    
     }catch(Exception e){     
      res=e.toString();    
     } 

     return ""; 
    }  

    @Override 
    protected void onPostExecute(Object result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result);    

     if(res.substring(0, 2).toString().equals("ja") || res.substring(0, 2).toString().equals("<b")){ 

      Toast.makeText(getApplicationContext(), "conection failed", Toast.LENGTH_LONG).show(); 
      pr.dismiss(); 

     }else{    
      apage= Math.ceil(apage/10); 

      pr.dismiss(); 
      more.setVisibility(View.VISIBLE); 

      setListAdapter(new IA());    
     }   
    }  
} 

public class getimage extends AsyncTask<String,Void,Bitmap>{ 

    private String url; 
    private int p; 
    public getimage(String u,int P){ 

     url=u; 
     p=P; 
    }  

    @Override 
    protected Bitmap doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     Bitmap temp=null; 

     try{     
      InputStream in=new java.net.URL(url).openStream(); 
      temp=BitmapFactory.decodeStream(in); 

     }catch(Exception e){    
     } 

     return temp; 
    }  

    protected void onPostExecute(Bitmap result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     if(result != null){ 
      img[p]=result; 
     } 
     if(p==0 || p==1 || p==2 || p==3){ 

      setListAdapter(new IA()); 
     }   
    }  
} 

///////////////////////////////fill list//////////////////////////////////////// 
class IA extends ArrayAdapter<String>{ 

    public IA() { 
     super(Main.this, R.layout.row_main,name);   
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) {      
     LayoutInflater in=getLayoutInflater(); 
     View row=in.inflate(R.layout.row_main, parent,false); 

     TextView titr=(TextView) row.findViewById(R.id.row_main_titr); 
     TextView matn=(TextView) row.findViewById(R.id.row_main_content); 
     TextView extra=(TextView) row.findViewById(R.id.row_main_extra); 
     ImageView image=(ImageView) row.findViewById(R.id.row_main_image); 

     ViewGroup.LayoutParams paramsi = image.getLayoutParams(); 
     paramsi.height= (W/2)-10; 
     image.setLayoutParams(paramsi);   

     image.setImageBitmap(img[position]);    
     titr.setTextSize((float)W/30);   
     titr.setText(name[position]); 
     matn.setText(Html.fromHtml(content[position]).toString().replace("•", "\n")); 
     extra.setText("comments: "+commentcount[position] +" "+"views:"+views[position]); 
     titr.setTypeface(yekan); 

     matn.setTypeface(yekan); 
     extra.setTypeface(Main.koodak);   

     return (row); 
    }  
} 

回答

0

時,問題是你的setListAdapter,因爲它的參數始終是IA類的新istance( )。你必須在onCreate()開始時創建你的ListView,你的Adapter和你的ArrayList一次,然後像這樣設置適配器 listView.setAdapter(adapter); 在此之後,你可以互相調用的AsyncTask或線程你爲了獲得新的數據需要,將它添加到現有的數組列表。 然後調用adapter.notifyDataSetChanged()。

+0

TNX很多關於thisanswer你在這種情況下,有ENY例子? –

+0

類似的東西,你可以在https://github.com/alessandroargentieri/CustomListViewExample適應您的具體情況 –

相關問題