2016-03-06 54 views
0

喜从服务器的数据我写的android程序从服务器获取数据,并显示在列表视图 但是当我运行程序它告诉我空列表视图 请参阅我的代码,并帮助我:得到的Android

MainActiviy类

package com.example.delta.mysite2; 

public class MainActivity extends ListActivity { 

String[] id,name,content,commentcount; 
String tid,tname,tcontent,tcomment; 
private int page=1; 
private int count; 
private String res=""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new getpost().execute(); 

} 

private void makearray(int c){ 
    id=new String[c]; 
    name=new String[c]; 
    content=new String[c]; 
    commentcount=new String[c]; 

    Arrays.fill(name,""); 
} 

public class getpost extends AsyncTask{ 

    @Override 
    protected Object doInBackground(Object[] params) { 

     try { 
      String data=  URLEncoder.encode("page","utf8")+"="+URLEncoder.encode(page+"","utf8"); 
      URL link=new  URL("http://deltaspot.ir/test_android_server/wp/app/get.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; 
       } 
      } 
      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; 

         for(int t=0;t<count;t++){ 
          if(name[t].equals("")){ 
           id[t]=tid; 
           name[t]=tname; 
           content[t]=tcontent; 
           commentcount[t]=tcomment; 
           break; 
          } 
         } 
         c=-1; 
        } 
        f=i+1; 
        c+=1; 
       } 
      } 

     }catch (Exception e){ 
      res=e.toString(); 
     } 

     return ""; 
    } 

    @Override 
    protected void onPostExecute(Object o) { 

     super.onPostExecute(o); 
     setListAdapter(new IA()); 
    } 
} 


public class IA extends ArrayAdapter<String>{ 

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

    @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 img=(ImageView) row.findViewById(R.id.row_main_image); 

     titr.setText(name[position]); 
     matn.setText(content[position]); 
     extra.setText("comment"+commentcount[position]); 

     return (row); 
    } 
} 

}

和布局代码:

 <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:id="@+id/row_main_image" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/row_main_titr" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Medium Text" 
    android:id="@+id/row_main_content" 
    android:layout_below="@+id/row_main_titr" 
    android:layout_alignLeft="@+id/row_main_titr" 
    android:layout_alignStart="@+id/row_main_titr" 
    android:layout_marginTop="34dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Medium Text" 
    android:id="@+id/row_main_extra" 
    android:layout_below="@+id/row_main_image" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 
</RelativeLayout> 

这是我的代码,请帮我解决我的问题。 谢谢。

+0

可以检查你的'res'变量('RES = sb.toString();')不为空或空值? (如果你不能调试只是放一个'Toast.makeText(MainActivity.this,res,Toast.LENGTH_SHORT).show();') – pleft

+0

你能告诉我们更多你的日志吗? – ghassen92

+0

res变量为空 – alireza

回答

0

我相信你的问题可能是adpater(IA)有getView方法做我认为应该由bindView方法完成的事情。这是尝试改变getView bindView。

此外,我认为,充气代码: -

LayoutInflater in=getLayoutInflater(); 
View row=in.inflate(R.layout.row_main, parent, false); 

所属的NewView的方法。

下面是一个工作适配器的示例。但是,这是一个光标。我相信他们是非常相似,虽然: -

public class Database_Inspector_AislesDB_Adapter extends CursorAdapter { 
    public Database_Inspector_AislesDB_Adapter(Context context, Cursor cursor, int flags) { 
     super(context, cursor, 0); 
    } 
    @Override 
    public View getView(int position, View convertview, ViewGroup parent) { 
     View view = super.getView(position, convertview, parent); 
     Context context = view.getContext(); 
     if(position % 2 == 0) { 
      view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven)); 
     } else { 
      view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd)); 
     } 
     return view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView textviewaisleid = (TextView) view.findViewById(R.id.adiae_aislesdb_id); 
     TextView textviewaisleshopref = (TextView) view.findViewById(R.id.adiae_aislesdb_shopref); 
     TextView textviewaislesorder = (TextView) view.findViewById(R.id.adiae_aislesdb_order); 
     TextView textviewaislesaislename = (TextView) view.findViewById(R.id.adiae_aislesdb_aislename); 

     textviewaisleid.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ID_INDEX)); 
     textviewaislesaislename.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_NAME_INDEX)); 
     textviewaislesorder.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ORDER_INDEX)); 
     textviewaisleshopref.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_SHOP_INDEX)); 
    } 
    public View newView(Context context, Cursor cursos, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.activity_database_inspect_aislesdb_entry, parent, false); 
    } 
} 

getView在这种情况下交替的背景颜色。

+0

我将getview更改为bindview,但未修复 – alireza

+0

请参阅编辑,Inflater应该位于newView方法中。 – MikeT

+0

不幸的是我尝试它,但它不工作 – alireza

0

告诉适配器x行将在那里。

替换此

super(MainActivity.this,R.layout.row_main); 

与(加入数)

super(MainActivity.this,R.layout.row_main, count); 
+0

我改变了超级(MainActivity.this,R.layout.row_main,count);但它还没有工作。 – alireza

+0

通过记录来检查计数,在您调用super之前是否给出正值。 – UMESH0492