完整的noob在這裏,請耐心等待。我有一個從我的服務器加載數據並在Listview中顯示它的活動。現在我需要能夠重新排列基於TabHost的列表。我使用MaterialTabHost(https://github.com/neokree/MaterialTabs)從TabHost填充Listview片段
下面是我用它來填充ListView代碼:
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
final Double finalLat = lat;
final Double finalLon = lon;
final Double finalLat1 = lat;
final Double finalLon1 = lon;
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
viewer movie = new viewer();
LatLng point1 = new LatLng(obj.getDouble("lat"), obj.getDouble("long"));
LatLng point2 = new LatLng(finalLat, finalLon);
distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
movie.setTitle(obj.getString("first_name"));
movie.setThumbnailUrl(obj.getString("pic"));
//movie.setRating(((String) obj.getString("experience")));
movie.setYear(Double.parseDouble(String.format("%.2f", distanceInMiles)));
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String clicked_at = null;
try {
clicked_at = response.getString(position);
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(getApplicationContext(), full_view_walker.class);
i.putExtra("data", clicked_at);
i.putExtra("lat", finalLat1);
i.putExtra("lon", finalLon1);
i.putExtra("sender_id", sender_id);
startActivity(i);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
現在我的標籤的東西:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_listview);
tabHost = (MaterialTabHost) this.findViewById(R.id.tabHost);
pager = (ViewPager) this.findViewById(R.id.pager);
// init view pager
adapter_page = new ViewPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter_page);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
tabHost.setSelectedNavigationItem(position);
}
});
// insert all tabs from pagerAdapter data
for (int i = 0; i < adapter_page.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(adapter_page.getPageTitle(i))
.setTabListener(this)
);
}
@Override
public void onTabSelected(MaterialTab tab) {
pager.setCurrentItem(tab.getPosition()); }
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int num) {
return new FragmentText();
}
@Override
public int getCount() {
return 3;
}
private String[] lst = {"Distance", "Experience", "History"};
@Override
public CharSequence getPageTitle(int position) {
return lst[position];
}
}
而且我已經在這裏FragmentText:
public class FragmentText extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.front_list, container, false);
}
}
現在我真的需要2件東西:
1)如何將FragmentText中的inflater.inflate連接到加載列表並加載它的代碼? 2)如何確保每次用戶切換選項卡時不必重新加載,並且列表被加載/排序一次,然後將從那裏重新加載?
編輯:我在FragmentText試過這樣:
public class FragmentText extends Fragment{
// Log tag
private static final String TAG = AvailableList.class.getSimpleName();
String sender_id;
double distanceInMiles;
Double lat = null;
Double lon = null;
private ProgressDialog pDialog;
private List<viewer> movieList = new ArrayList<viewer>();
private ListView listView;
private CustomListAdapter adapter;
private String[] leftSliderData = {"Logout", "Contact Us"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lon = getArguments().getDouble("lon");
lat = getArguments().getDouble("lat");
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.front_list, container, false);
listView = (ListView) V.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
Log.d("Adapter", adapter.toString()+"");
listView.setAdapter(adapter);
final Double finalLat = lat;
final Double finalLon = lon;
final Double finalLat1 = lat;
final Double finalLon1 = lon;
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
Log.d(TAG, response.toString());
//hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
viewer movie = new viewer();
LatLng point1 = new LatLng(obj.getDouble("lat"), obj.getDouble("long"));
LatLng point2 = new LatLng(finalLat, finalLon);
distanceInMiles = LatLngTool.distance(point1, point2, LengthUnit.MILE);
movie.setTitle(obj.getString("first_name"));
movie.setThumbnailUrl(obj.getString("pic"));
movie.setYear(Double.parseDouble(String.format("%.2f", distanceInMiles)));
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String clicked_at = null;
try {
clicked_at = response.getString(position);
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(getActivity(), full_view_walker.class);
i.putExtra("data", clicked_at);
i.putExtra("lat", finalLat1);
i.putExtra("lon", finalLon1);
i.putExtra("sender_id", sender_id);
startActivity(i);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return V;
}
這是加載頁面時,我打一個標籤,但他們沒有露出來了!不過,我可以看到正在加載的列表的活動!
嘿,謝謝你的回答。第一部分顯然是最重要的(當它顯示時,我將不會重新加載數據等)。目前正在顯示front_list佈局,但它是空白的。所以這個列表沒有被填充到片段視圖中。你有什麼建議如何解決這個問題?我可以告訴你的任何其他代碼? – user1610719
@ user1610719我查看了您的網絡代碼,但沒有看到將新數據添加到適配器列表的位置。在你從json創建查看器對象的for循環中你不應該添加'movieList.add(movie)'這行來將新創建的電影添加到適配器的列表中嗎? – Luksprog
AHH!那個非常愚蠢的男人,我一直堅持這幾天。非常感謝......我會研究你談到的其他事情,並嘗試自己解決這些問題。謝謝! – user1610719