嘿伙计我试图显示来自远程网址的json数据包含图片和文字,并希望以片段形式显示?任何人都可以发布示例代码或参考最好的一个教程。我已经谷歌搜索但找不到正确的解决方案,因为我正在寻找。
这是我的MainActivity
package com.androidbelieve.drawerwithswipetabs;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_sent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_inbox) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
现在我想要显示PrimaryFragment Like;
package com.androidbelieve.drawerwithswipetabs;
import android.os.Bundle;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.androidbelieve.drawerwithswipetabs.model.AndroidVersion;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class PrimaryFragment extends AppCompatActivity implements LoadJSONTask.Listener, AdapterView.OnItemClickListener {
private ListView mListView;
public static final String URL = "https://api.learn2crack.com/android/jsonandroid/";
private List<HashMap<String, String>> mAndroidMapList = new ArrayList<>();
private static final String KEY_VER = "ver";
private static final String KEY_NAME = "name";
private static final String KEY_API = "api";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
mListView.setOnItemClickListener(this);
new LoadJSONTask(this).execute(URL);
}
@Override
public void onLoaded(List<AndroidVersion> androidList) {
for (AndroidVersion android : androidList) {
HashMap<String, String> map = new HashMap<>();
map.put(KEY_VER, android.getVer());
map.put(KEY_NAME, android.getName());
map.put(KEY_API, android.getApi());
mAndroidMapList.add(map);
}
loadListView();
}
@Override
public void onError() {
Toast.makeText(this, "Error !", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this, mAndroidMapList.get(i).get(KEY_NAME),Toast.LENGTH_LONG).show();
}
private void loadListView() {
ListAdapter adapter = new SimpleAdapter(PrimaryFragment.this, mAndroidMapList, R.layout.list_item,
new String[] { KEY_VER, KEY_NAME, KEY_API },
new int[] { R.id.version,R.id.name, R.id.api });
mListView.setAdapter(adapter);
}
}
Logcat是:
PrimaryFragment cannot be converted to Fragment
谢谢。
答案 0 :(得分:0)
首先你需要异步获取数据,然后将日期从json解析为相应的对象,然后在各自的视图中显示它们,在获得json并解析它之后你必须异步下载图像你可以从here <中学习它/ p>