listview异步加载网络数据,不能成功加载,请教

Android 码拜 9年前 (2016-06-08) 968次浏览
本人放入的模拟数据可以加载,后台也成功获取到数据了,就是无法加载
请教一个,没空回答给个相关学习链接也可以!

package com.yanz.machine.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.yanz.machine.myapplication.adapter.SearchAdapter;
import com.yanz.machine.myapplication.entity.SCurrentStock;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
public class DeletaeActivity extends AppCompatActivity {
    private String uri = "/stock/findStock";
    private ImageView ivDeleteText;
    private EditText etSearch;
    private Button btSearch;
    private ListView lvResult;
    private List<SCurrentStock> sCurrentStocks = new ArrayList<SCurrentStock>();
    private SearchAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deletae);
        ivDeleteText = (ImageView) findViewById(R.id.iv_del_deleteText);
        etSearch = (EditText) findViewById(R.id.et_del_search);
        btSearch = (Button) findViewById(R.id.bt_del_search);
        lvResult = (ListView) findViewById(R.id.lv_del_result);
        adapter = new SearchAdapter(this,sCurrentStocks);
        lvResult.setAdapter(adapter);
        ivDeleteText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
            }
        });
        etSearch.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
            }
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub
            }
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    ivDeleteText.setVisibility(View.GONE);
                } else {
                    ivDeleteText.setVisibility(View.VISIBLE);
                }
            }
        });
        btSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DeletaeActivity.this,"search",Toast.LENGTH_SHORT).show();
                SCurrentStock s2 = new SCurrentStock();
                s2.setFcsQuantity(100.0);
                s2.setCcsPartName("更改测试名字");
                s2.setCcsPartStd("更改测试guige");
                s2.setCcsWhName("更改测试仓库");
                sCurrentStocks.add(s2);
                adapter.notifyDataSetChanged();
                initData();
            }
        });
    }
    private void initData(){
        Log.e("yanz","开始加载数据");
        //String url = HttpUtil.BASE_URL+uri;
        String url = "http://192.168.1.109:8080/graduation/stock/findStock";
        RequestParams params = new RequestParams();
        params.put("pArtStdF",etSearch.getText().toString());
        //params.put("pArtStdS"," ");
        params.put("partName","不锈钢钢板");
        //params.put("whCode","011101");
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(url, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(DeletaeActivity.this,"错误:"+statusCode,Toast.LENGTH_LONG).show();
            }
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    String[] message = responseString.split("@@");
                    String result = message[1];
                    ObjectMapper objectMapper = new ObjectMapper();
                    sCurrentStocks = objectMapper.readValue(
                            result,
                            new TypeReference<List<SCurrentStock>>() {}
                    );
                    sCurrentStocks.get(0).getCcsWhName();
                    sCurrentStocks.get(0).getCcsPartName();
                    //adapter = new SearchAdapter(DeletaeActivity.this,sCurrentStocks);
                    //lvResult.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
解决方案

10

http://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&stoken=aaec7f180c3a96aaf3736aade2325d5d271ecbaee2ae16b12f2c162c4913655da06fb9305bcc0faae8edcaba7a310ba4bccd80cd401e06acdeea6d92c819cd41931c01aa8d7c&bduss=20875db2ee926ec6cb75fb6803614b90db6e2f68af7b552c9fa8df3afc6c2b87a6f9f511b1980e0c546e2055a8af5bf78b965fa2650dd7f04dad78b0a5811e2b6b4ce599950d4d86905b2528046eef8d908c64c15b5865583d60306bb712ca1c08a20eafc7a98ba9367373d29bbba2bb606d70d8a6931cb0be6bba34da5dea3ad650eea7f36d0624f8044490a688ca4586067141f1ac957b163216fc0e9769cd5445cea1eda2150eda9dea1c5500e944176290785eadabe791d9040c747ac8ef3b163fc6fa15#list/path=%2F

10

 sCurrentStocks = objectMapper.readValue(
result,
new TypeReference<List<SCurrentStock>>() {}
);
你这里sCurrentStocks 已经改变了引用了,adapter里面的数据不再指向sCurrentStocks 了,所以没更新。

10

sCurrentStocks.clear();
sCurrentStocks.addAll(objectMapper.readValue(
result,
new TypeReference<List<SCurrentStock>>() {}
));

10

用 Picasso多好

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明listview异步加载网络数据,不能成功加载,请教
喜欢 (0)
[1034331897@qq.com]
分享 (0)