私はこの質問を以前に聞いたことがありますが、ナビゲーション・ドロワーを開いて別のタブに移動してから戻ってみると、もう一度作業していませんでした。もう一度リストにデータを表示していません。ありがとうございました!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)とにかくそれをする。私の答えが問題に対処していない場合は、私に 'String url = "http://goldleafcomics.com/application/UniverseGlimpse.JSON";移動する必要があるので –