无法使用hasmap创建自定义列表

时间:2015-05-10 09:12:11

标签: android adapter

package com.thesoft.smsdemo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.thesoft.smsdemo.JSONParser;

public class Upload extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        new Hi().execute();

    }


    private class Hi extends AsyncTask<String, Void, String> {
        ProgressDialog rDialog;
        JSONParser lv = new JSONParser();
        ListView vti ;
        public  HashMap<Integer, HashMap<String, String>> notes_data = new HashMap<Integer, HashMap<String, String>>();


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            rDialog = new ProgressDialog(Upload.this);
            rDialog.setMessage("Fetching Data From Server..");
            rDialog.setIndeterminate(false);
            rDialog.setCancelable(false);
            rDialog.show();


        }

        @Override
        protected String doInBackground(String... sam) {


            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("co_name", "All"));

            return lv.makeHttpRequest(
                            "+++++URL Link++++++++",
                            "GET", params);
        }

        @Override
        protected void onPostExecute(String result) {


            Log.d("value", result);

            String name = "", head = "", desb = "", date = "";
            JSONArray jsArray;
            try {

                jsArray = new JSONArray(result);
                Log.d("json arry", "" + jsArray.length());
                for (int j = 0; j < jsArray.length(); j++) {
                    HashMap<String, String> data = new HashMap<String, String>();
                    JSONObject jobj = jsArray.getJSONObject(j);
                    data.put("co_name", jobj.getString("co_name").toString());
                    data.put("head", jobj.getString("head").toString());
                    data.put("desb", jobj.getString("desb").toString());
                    data.put("date", jobj.getString("date").toString());
                    Log.d("valuesss",j+"");
                    notes_data.put(j, data);
                    Log.d("name",jobj.getString("co_name").toString());
                    Log.d("name",jobj.getString("head").toString());
                    Log.d("name",jobj.getString("date").toString());

                }

                /*String[] notes_da = {"hjhkh","hggg","hghgghgh"};
                for (int i = 0; i < notes_da.length; i++) {
                    notes_da[i] = "Notes " + i;
                    }*/

                MySimpleArrayAdapter mr = new MySimpleArrayAdapter(getApplicationContext(), notes_data);
/*              ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_list, R.id.tv, notes_da);
*/              vti=(ListView)findViewById(R.id.li1);
                vti.setAdapter(mr);
            } catch (JSONException e) {


                e.printStackTrace();
            } finally {

                rDialog.dismiss();


                Log.d("valuesss",notes_data.get(3).get("head"));
            }

        }

    }

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
        private final Context context;

        HashMap<Integer, HashMap<String, String>> data = new HashMap<Integer, HashMap<String, String>>();

        public MySimpleArrayAdapter(Context context, HashMap<Integer, HashMap<String, String>> data) {
            super(context, R.layout.custom_list);
            this.context = context;
            this.data = data;
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater
                    .inflate(R.layout.custom_list, parent);
            TextView textView = (TextView) convertView.findViewById(R.id.tv);
            TextView des = (TextView) convertView.findViewById(R.id.t2);
            TextView date = (TextView) convertView.findViewById(R.id.textView1);
            // change the icon for Windows and iPhone
            String head = data.get(position).get("head");
            String desb = data.get(position).get("desb");
            String date1 = data.get(position).get("date");

            textView.setText(head);
            des.setText(desb);
            date.setText(date1);


            return convertView;
        }

    }
    }

获取数据但无法在Listview的自定义适配器上设置数据 数据可以在log cat中看到,但列表活动未显示在设备上。请帮我解决此代码谢谢

获取数据但无法在Listview的自定义适配器上设置数据 数据可以在log cat中看到,但列表活动未显示在设备上。请帮我这个代码谢谢

0 个答案:

没有答案