我正在使用其getView()方法未被调用的自定义适配器。我有一个回调接口方法,当异步任务完成它的工作时调用。
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>
答案 0 :(得分:0)
您的代码应该代替:
LayoutInflater inflater = ((Home_Screen) context)
.getLayoutInflater();
试试这个:
LayoutInflater inflater =LayoutInflater.from(context);
看看它是否有效!