【问题标题】:After running application first time json parsed listview showing empty在第一次运行应用程序后,json 解析的列表视图显示为空
【发布时间】:2016-05-03 12:28:55
【问题描述】:

我正在开发一个与新闻相关的应用程序,我已成功解析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;
    }
}

【问题讨论】:

  • 显示一些代码以便我们可以查明错误
  • @Yashoda 请。发布您到目前为止完成的代码。
  • 是的,我添加了代码片段 pz 只需检查一次

标签: java android json listview android-studio


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    按照步骤 1.修改你的构造函数

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

    第 2 步。 在你的FunHallAdapter 添加这个方法

     public void swapData(ArrayList<FunHall> funHallList){
          this.funHallList = funHallList;
          notifyDataSetChanged();
    }
    
    1. 在你的 FunHallListActivity 中

      类 FunHallFetcherTask 扩展 AsyncTask&lt;Void,Void,String&gt; {

       @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");
              return resultFunHall ;
      
          }
          @Override
          protected void onPostExecute(String s) {
              super.onPostExecute(s);
              if (!TextUtils.isEmpty(s)) {
                  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);
                  }
                  adapterfunhall.swapData(funhallhandler.getAllFunHall());
              }
              catch(Exception e)
              {
                  Log.e("Fail 3", e.toString());
              }
      
              }
          }
      

      }

    【讨论】:

      【解决方案3】:

      我发现我做错了什么,我解决了如下,

      @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);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多