在列表视图上选中项目后,开始新活动



我有一个带有列表视图的活动,以免视图在每一行中都有一些检查选项。

我想在我的代码中添加在选中复选框时启动活动的可能性

我该怎么做?

提前感谢!!

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_available_rides);
    // Get the Intent that started this activity
    Intent intent = getIntent();
    String messageTime = intent.getStringExtra(DisplayMessageActivity.EXTRA_MESSAGE);
    String messageDep = intent.getStringExtra(DisplayMessageActivity.EXTRA_MESSAGE1);
    String messageArr = intent.getStringExtra(DisplayMessageActivity.EXTRA_MESSAGE2);
    String messageUser = intent.getStringExtra(DisplayMessageActivity.EXTRA_MESSAGE6);
    // Capture the layout's TextView and set the string as its text
    TextView textView1 = findViewById(R.id.textView26);
    textView1.setText(messageTime);
    TextView textView2 = findViewById(R.id.textView2);
    textView2.setText(messageDep);
    TextView textView3 = findViewById(R.id.textView4);
    textView3.setText(messageArr);
    TextView textView4 = findViewById(R.id.textView30);
    textView4.setText(messageUser);

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference tripsRef = rootRef.child("Trips");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> listTime = new ArrayList<String>();
            ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_checked, listTime);
            ListView Time = (ListView) findViewById(R.id.Listtime);
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String arrival = ds.child("Arrival").getValue(String.class);
                String departure = ds.child("Departure").getValue(String.class);
                String time = ds.child("Time").getValue(String.class);
                String trips = TextUtils.join("    |    ", new String[]{time, departure});
                String Trips = TextUtils.join(" ---> ", new String[]{trips, arrival});
                listTime.add(Trips);
                Time.setAdapter(adapter);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    tripsRef.addListenerForSingleValueEvent(valueEventListener);
}

之后 Time.setAdapter(adapter(;...把这个:

 Time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //you can start your activity here
        }
    });

如果列表中完全是您想要的复选框,则必须使用适合您的列表的自定义复选框View

Realize OnItemClickListener 方法。您可以定义单击每个项目时应执行的操作。

用于启动新活动并传递一些内容

Time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        intent.putExtra("time", listTime.get(i));
        startActivity(intent);
    }
});

最新更新