我的代码从服务器获取数据并在listview中显示第一个加载,listview显示10个项目,我去点击更多按钮,它添加10个新项目列表,显示20个项目怎么做??
我的代码我的问题是当点击更多按钮时显示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 :(得分:0)
问题是你的setListAdapter,因为它的参数总是IA(()类的新等。
你必须在onCreate()的开头创建一次ListView,你的适配器和一个ArrayList,然后像这样设置适配器 listView.setAdapter(适配器); 在此之后,您可以调用所需的每个Asynctask或线程以获取新数据,并将其添加到现有的arraylist中。 然后调用adapter.notifyDataSetChanged()。