在Android列表视图的每一行中添加编辑和删除按钮

时间:2019-07-11 06:34:00

标签: android

您好,在下面的代码中,我们创建了一个列表视图。屏幕中包含一个添加按钮。如果按下按钮,数据将作为json请求发送并从服务器获取响应。

如果响应成功,那么我将为每行添加每行添加和删除按钮。

任何人都可以帮助我在哪里弄错了

public class AddBuildingFragement extends Fragment {

    EditText et_building;
    TextView Add,mTitle,delete;
    ListView lv;
    String name;
    ArrayList<String> arrayList;
    ArrayAdapter<String> adapter;



    public AddBuildingFragement() {
    }

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



        //((NavigationViewActivity) getActivity ( )).setActionBarTitle ("LIGHT CONTROL");
        View rootView = inflater.inflate(R.layout.activity_building, container, false);


        init(rootView);

        return rootView;
    }

    private void clickListener() {
        Add.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {


                name=et_building.getText ().toString ();
                String level="1";
//                final ProgressDialog progressDialog = new ProgressDialog(getActivity ());
//                progressDialog.setIndeterminate(true);
//                progressDialog.setMessage("Authenticating...");
//                progressDialog.setCanceledOnTouchOutside(false);
//                progressDialog.setCancelable(false);
//                progressDialog.show();



                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(API.URL_BASE)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                API service = retrofit.create(API.class);

                try{
                    JSONObject parmobject=new JSONObject ();
                    parmobject.put("name",name);
                    parmobject.put("level",level);
                    Call <NewBuilding> userCall = service.getbuildinglist (parmobject.toString());
                    userCall.enqueue(new Callback <NewBuilding> () {
                        @Override
                        public void onResponse(Call<NewBuilding> call, Response <NewBuilding> response) {

//                            if (progressDialog != null)
//                                progressDialog.dismiss();
                            Integer response1= response.code();
                            Log.d ("response", String.valueOf (response1));
                            if (response !=null && response.isSuccessful()&&response.code()==200) {

                                String status=response.body ().getStatus ();

                                if(status.equalsIgnoreCase ("success")){
                                    makeText(getActivity (), "Building successfully Added", Toast.LENGTH_SHORT).show();
                                    arrayList.add (name);
                                    adapter.notifyDataSetChanged ();

                                }


                            } else {
                                makeText(getActivity (), "Invalid EmailId and password", Toast.LENGTH_SHORT).show();

                            }
                        }

                        @Override
                        public void onFailure(Call<NewBuilding> call, Throwable t) {

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


            }
        });


    }

    private void init(View rootView) {
        et_building=(EditText)rootView.findViewById (R.id.build_name);
        Add=(TextView)rootView.findViewById (R.id.addbuild);
        //delete=(TextView)rootView.findViewById (R.id.delete_item);
        lv=(ListView)rootView.findViewById (R.id.list_building);
        arrayList=new ArrayList<String> ();
        adapter=new ArrayAdapter<String> (getActivity (),R.layout.building_listview_item,arrayList);
         MyCustomAdapter adapter = new MyCustomAdapter(arrayList, this);

        //handle listview and assign adapter
        //ListView lView = (ListView)findViewById(R.id.my_listview);
        //lView.setAdapter(adapter);
        lv.setAdapter(adapter);
        clickListener();
    }

1 个答案:

答案 0 :(得分:0)

我认为问题来自adapter.notifyDataSetChanged(),因为它不起作用。 在这里:-

if(status.equalsIgnoreCase ("success")){
    makeText(getActivity (), "Building successfully Added", Toast.LENGTH_SHORT).show();
    arrayList.add (name);
    adapter.notifyDataSetChanged ();

}

可能的解决方案#1 :将Adapter类中的基本方法克隆为:

public void notifyDataSetChangedCustom(List<String> newdata) {
    mData.clear();
    mData.addAll(newdata);
    this.notifyDataSetChanged();
}

然后致电adapter.notifyDataSetChangedCustom(newdata)而不是adapter.notifyDataSetChanged


可能的解决方案2 :如果仍然无法运行,请仅使用notifyDataSetChanged 停止。相反,请尝试重新初始化适配器,然后调用notifyDataSetChanged

AttendanceAdapter adapter = new AttendanceAdapter(this, attendanceItems);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();

可能的解决方案3 :您收到的Retrofit响应为空。我认为可能有3个原因,

  1. 目标服务器没有响应
  2. 您忘记添加Internet权限。

  3. 在Android Pie上引入的
  4. The Invisible Killer ClearTextTraffic :通过将android:usesCleartextTraffic="true"添加到{{1}中的<application>标签中来解决}

希望它会对您有所帮助。