0

我正在尝试使用自定义 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+"]";
}

 }

帮助将不胜感激。谢谢。

4

2 回答 2

0

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

JSONArray jArray=new JSONArray();

应该

JSONArray jarray=new JSONArray(result);
于 2013-07-01T12:13:29.407 回答
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 + "]";
        }


}

像上面一样更改您的共享列表

于 2013-07-01T11:06:59.200 回答