gpt4 book ai didi

android - 处理 Fragment 和 listView 和 JSON

转载 作者:行者123 更新时间:2023-11-30 02:04:58 26 4
gpt4 key购买 nike

我正在尝试使用包含 4 个 fragment 的 MenuDrawer 制作一个应用程序。
这就是我的应用程序展示的方式。
当它启动时,它应该从网站检索数据。检索到的数据应该在第一个 fragment 的 ListView 中。
第二个 fragment 应该包含一个 webview 来查看我在 android 设备中的页面。
第三是显示一些文本细节。
当第一个 fragment 从网站获取详细信息时,我的应用程序崩溃。这是我的代码:
我的 fragment 布局如下:

我的第一个 fragment 包含以下内容:

<ListView
android:layout_marginTop="10dp"
android:layout_below="@+id/txtLabel"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background = "@android:color/transparent"
android:padding="3dp"
android:dividerHeight="1dp"
android:id="@+id/mlv"></ListView>

第一个 fragment 在应用程序启动时被调用。
我有一个列表适配器和一个运行良好的列表模型。

我的 Main 类包含一个获取数据的内部类。
我已经制作了模型类和适配器类:
这是我的主要代码:
  public class MainActivity extends Activity {

// declare properties
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mListView;
private ListViewNewsAdapter listViewNewsAdapter;
private ArrayList<ListViewNewsItem> listViewNewsItems;

private JSONParser jsonParser = new JSONParser();

private String READNEWS_URL =
"xyz-myweb-xyz.com";


private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.mlv);



// for proper titles
mTitle = mDrawerTitle = getTitle();

// initialize properties
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

// list the drawer items
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[4];

drawerItem[0] = new ObjectDrawerItem(R.drawable.home_icon, "Home");
drawerItem[1] = new ObjectDrawerItem(R.drawable.website_icon, "Website");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_share, "Help");

// Pass the folderData to our ListView adapter
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);

// Set the adapter for the list view
mDrawerList.setAdapter(adapter);

// set the item click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());





// for app icon control for nav drawer
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);



if (savedInstanceState == null) {
// on first time display view for first nav item
selectItem(0);
}
}

处理 fragment 的方法,它在 myMainActivity 里面:
     private void selectItem(int position) {

// update the main content by replacing fragments

Fragment fragment = null;

switch (position) {
case 0:
fragment = new CreateFragment();
new GetNewsTask().execute();
break;
case 1:

fragment = new ReadFragment();
break;
case 2:
fragment = new HelpFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);

} else {
// error in creating fragment
Log.e("MainAct10ity", "Error in creating fragment");
}

}

我使用了一个从网络检索数据的内部类:
  private class GetNewsTask extends AsyncTask<Void, Void, Boolean>
{
private ProgressDialog mProgressDialog;

private JSONObject jsonObjectResult = null;

private String error;

@Override
protected void onPreExecute()
{
super.onPreExecute();
listViewNewsItems = new ArrayList<ListViewNewsItem>();
mProgressDialog = ProgressDialog.show(MainActivity.this,
"Processing...", "Get last news", false, false);
}

@Override
protected Boolean doInBackground(Void... params)
{
jsonObjectResult = jsonParser.makeHttpRequest(READNEWS_URL, null);

if (jsonObjectResult == null)
{
error = "Error in the connection";
return false;
}

try
{
if (jsonObjectResult.getInt("success") == 1)
{
JSONArray jsonArray = jsonObjectResult.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject news = jsonArray.getJSONObject(i);

ListViewNewsItem listViewNewsItem = new ListViewNewsItem
(
news.getString("title"),
news.getString("type")
);
listViewNewsItems.add(listViewNewsItem);
}
return true;
}
else
error = jsonObjectResult.getString("message");

}
catch (Exception ex)
{

}

return false;
}

@Override
protected void onPostExecute(Boolean aBoolean)
{
super.onPostExecute(aBoolean);
mProgressDialog.dismiss();
if (aBoolean)
{
listViewNewsAdapter = new ListViewNewsAdapter(getApplicationContext(),
listViewNewsItems);
mListView.setAdapter(listViewNewsAdapter);
}
else
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
}
}

我必须在哪里使用
new GetNewsTask().execute();

我试图在 onCreate 中使用它,我的应用程序崩溃了。我试图在我的应用程序崩溃的开关中使用它。
你知道如何解决吗?

最佳答案

我通过以下方式修复了它:
将以下内容移至 fragment :

 private ListView mListView;
private ListViewNewsAdapter listViewNewsAdapter;
private ArrayList<ListViewNewsItem> listViewNewsItems;

private JSONParser jsonParser = new JSONParser();

private String READNEWS_URL =
"xxxxxxxx.xxxxx.xxxxx";


public CreateFragment() {
// Required empty public constructor
}

然后,在 onCreateView 内部
我创建 :

mListView = (ListView) rootView.findViewById(R.id.mlv);
    new GetNewsTask().execute();

之后,我将内部类写入 fragment 。好吧,我不得不更改一些参数以使我的代码运行良好。
然后,我运行它,它现在是工作属性。
谢谢

关于android - 处理 Fragment 和 listView 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com