我正在尝试使用自定义 BaseAdapter 类将数据从远程服务器加载到列表视图。除了数据未显示在列表视图上之外,一切正常。日志 cat 上没有生成错误。下面是代码:
活动类:
public class MyAccountActivity extends Activity{
public static final String URL="http://***.co.ke/ussd/get_shares.php";
private ProgressDialog progressDialog;
private static final String TAG_NUMBER = "share_number";
private static final String TAG_NAME = "share_name";
ArrayList shareList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_account);
ArrayList sharelist = update_shares();
final ListView lv1 = (ListView) findViewById(R.id.mylist);
lv1.setAdapter(new CustomShareListAdapter(this, sharelist));
//ListView listview = getListView();
}
String data="";
public ArrayList update_shares(){
InputStream is=null;
String result="";
String key="admin";
ArrayList results=new ArrayList();
ArrayList<NameValuePair>user_details=new ArrayList<NameValuePair>();
user_details.add(new BasicNameValuePair("user",key));
try{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(URL);
httpPost.setEntity(new UrlEncodedFormEntity(user_details));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity entity=response.getEntity();
is=entity.getContent();
}
catch(Exception e){
Log.e("log tag","Error in connection"+e.toString());
}
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine()) != null){
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray=new JSONArray();
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String share=json_data.getString(TAG_NAME);
String number=json_data.getString(TAG_NUMBER);
ShareList myshares=new ShareList();
myshares.set_share_name(share);
myshares.set_share_number(number);
results.add(myshares);
}
Toast.makeText(getApplicationContext(), "Parsed ok",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return results;
}
}
自定义适配器类:
public class CustomShareListAdapter extends BaseAdapter {
private ArrayList listData;
private LayoutInflater layoutInflater;
public CustomShareListAdapter(Context context, ArrayList listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.share_list, null);
holder = new ViewHolder();
holder.nameView = (TextView) convertView.findViewById(R.id.company);
holder.numberView = (TextView) convertView.findViewById(R.id.share_number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ShareList shareList = (ShareList)listData.get(position);
holder.nameView.setText(shareList.get_share_name());
holder.numberView.setText(shareList.get_share_number());
return convertView;
}
static class ViewHolder {
TextView nameView;
TextView numberView;
}
}
这是 shareList 类;
public class ShareList {
private String share_name;
private String share_number;
public String get_share_name(){
return share_name;
}
public void set_share_name(String share_name){
this.share_name=share_name;
}
public String get_share_number(){
return share_number;
}
public void set_share_number(String share_number){
this.share_number=share_number;
}
public String to_string(){
return "[name="+share_name+",number="+share_number+"]";
}
}
帮助将不胜感激。谢谢。