如何在Android Studio中的AsyncDataClass中将数据从JSON填充到listview?

时间:2015-12-16 13:38:49

标签: android json listview android-asynctask

我目前正在创建一个简单的Android应用程序,使用php和JSON从Wamp DB获取数据。我的问题是如何在我的Activity中向我的ListView显示数据? 有人帮帮我吗?

这是我的呐喊。我使用了AsyncDataClass

private class AsyncDataClass extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... params)
    {
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);

        HttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(params[0]);

        String jsonResult = "";
        try
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", params[1]));
            nameValuePairs.add(new BasicNameValuePair("password", params[2]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);
            jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
        }
        catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }

        catch (IOException e)
        {
            e.printStackTrace();
        }
        return jsonResult;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result)
    {

        if (result.equals("") || result == null)
        {
            Toast msg = Toast.makeText(Crops.this, "Connection Failed!", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, 0, 0);//para naka-CENTER ang Toast (parang msgbox)---
            msg.show();
        }

        else
        {
            Toast msg = Toast.makeText(Crops.this, "Connected!", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER,0,0);//para naka-CENTER ang Toast (parang msgbox)-----
            msg.show();
        }

        try
        {
            JSONArray json = new JSONArray(result);

            for (int i = 0; i < json.length(); i++)
            {
                JSONObject e = json.getJSONObject(i);
                String cropID = e.getString("cropID");
                String crop = e.getString("cropID");



                String s1 ="Test1";
                String s2 ="Test2";
                String s3 ="Test3";
                ar.add(s1);
                ar.add(s2);
                ar.add(s3);

                String s4 ="Test4";
                ar.add(s4);

                //==============================================================================

            } //---End of FOR LOOP---//
        }//---end of TRY---//

        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }

    private StringBuilder inputStreamToString(InputStream is)
    {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try
        {
            while ((rLine = br.readLine()) != null)
            {
                answer.append(rLine);
            }
        }

        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return answer;
    }
}
//----------AsyncDataClass ends here------------------------------------------------------------

我创建了一个自定义列表适配器,并使用这些:

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

我可以将FIXED字符串加载到我的ListView中...但不是来自JSON ...请帮助......

修改

public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
    super(context, R.layout.mylist, itemname);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
}

public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

    txtTitle.setText(itemname[position]);
    imageView.setImageResource(imgid[position]);
    extratxt.setText("Description "+itemname[position]);
    return rowView;

};
}

0 个答案:

没有答案