安卓应用程序。选项卡 fragment 上的 Mysql ListView

标签 android mysql listview android-fragments tabs

我将两个 Android 项目合并为一个。第一个是“Tab Fragments”项目,第二个是“自定义 ListView 内的多个 JSON PHP MySQL 数据”。

我想在第一个 fragment 选项卡上将 Mysql 数据库的数据显示为 ListView 。

这里有一张照片: My Project

这里出现错误: Tab1Fragment.java

enter package de.transporte_express.transporteexpress21;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;

/**
 * Created by User on 2/28/2017.
 */

public class Tab1Fragment extends Fragment{
    private static final String TAG = "Tab1Fragment";

    SwipeRefreshLayout mSwipeRefreshLayout;
    ListView SubjectFullFormListView;
    ProgressBar progressBar;
    String HttpURL = "http://example.com/script.php";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);


        getActivity().setContentView(R.layout.activity_courier_menu);
        SubjectFullFormListView = (ListView) getActivity().findViewById(R.id.container);

        new ParseJSonDataClass(this).execute();
    }

        public class ParseJSonDataClass extends AsyncTask<Void, Void, Void> {
            public Context context;
            String FinalJSonResult;
            List<Subject> SubjectFullFormList;

            public ParseJSonDataClass(Context context) {
                mSwipeRefreshLayout.setRefreshing(false);

                this.context = context;
            }


            @Override
            protected void onPreExecute() {

                super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... arg0) {

                HttpServiceClass httpServiceClass = new HttpServiceClass(HttpURL);

                try {
                    httpServiceClass.ExecutePostRequest();

                    if (httpServiceClass.getResponseCode() == 200) {

                        FinalJSonResult = httpServiceClass.getResponse();

                        if (FinalJSonResult != null) {

                            JSONArray jsonArray = null;
                            try {

                                jsonArray = new JSONArray(FinalJSonResult);
                                JSONObject jsonObject;
                                Subject subject;

                                SubjectFullFormList = new ArrayList<Subject>();

                                for (int i = 0; i < jsonArray.length(); i++) {

                                    subject = new Subject();

                                    jsonObject = jsonArray.getJSONObject(i);

                                    subject.Subject_Name = jsonObject.getString("shipping_date_c");

                                    subject.Subject_Full_Form = jsonObject.getString("shipping_time_c");

                                    subject.Billing_Address_City = jsonObject.getString("billing_address_city");

                                    subject.Billing_Address_Postalcode = jsonObject.getString("billing_address_postalcode");

                                    subject.Billing_Address_Country = jsonObject.getString("billing_address_country");

                                    subject.Arrival_Date = jsonObject.getString("arrival_date_c");

                                    subject.Arrival_Time = jsonObject.getString("arrival_time_c");

                                    subject.Shipping_Address_Postalcode = jsonObject.getString("shipping_address_city");

                                    subject.Shipping_Address_City = jsonObject.getString("shipping_address_postalcode");

                                    subject.Shipping_Address_Country = jsonObject.getString("shipping_address_country");

                                    subject.Description = jsonObject.getString("description");

                                    SubjectFullFormList.add(subject);
                                }
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    } else {

                        Toast.makeText(context, httpServiceClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result)

            {
                mSwipeRefreshLayout.setRefreshing(false);
                progressBar.setVisibility(View.GONE);

                SubjectFullFormListView.setVisibility(View.VISIBLE);

                if (SubjectFullFormList != null) {

                    ListAdapter adapter = new ListAdapter(SubjectFullFormList, context);

                    SubjectFullFormListView.setAdapter(adapter);

                }
            }
        }

    }

这里是 CourierActivityMenu.java:

package de.transporte_express.transporteexpress21;


import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

public class CourierActivityMenu extends AppCompatActivity {

    private static final String TAG = "CourierActivityMenu";

    private SectionsPageAdapter mSectionsPageAdapter;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courier_menu);
        Log.d(TAG, "onCreate: Starting.");

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "TAB1");
        adapter.addFragment(new Tab2Fragment(), "TAB2");
        adapter.addFragment(new Tab3Fragment(), "TAB3");
        adapter.addFragment(new Tab3Fragment(), "TAB4");
        viewPager.setAdapter(adapter);
    }

}

错误: 错误:(45, 32) 错误:类型不兼容:Tab1Fragment 无法转换为 Context 错误:任务“:app:compileDebugJavaWithJavac”执行失败。

Compilation failed; see the compiler error output for details.

有人有想法吗?谢谢

最佳答案

我认为这可能是您的SectionsPageAdapter。在 CourierActivityMenu 中,您将 mSectionsPageAdapter 设置为 onCreate 中的新实例,但您还在 setupViewPager 中创建了一个新的SectionsPageAdapter。该 Activity 不会了解您在设置方法中创建的 Activity 的任何信息。

关于安卓应用程序。选项卡 fragment 上的 Mysql ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47397636/

相关文章:

android - 如何在 Android 中属于 ListView 的自定义适配器中的一行中获取特定按钮并编辑其文本

android - 手动在谷歌地图上拖放图钉并相应地获取经度和纬度

android - 为偏好 Activity 提供我自己的 ListView

MySQL 查询 GROUP, COUNT

MYSQL 使用 WHERE 按 ID 列表搜索

c# - 使用类在 ListView 上实现初始排序

android - OverlayItem 中的可点击列表(适用于 Android 的 MapView)

android - 对话框关闭延迟 - Android

android - 有没有办法避免在带有自定义 View (Android)的 xml 中使用完整的包名称?

mysql - 不知道为什么 django South 试图进行反向迁移