我试图在我的应用程序中使用著名的 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;
}
}
上面的代码编译没有任何错误,但是列表只包含一个元素。知道我做错了什么吗?如果我从上面的代码中删除 FAB 部分并正常设置内容,那么列表将加载 10 个元素。任何帮助将不胜感激。
谢谢!
最佳答案
我也遇到过。解决方案是您的 listView 的 ID 必须是 android.R.id.list
因为这是他用来确定您是否有 listView 的 ID,否则它会回退到 scrollView。
关于android - 淡化操作栏 Android 仅显示列表中的一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800402/