因此,我之前問過這個問題,但在嘗試打開導航抽屜並轉到其他選項卡然後返回之後,它不再工作。它不再在列表中顯示數據。先謝謝了!ListFragment不顯示列表中的數據
主要活動
package com.goldleaf.branden.goldleafcomics;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
ListView lvGlimpse;
public List<UniverseListing>universalListings = new ArrayList<UniverseListing>();
final GlimpseListFragment glimpse = new GlimpseListFragment();
NavigationView navigationView;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://goldleafcomics.com/application/UniverseGlimpse.JSON";
PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
@Override
public void processFinish(String s) {
universalListings = new JsonConverter<UniverseListing>().toArrayList(s, UniverseListing.class);
Toast.makeText(MainActivity.this, "Application Data Refreshed", Toast.LENGTH_LONG).show();
}
});
task.execute(url);
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, glimpse);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Glimpse");
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_news) {
getSupportActionBar().setTitle("G.C. News");
} else if (id == R.id.nav_team) {
TeamFeedFragment fragment = new TeamFeedFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
getSupportActionBar().setTitle("Team Feed");
fragmentTransaction.commit();
}else if(id == R.id.nav_notifications){
getSupportActionBar().setTitle("Notifications");
}else if(id == R.id.nav_comics){
getSupportActionBar().setTitle("Comics");
}else if(id == R.id.nav_glimpse){
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, glimpse);
getSupportActionBar().setTitle("Glimpse");
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
UniverseListing
package com.goldleaf.branden.goldleafcomics;
import com.google.gson.annotations.SerializedName;
public class UniverseListing {
@SerializedName("universe_id")
public String universeId;
@SerializedName("universe_glimpse")
public String universeGlimpse;
@SerializedName("universe_url")
public String universeuRL;
}
掠影名單片段
package com.goldleaf.branden.goldleafcomics;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.*;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
public class GlimpseListFragment extends ListFragment {
List<UniverseListing> universalListings = new ArrayList<UniverseListing>();
List<String> glimpse = new ArrayList<String>();
List<String> titles = new ArrayList<String>();
public GlimpseListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_glimpse_list, container, false);
return rootView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainActivity act = new MainActivity();
universalListings = act.universalListings;
ArrayList<String> glimpse = new ArrayList<String>();
for(UniverseListing value: universalListings){
glimpse.add(value.universeGlimpse);
}
ArrayList<String> titles = new ArrayList<String>();
for(UniverseListing value: universalListings){
titles.add(value.universeId);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String value = (String)getListAdapter().getItem(position);
Toast.makeText(getActivity(), value, Toast.LENGTH_LONG).show();
}
}
決不做'新MainActivity();'...你可以從片段調用'getActivity' –
我這樣做是因爲我從它調用一個變量,我不能使用getActivity –
1)你將需要投它2)你不應該需要無論如何要這樣做。如果我下面的答案沒有解決問題,那麼讓我知道 –