我正在使用getView()方法沒有被調用的自定義適配器。我有一個回調接口方法,當異步任務完成時它會被調用。ArrayAdapter的行爲很糟糕
public class Student extends Activity implements AsyncFinishInterface
{
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.student_screen);
list= (ListView) findViewById(R.id.listViewList);
new Aynctask().execute();
}
@Override
public void finish(String response, String method) throws Exception {
ArrayList<Friend> FriendList= new ArrayList<Friend>();
Friend friend;
JSONObject row;
JSONObject json= new JSONObject(response);
JSONArray jsonArray=json.getJSONArray("Friends");
int size=jsonArray.length();
Friend[] friendArray= new Friend[size];
for(int i=0 ; i<jsonArray.length(); i++)
{
friend = new Friend();
row= jsonArray.getJSONObject(i);
friend.setFirstName(row.getString("firstName"));
friend.setLastName(row.getString("lastName"));
friend.setEmail(row.getString("email"));
friendArray[i]=friend;
}
ContentAdapter contentAdapter= new ContentAdapter(this, R.layout.friend_detail, friendArray);
list.setAdapter(contentAdapter); //Till here it's working fine.
}
class ContentAdapter extends ArrayAdapter<Friend> {
Context context;
int layoutResourceId;
Friend data[] = null;
public ContentAdapter(Context context, int resource, Friend[] objects) {
super(context, resource, objects);
this.layoutResourceId = resource;
this.data = objects;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// ContentsHolder holder;
LayoutInflater inflater = ((Home_Screen) context)
.getLayoutInflater();
return row = inflater.inflate(layoutResourceId, parent, false);
}
}
有沒有人有想法這裏有什麼問題。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<ImageView
android:id="@+id/imageViewFrndImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:src="@drawable/app_icon" />
<TextView
android:id="@+id/textViewFrndName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textViewFrndEmail"
android:layout_alignTop="@+id/imageViewFrndImage"
android:text="TextView" />
<TextView
android:id="@+id/textViewFrndEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewFrndName"
android:layout_toRightOf="@+id/imageViewFrndImage"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
int i = super.getCount(); return i;你應該返回data.length –
調試你的代碼並檢查Adapter的構造函數上的'objects'的大小,在ArrayAdapter中你不需要'getCount',刪除那個關於getcount()的 –
它甚至不被調用 – Sunny