Fading Action Bar Android在List中只显示一个项目

时间:2013-12-27 12:03:25

标签: android android-listview android-actionbar

我试图在我的应用程序中使用着名的Fading Action Bar(FAB)库,但我遇到了一个奇怪的问题。如果我不使用FAB库,我的listview加载就好了但是如果我使用FAB库,listview中只有一个元素而不是10.

我已将FAB库导入为库项目,并为其提供所需的适当资源。这是我的代码:

 public class JobList extends Activity {
    ArrayList<String> jobList;
    ListView lv;
    ProgressDialog progress;
    HashMap<String,JobItem> jobMap;
    HashMap<String,String> positionAndIdMap;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_job_list);

     FadingActionBarHelper helper = new FadingActionBarHelper()
     .actionBarBackground(R.drawable.ab_background)
     .headerLayout(R.layout.header)
     .contentLayout(R.layout.activity_job_list);
     setContentView(helper.createView(this));
     helper.initActionBar(this);


    lv = (ListView) findViewById(R.id.list);
    jobList = new ArrayList<String>();
    jobMap = new HashMap<String, JobItem>();
    positionAndIdMap = new HashMap<String, String>();

    String url = "https://cs5.salesforce.com/services/data/v20.0/query";
    url += "?q="+ URLEncoder.encode("SELECT Id,Name,tc9_ti__Client_Name__c,Date_Posted_Formatted__c,ts2__Employment_Type__c,ts2__Job_Description__c,ts2__Status__c from ts2__Job__c Limit 10");
    requestWithSomeHttpHeaders(url, getApplicationContext());

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long arg3) {
            // TODO Auto-generated method stub
            String jobId = positionAndIdMap.get(""+position);
            JobItem item = jobMap.get(jobId);
            Intent jobIntent = new Intent(getApplicationContext(), JobDetail.class);
            jobIntent.putExtra("obj",item);
            startActivity(jobIntent);
            Toast.makeText(getApplicationContext(), ""+jobList.get(position), 500).show();
        }
    });
}


public void requestWithSomeHttpHeaders(String reqUrl, Context context){
    RequestQueue queue = Volley.newRequestQueue(context);
    String finalResponse ="";
    Context con = context;
    String url = reqUrl;

    StringRequest postRequest = new StringRequest(Request.Method.GET, url, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response", response);
                progress.dismiss();
                try {
                    JSONObject jObj = new JSONObject(response);
                    try {
                        // Getting Array of Contacts
                        JSONArray records = jObj.getJSONArray("records");

                        // looping through All Contacts
                        for(int i = 0; i < records.length(); i++){
                            JSONObject c = records.getJSONObject(i);

                            // Storing each json item in variable
                            String title = c.getString("Name");
                            String Id = c.getString("Id");
                            String jobStatus = c.getString("ts2__Status__c");
                            String datePosted = c.getString("Date_Posted_Formatted__c");
                            String clientName = c.getString("tc9_ti__Client_Name__c");
                            String employmentType = c.getString("ts2__Employment_Type__c");
                            String jobDescription = c.getString("ts2__Job_Description__c");

                            JobItem jItem = new JobItem(Id,title,clientName,datePosted,employmentType,jobDescription,jobStatus);
                            jobList.add(title);
                            Log.d("Job list:"+i, ""+title);
                            positionAndIdMap.put(""+i,Id);
                            jobMap.put(Id, jItem);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("Json:", ""+jObj);
                } catch (JSONException e) {
                    Log.e("JSON Parser", "Error parsing data " + e.toString());
                }
               if(jobList.size()>0){

                   lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, jobList));
               }else{
                   setContentView(R.layout.empty);
               }

            }
        }, 
        new Response.ErrorListener() 
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Log.d("ERROR","error => "+error.toString());
            }
        }
    ) {     
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError { 
                Map<String, String>  params = new HashMap<String, String>(); 
                String sessionId = RequestHandler.TOKEN;
                params.put("X-PrettyPrint", "1");
                params.put("Authorization", "OAuth " + sessionId);
                return params;  
        }
    };

    queue.add(postRequest);

    progress = ProgressDialog.show(this, "Please Wait...",
            "Loading List Of Jobs..."); 
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.job_list, menu);
    return true;
}

 }

上面的代码编译没有任何错误,但List只包含一个元素。我有什么想法我做错了吗?如果我从上面的代码中删除FAB部分并正常设置内容,那么列表将加载10个元素。任何帮助将非常感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

我也遇到过这个。解决方案是你的listView的ID必须是android.R.id.list,因为这是他用来确定你是否有listView的id,否则它会回退到scrollView。