个性化适配器,尝试获取每个元素的ID并显示它

时间:2018-10-28 19:56:54

标签: java android android-recyclerview android-adapter

我目前正在使用个人适配器。 我每个人都有一些按钮。 一删除,一编辑,一添加到共享首选项。列表。

但是,我不知道如何从元素(例如ID)获取ID或特定属性。

换句话说,当我单击其中一个按钮时,我尝试获取ID(对象中的属性)以显示,稍后,我将实现该功能。 但是,我真的不明白在适配器中从哪里获取元素以及每个"user.info"的作用,以及在哪里声明import java.security.AccessControlException; public class TestSecurityManager { final static String securityPolicyFile = "properties_permissions.policy"; public static void main(String[] args) { System.setProperty("user.info", "123456"); System.out.println("user.info is : " + System.getProperty("user.info")); // Enable the security manager try { System.out.println("***"); System.out.println("Setting policy file"); System.out.println("***"); System.setProperty(securityPolicyFile, securityPolicyFile); System.out.println("***"); System.out .println("Security manager is STILL disabled, " + "read/write access to \"user.info\" system property " + "is allowed" ); System.out.println("***"); System.setProperty("user.info", "123456"); System.out.println("user.info is : " + System.getProperty("user.info")); // System.out.println("***"); System.out.println("Setting Security manager"); System.out.println("***"); SecurityManager securityManager = new SecurityManager(); System.setSecurityManager(securityManager); } catch (SecurityException se) { try { System.setProperty("user.info", "123456"); } catch (AccessControlException acew) { System.out.println("!!!Write access to the user.info system property is not allowed!"); } try { System.out.println("user.info is : " + System.getProperty("user.info")); } catch (AccessControlException acer) { System.out.println("Read access to the user.info system property is not allowed!"); } } } 函数?

现在是我的适配器(用于测试的Toastare):

grant {
  permission java.util.PropertyPermission "user.home", "read";
  permission java.util.PropertyPermission "user.info", "write";
};

这是我的对象类:

@Override

另外,这是我的MainActivity:

onClick

请随时向我建议链接,以便知道从哪里开始或要求任何规格。

谢谢您的时间。

setOnItemClickListner的问题:

package a.bogin.revmidterm.Adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import a.bogin.revmidterm.Models.StringInfos;

import a.bogin.revmidterm.R;

public class StringsAdapter extends RecyclerView.Adapter<StringsAdapter.ViewHolder> {


    private List<StringInfos> allStrings;

    public StringsAdapter(List<StringInfos> lst){
        this.allStrings = lst;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView string1;
        TextView string2;
        TextView string3;
        Button delB;
        Button editB;
        Button addPrefB;

        public ViewHolder(final View itemView) {
            super(itemView);

            string1 = itemView.findViewById(R.id.forS1);
            string2 = itemView.findViewById(R.id.forS2);
            string3 = itemView.findViewById(R.id.forS3);

            delB = itemView.findViewById(R.id.delBtn);
            delB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    }
            });

            editB = itemView.findViewById(R.id.editbtn);
            editB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(itemView.getContext(), "TEST", Toast.LENGTH_SHORT).show();
                    }
            });

            addPrefB = itemView.findViewById(R.id.pref);
            addPrefB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(itemView.getContext(), " C MON NOM", Toast.LENGTH_SHORT).show();
                }
            });
        }


    }

    @Override
    public void onBindViewHolder(final StringsAdapter.ViewHolder vh, final int position) {
        StringInfos str = allStrings.get(position);
        vh.string1.setText(str.getString1());
        vh.string2.setText(str.getString2());
        vh.string3.setText(str.getString3());


    }

    @Override
    public int getItemCount() {
        return allStrings.size();
    }


    @Override
    public StringsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Context ctx = parent.getContext();
        LayoutInflater inf = LayoutInflater.from(ctx);
        View createdViewForAdapter = inf.inflate(R.layout.string_adapter_infos, parent, false);
        ViewHolder vh = new ViewHolder(createdViewForAdapter);

        vh.editB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "TEEEEST", Toast.LENGTH_SHORT).show();
            }
        });


        return  vh;
    }

}

在myOwnRc之后,它是红色,但是,第二个被接受。

1 个答案:

答案 0 :(得分:0)

使用这些行显示活动中的数据后:

 mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);


 mAdapter = new MyAdapter(myDataset);
 mRecyclerView.setAdapter(mAdapter);

您可以设置 setonItemClickListener

mRecylerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           //here you can use the id or position to make changes to your data
           //Something like myDataset.add or myDataset.remove

           String one = myDataset.get(position).getString1();
        }
    });

编辑:更新了“主要活动”部分:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
myOwnRc.setLayoutManager(linearLayoutManager);
strAdapter = new StringsAdapter(listOfAll);
myOwnRc.setAdapter(strAdapter);
myOwnRc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
});