单击 ListView 项目时如何加载 Firebase DB 值的详细活动

Posted

技术标签:

【中文标题】单击 ListView 项目时如何加载 Firebase DB 值的详细活动【英文标题】:How to load Firebase DB Vaule on detail activity when ListView Item Clicked 【发布时间】:2018-01-16 23:01:56 【问题描述】:

我正在开发使用 Firebase 数据库作为后端的 android 应用程序。列出从我的 Json 密钥中检索到的项目。例如:-

 
    "Player3" : 
        "LY" : "ewfw",
        "Ty" : "rwerw"
      
  

然后,“LY”和“TY”将加载到作为列表视图项目(工作正常)。在我想要的是当“LY”或“TY”(列表视图项目)单击“ewfw”或“rwerw”时将显示在详细活动中。问题是仅在详细活动上显示键而不是值。我如何加载详细活动的价值? 这是我的代码...

ListItemActivity.Java

public class ListItemActivity5 extends Fragment 
    private static final String TAG = "Tab4Fragment";
    //String[] values = new String[]"", "", "", "", "", "", "", "", "";
    ListView listView;
    TextView textView;
    Array array;
    RelativeLayout relativeLayout;
    ViewGroup.LayoutParams layoutparams;
    private DatabaseReference mDatabase;
    RelativeLayout container;
    AnimationDrawable anim;
    Context context;

    //private TextView text;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.activity_conferance, container, false);
        //text = (TextView) view.findViewById(R.id.textView1);
        listView = (ListView) view.findViewById(R.id.list);
        container = (RelativeLayout) view.findViewById(R.id.container5);
        final TextView textView1 = (TextView) view.findViewById(R.id.textView1);
        anim = (AnimationDrawable) container.getBackground();
        anim.setEnterFadeDuration(2000);
        anim.setExitFadeDuration(3000);
        final ListView listView = (ListView) view.findViewById(R.id.list);
        final ArrayList<String> friends = new ArrayList<String>();

        mDatabase = FirebaseDatabase.getInstance().getReference("Player");
        mDatabase.addValueEventListener(new ValueEventListener() 
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) 
                friends.clear();
                for (final DataSnapshot player : dataSnapshot.getChildren()) 
                    Log.i("Player", player.getKey());
                    friends.add(player.getKey());
                    player.getValue();


                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) 
                            player.getValue();
                            //player2 = (String) String.valueOf(player.getValue());
                            //String topic = String.valueOf(player.getValue());
                            //topic = (String) parent.getItemAtPosition(position);
                            Intent intent = new Intent(getContext(), Details.class);

                            intent.putExtra("key", String.valueOf(player.getValue()));

                            startActivity(intent);


                        

                    );
                
                ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,
                        friends) 

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

                        View view = super.getView(position, convertView, parent);

                        layoutparams = view.getLayoutParams();
                        TextView textView = (TextView) view.findViewById(android.R.id.text1);

            /*YOUR CHOICE OF COLOR*/
                        textView.setTextColor(Color.WHITE);
                        textView.setTextSize(22);

                        //Define your height here.
                        //layoutparams.height = 240;

                        view.setLayoutParams(layoutparams);

                        return view;


                    
                ;
                listView.setAdapter(arrayAdapter);
            

            @Override
            public void onCancelled(DatabaseError databaseError) 

            


        );

        return view;
    


Details.Java

public class Details extends Activity 
    RelativeLayout container;
    AnimationDrawable anim;
    ScrollView mScrollView;
    TextView textView1;
    private DatabaseReference mDatabase;
    private ListItemActivity1 mylist1;
    private DataSnapshot dataSnapshot;
    FloatingActionButton floatingActionButton;
    private ListItemActivity5 listItemActivity5;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ab1);

        textView1 = (TextView) findViewById(R.id.textView1);
        //mref = new Firebase("https://tafach-love-2.firebaseio.com/");
        Intent intent = getIntent();

        if(intent.hasExtra("key"))
            textView1.setText(intent.getStringExtra("key"));
        
        textView1.setMovementMethod(new ScrollingMovementMethod());

 

感谢您的帮助。

【问题讨论】:

您面临的问题是什么? 当我将 Friends.get(position) 放在 Intent 上时,会显示 getKey()。我想要的是要显示的键的值 您是否从 Firebase DB 获得了正确的值? 我正在从 Firebase DB 获取密钥,但我需要从 Firebase DB 获取值以获取详细信息 将加载来自 Firebase DB 的列表视图键,当单击列表项时,我希望键的相应值显示在另一个活动上 【参考方案1】:

我的问题得到了答案。我将这些行放在 List ItemActivity 上的代码中,并在 ChildEventListner 而不是 AddEventLister 中设置 onclickListner ...

 mDatabase.addChildEventListener(new ChildEventListener() 
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) 
                final String playerName = dataSnapshot.getValue(String.class);
                hy.add(playerName);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) 
                        //player.getValue();
                        //player2 = (String) String.valueOf(player.getValue());
                        //String topic = String.valueOf(player.getValue());
                        //topic = (String) parent.getItemAtPosition(position);
                        Intent intent = new Intent(getContext(), Details.class);

                        intent.putExtra("key", String.valueOf(hy.get(position)));

                        startActivity(intent);


                    

                );
            

【讨论】:

以上是关于单击 ListView 项目时如何加载 Firebase DB 值的详细活动的主要内容,如果未能解决你的问题,请参考以下文章

单击 Listview 中的项目时,如何在新活动中从 Firebase 检索数据?

将“标题”项目保存到 ListView 并单击项目以将数据加载到新活动

单击后如何禁用 ListView 项目?

单击图像时如何更改ListView中的图像?

如何修复“所有单击的 Listview 项目都可以使用相同的数据”

如何在单击android中的listview项目时从数据库中获取id