您好我编写android程序从服务器获取数据并在列表视图中显示它 但当我运行程序时,它显示我的空列表视图 请看我的代码并帮助我:
MainActiviy class
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 :(得分:0)
我相信您的问题可能是adpater(IA)使用getView方法执行我认为应该通过bindView方法完成的操作。这是尝试将getView更改为bindView。
另外我相信inflater代码: -
LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.row_main, parent, false);
属于newView方法。
这是一个工作适配器的示例。但是,这是一个Cursor。我确实相信他们非常相似: -
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是交替背景颜色。
答案 1 :(得分:0)
告诉适配器x行没有。
替换此
super(MainActivity.this,R.layout.row_main);
(通过添加计数)
super(MainActivity.this,R.layout.row_main, count);