在列表视图上检查项目时启动新活动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在列表视图上检查项目时启动新活动相关的知识,希望对你有一定的参考价值。
我有一个listview的活动,lest视图在每一行都有一些检查选项。
我想在我的代码中添加选中复选框时启动活动的可能性
我怎样才能做到这一点?
提前致谢!!
@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(适配器)之后; ...把这个:
Time.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//you can start your activity here
}
});
如果它是列表中您想要的复选框,则必须使用自定义适合您的listView
另一答案
实现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);
}
});
以上是关于在列表视图上检查项目时启动新活动的主要内容,如果未能解决你的问题,请参考以下文章