我将actionBarSherlock
与SherlockListFragment
一起使用,并希望设置自定义BaseAdpater
。我正在使用AsyncTask
来解析JSON
并填充适配器。但是没有显示listview
。片段是空的。这是我正在尝试的代码:
public class LatestSubmissions extends SherlockListFragment {
LatestSubmissionsAdapter adapter = null;
ArrayList<HashMap<String, String>> submissions = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
new GetSubmissionsListTask().execute(CommonUtils.USER_SUBMISSION_URL);
return super.onCreateView(inflater, container, savedInstanceState);
}
protected class GetSubmissionsListTask extends
AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
return new JSONFetcher().getJSONStringFromUrl(params[0]);
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jObject = new JSONObject(result);
//related code
submissions.add(map);
}
} catch (JSONException e) {
}
adapter = new LatestSubmissionsAdapter(getSherlockActivity(), submissions);
setListAdapter(adapter);
}
}
}
这是LatestSubmissionsAdapter:
public class LatestSubmissionsAdapter extends BaseAdapter {
private Context mContex;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public LatestSubmissionsAdapter(Context context,
ArrayList<HashMap<String, String>> data) {
this.mContex = context;
this.data = data;
inflater = (LayoutInflater) mContex
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.latest_submissions, null);
RelativeLayout firstRow = (RelativeLayout) vi
.findViewById(R.id.first_row);
TextView language = (TextView) firstRow.findViewById(R.id.lang);
TextView verdict = (TextView) firstRow.findViewById(R.id.verdict);
RelativeLayout secondRow = (RelativeLayout) vi
.findViewById(R.id.second_row);
TextView problem = (TextView) secondRow
.findViewById(R.id.problem_name_id);
TextView rank = (TextView) secondRow.findViewById(R.id.rank);
RelativeLayout thirdRow = (RelativeLayout) vi
.findViewById(R.id.third_row);
TextView submissionTime = (TextView) thirdRow
.findViewById(R.id.submit_time);
TextView executionTime = (TextView) thirdRow
.findViewById(R.id.exec_time);
HashMap<String, String> submissionInfo = new HashMap<String, String>();
submissionInfo = data.get(position);
language.setText(submissionInfo.get(CommonUtils.KEY_LANGUAGE_ID));
verdict.setText(submissionInfo.get(CommonUtils.KEY_VERDICT_ID));
problem.setText(submissionInfo.get(CommonUtils.KEY_PROBLEM_ID));
rank.setText(submissionInfo.get(CommonUtils.KEY_SUBMISSION_RANK));
submissionTime.setText(submissionInfo
.get(CommonUtils.KEY_SUBMISSION_TIME));
executionTime.setText(submissionInfo.get(CommonUtils.KEY_RUNTIME));
return vi;
}
}
JSON已正确加载并解析。但问题是列表视图。
答案 0 :(得分:1)
你必须覆盖你的
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
正确,因为ListView认为你有0个项目,这就是它没有显示任何内容的原因。