Click here to Skip to main content
14,734,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help to fix this problem. i want remove item in list item in programmatically , here is my code...

absensi_team.java
package com.example.eis2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.eis2.Item.ListViewAdapterCutiKhusus;
import com.example.eis2.Item.ListViewAdapterTeam;
import com.example.eis2.Item.absensiteammodel;
import com.example.eis2.Item.cutikhususmodel;
import com.example.eis2.Item.namanikmodel;

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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.example.eis2.Item.ListViewAdapterTeam.lokasi;
import static com.example.eis2.Item.ListViewAdapterTeam.nikbaru2;
import static com.example.eis2.Item.LoginItem.KEY_NIK;
import static com.example.eis2.izin.txt_jabatan;
import static com.example.eis2.izin.txt_nomor;
import static com.example.eis2.izinfull.txt_lokasi;
import static com.example.eis2.spv_absensi.txt_jabatan_struktur;
import static com.example.eis2.spv_absensi.txt_lokasi_struktur;


public class absensi_team extends AppCompatActivity {
    public ListView list;
    private List<absensiteammodel> team;
    private List<namanikmodel> movieItemList;

    public static TextView lokasi2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_absensi_team);
        list = findViewById(R.id.list_team);
        team = new ArrayList<>();
        lokasi2 = (TextView) findViewById(R.id.lokasiteam);
        movieItemList = new ArrayList<>();

        getbiodata();
        loadPlayer();
    }

        private void getbiodata () {
            String nik_baru = getIntent().getStringExtra(KEY_NIK);
            StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://hrd.tvip.co.id/rest_server/api/login/index?nik_baru=" + nik_baru,
                    new com.android.volley.Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            try {
                                JSONObject obj = new JSONObject(response);
                                JSONArray movieArray = obj.getJSONArray("data");

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

                                    JSONObject movieObject = movieArray.getJSONObject(i);

                                    namanikmodel movieItem = new namanikmodel(
                                            movieObject.getString("nama_karyawan_struktur"),
                                            movieObject.getString("level_jabatan_karyawan"),
                                            movieObject.getString("lokasi_struktur"),
                                            movieObject.getString("jabatan_struktur")
                                    );
                                    movieItemList.add(movieItem);
                                    lokasi2.setText(movieItem.getLokasi_struktur());

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

        }


    private void loadPlayer() {
        String jabatan = txt_jabatan_struktur.getText().toString().trim();
        System.out.println("test 111 = " + jabatan);

        StringRequest stringRequest1 = new StringRequest(Request.Method.GET, "http://hrd.tvip.co.id/rest_server/master/team/index?jabatan_struktur=" + jabatan,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            final JSONObject obj = new JSONObject(response);
                            JSONArray movieArray = obj.getJSONArray("data");

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

                                final JSONObject movieObject = movieArray.getJSONObject(i);

                                final absensiteammodel movieItem = new absensiteammodel(
                                        movieObject.getString("nik_baru"),
                                        movieObject.getString("nama_karyawan_struktur"),
                                        movieObject.getString("jabatan_karyawan"),
                                        movieObject.getString("lokasi_struktur"));

                                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                                    Intent i = new Intent(absensi_team.this, detail_absensi.class);
                                        i.putExtra(KEY_NIK, nikbaru2.getText().toString());
                                        startActivity(i);
                                        System.out.println("Test :" + nikbaru2);
                                    }
                                });
                                team.add(movieItem);
                            }


                            ListViewAdapterTeam adapter = new ListViewAdapterTeam(team, getApplicationContext());

                            list.setAdapter(adapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), "Maaf, anda belum pernah mengajukan cuti khusus", Toast.LENGTH_SHORT).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest1);
    }
}


ListViewAdapterTeam.java
package com.example.eis2.Item;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.eis2.R;
import com.example.eis2.absensi_team;
import com.example.eis2.detail_absensi;

import java.util.List;

import static com.example.eis2.Item.LoginItem.KEY_NIK;
import static com.example.eis2.absensi_team.lokasi2;
import static com.example.eis2.kehadiran.list;
import static com.example.eis2.spv_absensi.txt_jabatan_struktur;
import static com.example.eis2.spv_absensi.txt_lokasi_struktur;

public class ListViewAdapterTeam extends ArrayAdapter<absensiteammodel> {

    List<absensiteammodel> team;

    private Context context;
    public static TextView nikbaru2, lokasi;

    public ListViewAdapterTeam(List<absensiteammodel> team, Context context) {
        super(context, R.layout.list_view_absensi_team, team);
        this.team = team;
        this.context = context;
    }


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

        LayoutInflater inflater = LayoutInflater.from(context);

        View listViewItem = inflater.inflate(R.layout.list_view_absensi_team, null, true);

        nikbaru2 = listViewItem.findViewById(R.id.nik);
        TextView nama = listViewItem.findViewById(R.id.nama);
        TextView jabatan = listViewItem.findViewById(R.id.jabatan);
        lokasi = listViewItem.findViewById(R.id.lokasi);

        absensiteammodel movieItem = team.get(position);

     

        nikbaru2.setText(movieItem.getNikbaru());
        nama.setText(movieItem.getNama_karyawan_struktur());
        jabatan.setText(movieItem.getJabatan_struktur());
        lokasi.setText(movieItem.getLokasi_struktur());

 if (!lokasi2.getText().toString().contentEquals(lokasi.getText().toString())){
            listViewItem.setVisibility(View.INVISIBLE);
        }

        return listViewItem;
    }
}

absensiteammodel.java

package com.example.eis2.Item;

public class absensiteammodel {
    private String nik_baru;
    private String nama_karyawan_struktur;
    private String jabatan_struktur;
    private String lokasi_struktur;

    public absensiteammodel(String nik_baru, String nama_karyawan_struktur, String jabatan_struktur, String lokasi_struktur) {
        this.nik_baru = nik_baru;
        this.nama_karyawan_struktur = nama_karyawan_struktur;
        this.jabatan_struktur = jabatan_struktur;
        this.lokasi_struktur = lokasi_struktur;
    }
    public String getNikbaru() {return nik_baru; }

    public String getNama_karyawan_struktur() {
        return nama_karyawan_struktur;
    }

    public String getJabatan_struktur() {
        return jabatan_struktur;
    }

    public String getLokasi_struktur() {
        return lokasi_struktur;
    }
}


i wanna remove the item programmaticaly (auto), in this case.. it removes the item when i scrolldown..

this the code which use it for remove item

if (!lokasi2.getText().toString().contentEquals(lokasi.getText().toString())){
           listViewItem.setVisibility(View.INVISIBLE);
       }


I will so grateful, if someone solve this problem

What I have tried:

if (!lokasi2.getText().toString().trim().equalsIgnoreCase(ListViewAdapterTeam.lokasi.getText().toString().trim())) {
                                team.remove(movieItemList);
                                adapter.notifyDataSetInvalidated();
                            }


i already change that in absensi_team.java
but always says
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
Posted
Updated 4-Oct-20 4:31am
v3
Comments
David Crow 5-Oct-20 9:22am
   
After the item has been removed from the array, you need to call notifyDataSetChanged().

As far as the exception goes, you have four calls to getText() in the code you've shown. One of those is throwing a NullPointerException because the object being used to call getText() is null. You can easily find out which one using the debugger.

Your adapter object should really be created and assigned in the onCreate() method rather than in an anonymous inner class.
Komang Putra 5-Oct-20 21:58pm
   
Can you give me an example ?
i still don't get it
David Crow 5-Oct-20 22:09pm
   
"Can you give me an example ?"

To what, exactly?

i still don't get it"

Which part?
Komang Putra 5-Oct-20 22:15pm
   
i meaan four calls to getText(), what's that mean ?

1 solution

(My condolences: The Android documentation sucks.)

If you can't figure out how to remove, try reloading (and syncing) the list view (without the "removed" item).

I've found that reloading a (reasonable) list view doesn't necessarily mean the ui is adversely affected or slowed down (but I'm using a Windows tablet).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900