我必须根据Firebase数据在ListView中设置Switch状态,它现在工作正常但我的ListView OnItemClickListener现在不工作

时间:2017-07-29 04:49:08

标签: android listview onitemclicklistener

这里OnItemclickListener没有被调用可以任何人都知道是什么问题?这是我的代码

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

    ListView listView = (ListView) findViewById(R.id.list_item);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String Name = pairName[position];
            Intent intent = new Intent(MainActivity.this, EditDetailsActivity.class);
            intent.putExtra("Name", Name);
            startActivity(intent);
        }
    });

    getSwitchStatus(pairName,listView);
}

public ListView getSwitchStatus(final String[] pairName1, final ListView listView1) {
    final ArrayList<Boolean> xz=new ArrayList<>();
    String x;

    for (int n =0 ;n<pairName1.length; n++) {
        x = pairName1[n];

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = database.getReference("data").child(x.replace("/",""));;
        DatabaseReference databaseReference11 = databaseReference.child("Switch");
        Log.v("value111",String.valueOf(n)+databaseReference11.toString());
        databaseReference11.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Boolean value = dataSnapshot.getValue(Boolean.class);

                xz.add(value);
                if (xz.size()==pairName1.length) {
                    sigAdapter adapter = new sigAdapter(MainActivity.this, pairName, imageId, xz);
                    listView1.setAdapter(adapter);
                }

                Log.v("valu",xz.toString());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    return listView1;
}

1 个答案:

答案 0 :(得分:0)

请试试这个, 可能有用。

这样的方法调用。

//holder.relQuestion is you row file main layout id so replace this with your id.

此方法放在您的适配器

holder.relQuestion.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             //here you can write your code
         }
    });