运行应用程序后第一次json解析listview显示为空

时间:2016-05-03 12:28:56

标签: java android json listview android-studio

我正在处理与新闻相关的应用程序,我已成功解析json响应并在listview中显示数据。

但是listview第一次没有加载数据并且仍然是空的。

第二次它在listview中显示已解析的数据。

可能是什么问题?以下是我的代码

// This is my java file:
public class FunHallListActivity extends AppCompatActivity {
    FunHallDBHandler funhallhandler;
    SQLiteDatabase db;

    InputStream is=null;
    String resultFunHall = null;
    String line=null;
    int code;
    String URL = "";

    private Context context;
    private ListView fhListview;
    private FunHallAdapter adapterfunhall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_hall_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        fhListview=(ListView)findViewById(R.id.funhall_lv);

        context=this;
        new FunHallFetcherTask().execute();
        funhallhandler=new FunHallDBHandler(context);

        ArrayList<FunHall> fhDataList = new ArrayList<FunHall>();

        fhDataList = funhallhandler.getAllFunHall();
        adapterfunhall = new FunHallAdapter(context, fhDataList);
        fhListview.setAdapter(adapterfunhall);
    }

    //Json parsing code with to fech and add into Sqlite Database
    class FunHallFetcherTask extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... params) {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://cmr.newsoft.co.in/FunctionalHall.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                is = entity.getContent();
                Log.d("pass 1", "connection success ");
                Log.d("Data:",is.toString());
            }
            catch(Exception e)
            {
                Log.e("Fail 1", e.toString());
            }
            try
            {
                BufferedReader reader = new BufferedReader
                        (new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                is.close();
                resultFunHall = sb.toString();
                Log.d("Json_string_Result", resultFunHall);
                Log.e("pass 2", "connection success ");

            }
            catch(Exception e)
            {
                Log.d("Fail 2", e.toString());
            }
            // Log.e(result,"hello");

            try
            {
                JSONArray jArrayFunHall = new JSONArray(resultFunHall);

                for(int i=0; i<jArrayFunHall.length();i++)
                {
                    Log.d("jArrayFunHall.length()", ""+jArrayFunHall.length());
                    JSONObject json_data = jArrayFunHall.getJSONObject(i);
                    publishProgress();
                    code=1;
                    String funhall_Name = json_data.getString("Name");
                    String funhall_Address = json_data.getString("Address");
                    String funhall_Phone = json_data.getString("Phone");
                    FunHall funhall=new FunHall();
                    funhall.setFHallName(funhall_Name);
                    funhall.setFHalladdress(funhall_Address);
                    funhall.setFHallContact(funhall_Phone);
                    funhallhandler.addFunHall(funhall);
                }
            }
            catch(Exception e)
            {
                Log.e("Fail 3", e.toString());
            }
            //Json Parsing code end
            return null;
        }
    }
}


// and this is my adapter

public class FunHallAdapter extends BaseAdapter {
    private List<FunHall> funHallList;
    private Context context;
    private LayoutInflater layoutInflater;

    public FunHallAdapter(Context context, List<FunHall> funHallList)
    {
        this.context = context;
        this.funHallList = funHallList;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return funHallList.size();
    }

    @Override
    public FunHall getItem(int position) {
        return funHallList.get(position);
    }

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

    public  class  Holder
    {
        TextView tvfunHallName,tvfunHalladdress,tvfunHallcontact;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        if (convertView == null)
        {
            holder = new Holder();
            convertView = layoutInflater.inflate(R.layout.funhall_list_item,null);
            holder.tvfunHallName=(TextView)convertView.findViewById(R.id.funhallname_tv);
            holder.tvfunHalladdress=(TextView)convertView.findViewById(R.id.funhalladdress_tv);
            holder.tvfunHallcontact=(TextView)convertView.findViewById(R.id.hunhallcantact_tv);
            convertView.setTag(holder);
        }else
        {
            holder = (Holder) convertView.getTag();
        }
        holder.tvfunHallName.setText(funHallList.get(position).getFHallName());
        holder.tvfunHalladdress.setText(funHallList.get(position).getFHalladdress());
        holder.tvfunHallcontact.setText(funHallList.get(position).getFHallContact());

        return convertView;
    }
}

3 个答案:

答案 0 :(得分:0)

我认为你必须声明setAdapter是onPostExecute。在onPreExecute或doInBackground之前不要声明。我认为这是问题..

答案 1 :(得分:0)

问题是您在更改数据时没有通知适配器。

按照步骤操作 1.修改构造函数

legendCallback: function(chart) {
                console.log(chart.data);
                var text = [];
                text.push('<ul>');
                for (var i=0; i<chart.data.datasets[0].data.length; i++) {
                    text.push('<li>');
                    text.push('<span style="background-color:' + chart.data.datasets[0].backgroundColor[i] + '">' + chart.data.datasets[0].data[i] + '</span>');
                    if (chart.data.labels[i]) {
                        text.push(chart.data.labels[i]);
                    }
                    text.push('</li>');
                }
                text.push('</ul>');
                return text.join("");
            }

第2步。 在ArrayList<FunHall> funHallList; public FunHallAdapter(Context context, ArrayList<FunHall> funHallList) { this.context = context; this.funHallList = funHallList; layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } 添加此方法

FunHallAdapter
  1. 在您的FunHallListActivity

    class FunHallFetcherTask扩展了AsyncTask public void swapData(ArrayList<FunHall> funHallList){ this.funHallList = funHallList; notifyDataSetChanged(); } {

    <Void,Void,String>

    }

答案 2 :(得分:0)

我发现我做了什么错误,我按照以下方式解决了这个问题,

@Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        ArrayList<FunHall> fhDataList = new ArrayList<FunHall>();
        fhDataList = funhallhandler.getAllFunHall();
        adapterfunhall = new FunHallAdapter(context, fhDataList);
        fhListview.setAdapter(adapterfunhall);
    }