将数据加载到listvcoiew时出错

时间:2013-07-01 10:54:57

标签: android listview

我正在尝试使用自定义BaseAdapter类从远程服务器将数据加载到listview。除了listview上没有显示数据外,一切正常。在日志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+"]";
}

 }

帮助将受到高度赞赏。谢谢。

2 个答案:

答案 0 :(得分:0)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

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> shareList=new ArrayList<ShareList>();

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);  

    shareList = update_shares();
    final ListView lv1 = (ListView) findViewById(R.id.mylist);
    lv1.setAdapter(new CustomShareListAdapter(this, sharelist));
    //ListView listview = getListView();

}

String data="";
public ArrayList<ShareList> update_shares(){
    InputStream is=null;
    String result="";
    String key="admin";

    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.add(new ShareList(share, number));
        }
        Toast.makeText(getApplicationContext(), "Parsed ok",Toast.LENGTH_SHORT).show();
    }
    catch(Exception e){
         Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return shareList;
}

}

如上所述更改您的活动

  public class ShareList {
        private String share_name;
        private String share_number;
        public String getShare_name() {
            return share_name;
        }
        public void setShare_name(String share_name) {
            this.share_name = share_name;
        }
        public String getShare_number() {
            return share_number;
        }
        public void setShare_number(String share_number) {
            this.share_number = share_number;
        }
        public ShareList(String share_name, String share_number) {
            super();
            this.share_name = share_name;
            this.share_number = share_number;
        }
        @Override
        public String toString() {
            return "ShareList [share_name=" + share_name + ", share_number="
                    + share_number + "]";
        }


}

如上所述更改您的共享列表

答案 1 :(得分:0)

我忘了在MyAccountActivity活动的try部分中解析结果。

JSONArray jArray=new JSONArray();

应该是

JSONArray jarray=new JSONArray(result);