Android应用。选项卡片段上的Mysql列表视图

时间:2017-11-20 17:20:56

标签: android mysql listview android-fragments tabs

我将两个Android Project合二为一。一个是“Tab Fragments”项目,第二个是“Custom ListView中的多个JSON PHP MySQL数据”。

我想在第一个片段选项卡上将我的Mysql Db的数据显示为列表视图。

这里有一张照片: 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'执行失败。

  

编译失败;有关详细信息,请参阅编译器错误输出。

有人有想法吗?感谢

1 个答案:

答案 0 :(得分:0)

我认为它可能是你的SectionsPageAdapter。在CourierActivityMenu中,您将mSectionsPageAdapter设置为onCreate中的新实例,但您还可以在setupViewPager中创建新的SectionsPageAdapter。活动对您在设置方法中创建的活动一无所知。