恢复从另一个目录中的片段显示我的列表视图了

时间:2019-02-11 05:05:39

标签: java android-studio listview android-arrayadapter android-adapter

我正在尝试开发android应用程序,但是我很新,遇到了问题。用户登录,并将其重定向到用户活动,并在这四个片段之一上打开,这些片段中的每个片段都有不同的作用。 JobFragment包含一个列表视图,以显示数据库中所有用户的作业。我有一个自定义的适配器,它扩展了基本适配器。问题是,当我在片段之间切换然后返回到Job片段时,列表视图不再显示.....我尝试了很多不同的操作,但似乎没有任何作用。如何进行这项工作,以及从数据库中删除ob时,它正在更新列表视图。我不知道应该在哪里更改代码或做错了什么。

public class JobListAdapter extends BaseAdapter {

    private Context aContext;
    private List<JobModel> aJobList;

    //Constructor


    public JobListAdapter(Context aContext, List<JobModel> aJobList) {
        this.aContext = aContext;
        this.aJobList = aJobList;
    }

    @Override
    public int getCount() {
        return aJobList.size();
    }

    @Override
    public Object getItem(int position) {
        return aJobList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final View v = View.inflate(aContext,R.layout.item_job_list,null);

        ImageButton deleteButton = v.findViewById(R.id.deleteButton);
        ImageButton editButton = v.findViewById(R.id.editButton);

        TextView jobIDTW = v.findViewById(R.id.itemListID);
        TextView jobTitleTW = v.findViewById(R.id.itemListTitle);
        TextView jobCategoryTW = v.findViewById(R.id.itemListCategory);
        TextView jobDescriptionTW = v.findViewById(R.id.itemListDescription);
        TextView jobAddressTW = v.findViewById(R.id.itemListAddress);
        TextView jobDueBytTW = v.findViewById(R.id.itemListDueBy);
        TextView jobBudgetTW = v.findViewById(R.id.itemListBudget);


        //set text for text view

        jobIDTW.setText(String.valueOf(aJobList.get(position).getId()));
        jobTitleTW.setText(aJobList.get(position).getTitle());
        jobCategoryTW.setText(aJobList.get(position).getCategory());
        jobDescriptionTW.setText(aJobList.get(position).getDescription());
        jobAddressTW.setText(aJobList.get(position).getStreet());
        jobDueBytTW.setText(String.valueOf(aJobList.get(position).getDate()));
        jobBudgetTW.setText(String.valueOf(aJobList.get(position).getBudget()));


        v.setTag(aJobList.get(position).getId());

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //your click action
                Toast.makeText(view.getContext(), "Item clicked: delete " + v.getTag(), Toast.LENGTH_LONG).show();

                int id = (int) v.getTag();

                removeRequest(id);
                //notifyDataSetChanged();

            }});

        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //your click action
                Toast.makeText(view.getContext(), "Item clicked: edit " + v.getTag(), Toast.LENGTH_LONG).show();

            }});

        return v;
    }
    public void removeRequest(int id){
        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    String success = jsonResponse.getString("success");

                    if (success.equals("1")) {

                        Toast.makeText(aContext, "Job removed successful.", Toast.LENGTH_LONG).show();

                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(aContext);

                        builder.setMessage("Login Failed")
                                .setNegativeButton("Retry", null)
                                .create()
                                .show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        RemoveJobRequest removeJobRequest = new RemoveJobRequest(id, responseListener);
        RequestQueue queue = Volley.newRequestQueue(Objects.requireNonNull(aContext));
        queue.add(removeJobRequest);
    }
}



/**
 * A simple {@link Fragment} subclass.
 */
public class JobsFragment extends Fragment {

    ArrayList<JobModel> jobModelList;
    JobListAdapter listViewAdapter;

    EmployerModel employer;

    String username;

    View view3;
    ListView listView;

    FragmentActivity listener;


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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Activity){
            this.listener = (FragmentActivity) context;
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        if(bundle != null){
            employer = bundle.getParcelable("Employer");

            username = employer.getNickname();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view3 = inflater.inflate(R.layout.fragment_jobs,container,false);

        listView = (ListView) view3.findViewById(R.id.listviewJob);


        return view3;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //listView = (ListView) view.findViewById(R.id.listviewJob);

    }

    private Date parseStringToDate(String sDate){

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat formatter2 = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat newFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = null;
        Date date2 = null;
        try {
            date = (Date)formatter.parse(sDate);
            String strDate = newFormat.format(date);
            Log.i("date", strDate);
            date2 = (Date)formatter2.parse(strDate);
            Log.i("date2", String.valueOf(date2));
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return date2;
    }

    public void setList(){

        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray (response);

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

                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        int id = jsonObject.optInt("jobID");
                        String title = jsonObject.optString("jobTitle");
                        String category = jsonObject.optString("jobCategory");
                        String description = jsonObject.optString("jobDescription");
                        String postCode = jsonObject.getString("jobPostCode");
                        int houseNumber = jsonObject.getInt("jobHouseNumber");
                        String streetName = jsonObject.optString("jobStreetName");
                        String area = jsonObject.optString("jobArea");
                        String date = jsonObject.optString("jobRangeDate");
                        //Date date = new Date();
                        int budget = jsonObject.getInt("jobBudget");

                        jobModelList.add(new JobModel(id, title, category, description, postCode, houseNumber, streetName, area, parseStringToDate(date), budget));

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };


        JobRequest jobRequest = new JobRequest(username, responseListener);
        RequestQueue queue = Volley.newRequestQueue(this.getActivity());
        queue.add(jobRequest);

    }

    @Override
    public void onResume() {
        super.onResume();

        jobModelList = new ArrayList<>();

        listViewAdapter = new JobListAdapter(getActivity(),jobModelList);
        listView.setAdapter(listViewAdapter);
        setList();
    }

}







    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".JobsFragment">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20sp"
            android:layout_marginBottom="25sp"
            android:background="@drawable/trans_white_rectangle"
            android:orientation="vertical">

            <ListView
                android:id="@+id/listviewJob"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10sp"
                android:layout_marginTop="10sp"
                android:layout_marginRight="10sp"
                android:background="#88A19E9E"
                android:divider="@color/colorGradient"
                android:dividerHeight="10sp">

            </ListView>
        </RelativeLayout>

    </FrameLayout>

</ScrollView>


    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/itemListID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/colorGradient"
        android:text="Job ID"
        />

            <TextView
                android:id="@+id/itemListTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_gravity="center_horizontal"
                android:textColor="@color/colorGradient"
                android:text="Job Ttile"
                />

    <ImageButton
        android:id="@+id/deleteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_delete_forever_black_24dp"
        android:text="Delete" />

    <ImageButton
        android:id="@+id/editButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_edit_black_24dp"
        android:text="Edit" />


    <TextView
        android:id="@+id/itemListCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:text="Category"/>

    <TextView
        android:id="@+id/itemListDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:text="Due by"/>

    <TextView
        android:id="@+id/itemListAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:text="Address"/>

    <TextView
        android:id="@+id/itemListDueBy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:text="Due by"/>

    <TextView
        android:id="@+id/itemListBudget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:text="Budget"/>


</LinearLayout>

0 个答案:

没有答案