无法使用onItemClickListener获取数据

时间:2017-04-22 01:43:03

标签: android firebase firebase-realtime-database onitemclicklistener

我想onItem单击列表视图并获取DetailsPage以显示详细信息,但它得到null。 ListView中的项目是图片网址,名称和地址。 DetailsPage中的项目是图像URL,名称,地址,联系人和类别。

Image of the view on my phone when get null

这是我的ListViewPage

public class ImageListActivity extends AppCompatActivity {

private DatabaseReference mDatabaseRef;
private List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_list);
    imgList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.listViewImage);
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait loading list image...");
    progressDialog.show();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference(WorkshopsInfoActivity.FB_DATABASE_PATH);

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            progressDialog.dismiss();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                ImageUpload img = snapshot.getValue(ImageUpload.class);
                imgList.add(img);
            }
            adapter = new ImageListAdapter(ImageListActivity.this, R.layout.image_item, imgList);
            lv.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            progressDialog.dismiss();
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(getApplicationContext(), DetailsOfWorkshops.class);
            startActivity(intent);
        }
    });
}}

这是我的详情页面

public class DetailsOfWorkshops extends AppCompatActivity {

DatabaseReference mDatabaseRef;
List<ImageUpload> listImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_of_workshops);
    mDatabaseRef = FirebaseDatabase.getInstance().getReference(WorkshopsInfoActivity.FB_DATABASE_PATH);
    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ImageUpload img = dataSnapshot.getValue(ImageUpload.class);

            String name = img.getName();
            String address = img.getAddress();
            String contact = img.getContact();
            String category = img.getCategory();


            final TextView textViewDetails = (TextView) findViewById(R.id.tvName);
            String string = "Name: " + name + "\nAddress: " + address + "\nContact: " + contact + "\nCategory: " + category + "\n\n";
            textViewDetails.setText(string);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}}

3 个答案:

答案 0 :(得分:0)

您不向详细信息类发送任何内容:

Intent intent = new Intent(getApplicationContext(), DetailsOfWorkshops.class);
        startActivity(intent);

尝试使用:

YourClass item = (YourClass) adapterView.getAdapter().getItem(position);
Intent intent = new Intent(getApplicationContext().this, DetailsOfWorkshops.class);
Bundle bundle = new Bundle();
bundle.putString("valueName", value);
intent.putExtra("MyPackage", bundle);
startActivity(intent);

答案 1 :(得分:0)

我们需要通过intent将ActivityOne中的一些数据发送到ActivityTwo,然后在ActivityTwo中访问该数据并将其设置为所需的textview。

检查ActivityOne中的以下代码。

public class ImageListActivity extends AppCompatActivity {

private DatabaseReference mDatabaseRef;
public List<ImageUpload> imgList;
private ListView lv;
private ImageListAdapter adapter;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_list);
    imgList = new ArrayList<>();
    lv = (ListView) findViewById(R.id.listViewImage);
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait loading list image...");
    progressDialog.show();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference(WorkshopsInfoActivity.FB_DATABASE_PATH);

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            progressDialog.dismiss();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                ImageUpload img = snapshot.getValue(ImageUpload.class);
                imgList.add(img);
            }
            adapter = new ImageListAdapter(ImageListActivity.this, R.layout.image_item, imgList);
            lv.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            progressDialog.dismiss();
        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            ImageUpload itemData = imgList.get(i);
            Intent intent = new Intent(getApplicationContext(), DetailsOfWorkshops.class);
            intent.putExtra("name", itemData.getName());
            intent.putExtra("address", itemData.getAddress());
            intent.putExtra("contact", itemData.getContact());    
            intent.putExtra("category", itemData.getCatogory());    
            startActivity(intent);
        }
    });
}}

查看ActivityTwo中的一些更改

public class DetailsOfWorkshops extends AppCompatActivity {

DatabaseReference mDatabaseRef;
List<ImageUpload> listImage;

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

    String name = getIntent().getStringExtra("name");
    String address = getIntent().getStringExtra("address");
    String contact = getIntent().getStringExtra("contact");
    String category = getIntent().getStringExtra("category");


    TextView textViewDetails = (TextView) findViewById(R.id.tvName);
    String string = "Name: " + name + "\nAddress: " + address + "\nContact: " + contact + "\nCategory: " + category + "\n\n";
    textViewDetails.setText(string);

    mDatabaseRef = FirebaseDatabase.getInstance().getReference(WorkshopsInfoActivity.FB_DATABASE_PATH);
    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ImageUpload img = dataSnapshot.getValue(ImageUpload.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}}

希望这有用。

@Rajesh

答案 2 :(得分:0)

尝试使用onItemSelected侦听器而不是onItemClick侦听器,这将为您提供可以帮助您获取所需数据的特定对象的位置 然后你可以发送具有适当值的额外内容

        ImageUpload itemData = imgList.get(pos);
        Intent intent = new Intent(getApplicationContext(), 
        DetailsOfWorkshops.class);
        intent.putExtra("name", itemData.getName());
        intent.putExtra("address", itemData.getAddress());
        intent.putExtra("contact", itemData.getContact());    
        intent.putExtra("category", itemData.getCatogory());    
        startActivity(intent);

OnClick侦听器适用于整个视图,而不是特定元素.OnSelected给出了您在listView上选择的项目的位置