Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用异步任务和BaseAdapter进行Json解析_Android_Json_Android Asynctask_Baseadapter_Android Adapter - Fatal编程技术网

Android 使用异步任务和BaseAdapter进行Json解析

Android 使用异步任务和BaseAdapter进行Json解析,android,json,android-asynctask,baseadapter,android-adapter,Android,Json,Android Asynctask,Baseadapter,Android Adapter,我正在列表视图中访问并显示姓名、id和性别。我不知道如何从doInBackground和BaseAdapter调用结果 public class FbMainActivity extends Activity { Button b; ListView lv; DefaultHttpClient client; HttpGet get; HttpResponse response; HttpEntity entity; ArrayList<FBAccount> al; MyAdapte

我正在
列表视图中访问并显示姓名、id和性别。我不知道如何从
doInBackground
和BaseAdapter调用结果

public class FbMainActivity extends Activity {
Button b;
ListView lv;
DefaultHttpClient client;
HttpGet get;
HttpResponse response;
HttpEntity entity;
ArrayList<FBAccount> al;
MyAdapter adapter;
TextView tv1, tv2, tv3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fb_main);
    b = (Button) findViewById(R.id.bFAce);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);
    tv3 = (TextView) findViewById(R.id.textView3);

    lv = (ListView) findViewById(R.id.list);
    al = new ArrayList<FBAccount>();
    adapter = new MyAdapter();
    lv.setAdapter(adapter);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MyOperation operation = new MyOperation();
            operation.execute();
        }
    });
}

private class MyOperation extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(FbMainActivity.this);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog.setMessage("Loading....");
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        client = new DefaultHttpClient();
        get = new HttpGet("http://graph.facebook.com/somename");
        try {
            response = client.execute(get);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        entity = response.getEntity();
        InputStream is = null;
        try {
            is = entity.getContent();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = null;
        do {
            try {
                line = reader.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            buffer.append(line);
        } while (line != null);
        String str = buffer.toString();
        FBAccount account;
        JSONObject object;
        try {
            object = new JSONObject(str);
            String id = object.getString("id");
            String name = object.getString("name");
            String gender = object.getString("gender");

            account = new FBAccount();
            account.setId(id);
            account.setName(name);
            account.setGender(gender);

            al.add(account);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        adapter.notifyDataSetChanged();
        return str;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();


    }
}

private class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return al.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return al.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(
                R.layout.cust_layout, null);

        String id = al.get(position).getId();
        String name = al.get(position).getName();
        String gender = al.get(position).getGender();
        tv1.setText(id);
        tv2.setText(name);
        tv3.setText(gender);
        return layout;
    }

}
公共类FbMainActivity扩展活动{
按钮b;
ListView lv;
默认HttpClient客户端;
HttpGet;
HttpResponse响应;
http实体;
ArrayList al;
MyAdapter适配器;
文本视图tv1、tv2、tv3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u fb\u main);
b=(按钮)findViewById(R.id.bFAce);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
lv=(ListView)findViewById(R.id.list);
al=新的ArrayList();
适配器=新的MyAdapter();
低压设置适配器(适配器);
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
MyOperation操作=新的MyOperation();
operation.execute();
}
});
}
私有类MyOperation扩展了异步任务{
ProgressDialog=新建ProgressDialog(FbMainActivity.this);
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
setMessage(“加载…”);
dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
client=新的DefaultHttpClient();
get=新的HttpGet(“http://graph.facebook.com/somename");
试一试{
response=client.execute(get);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
entity=response.getEntity();
InputStream=null;
试一试{
is=entity.getContent();
}捕获(非法状态){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(is));
StringBuffer=新的StringBuffer();
字符串行=null;
做{
试一试{
line=reader.readLine();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
buffer.append(行);
}while(line!=null);
String str=buffer.toString();
FBI账户;
JSONObject对象;
试一试{
对象=新的JSONObject(str);
stringid=object.getString(“id”);
字符串名称=object.getString(“名称”);
字符串性别=object.getString(“性别”);
账户=新账户();
帐户设置id(id);
account.setName(名称);
账户。设置性别(性别);
新增(账户);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
adapter.notifyDataSetChanged();
返回str;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
dialog.dismise();
}
}
私有类MyAdapter扩展了BaseAdapter{
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回al.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回al.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
LinearLayout布局=(LinearLayout)getLayoutInflater()。充气(
R.layout.cust_布局,空);
字符串id=al.get(position.getId();
String name=al.get(position.getName();
字符串gender=al.get(position.getGender();
tv1.setText(id);
tv2.setText(名称);
tv3.setText(性别);
返回布局;
}
}

}

从api获取数据后,使用
onPostExecute
方法更新ListView。通过调用
adapter.notifyDataSetChanged()
内部
onPostExecute
而不是
doInBackground
,将代码更改为:

 @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

       // update listView here

      adapter.notifyDataSetChanged();

      dialog.dismiss();

    }

从api获取数据后,使用
onPostExecute
方法更新ListView。通过调用
adapter.notifyDataSetChanged()
内部
onPostExecute
而不是
doInBackground
,将代码更改为:

 @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

       // update listView here

      adapter.notifyDataSetChanged();

      dialog.dismiss();

    }

您在活动中错误地初始化了
TextView
。它应该位于适配器的
getView
方法中,并且应该使用
LinearLayout布局
实例访问它,如下所示:

尝试如下所示:


您在活动中错误地初始化了
TextView
。它应该位于适配器的
getView
方法中,并且应该使用
LinearLayout布局
实例访问它,如下所示:

尝试如下所示:


您需要在getView()方法中初始化TextView

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.row_student_sms, parent, false);
    YourModelClass model = al.get(position);

    final TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
    tv1.setText(model.getId());

    TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
    tv2.setText(model.getName());

    TextView tv3 = (TextView) rowView.findViewById(R.id.textView3);
    tv2.setText(model.getGender());

    return rowView;
 }

您需要在getView()方法中初始化TextView

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.row_student_sms, parent, false);
    YourModelClass model = al.get(position);

    final TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
    tv1.setText(model.getId());

    TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
    tv2.setText(model.getName());

    TextView tv3 = (TextView) rowView.findViewById(R.id.textView3);
    tv2.setText(model.getGender());

    return rowView;
 }

lv=(ListView)findViewById(R.id.list);al=新的ArrayList();适配器=新的MyAdapter();低压设置适配器(适配器);adapter.notifyDataSetChanged()不工作…@DroidLearner:只需使用adapter.notifyDataSetChanged();在onpost内部执行并检查它