Android – how to set buttons outside listview
•
Android
In my activity, I have a list view and a button. Each has a different purpose
My list view shows the details of an item in another activity
My button opens another activity
There is no problem with listview here. But my button doesn't work
In my code, I have used the following examples for listview and button
public class MainActivity extends AppCompatActivity implements ListView.OnItemClickListener{
private Button buttonaddticket;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity );
listView = (ListView) findViewById(R.id.srListView);
listView.setOnItemClickListener(this);
buttonaddticket = (Button) findViewById(R.id.btnaddticket);
//Setting listeners to button
buttonaddticket.setOnClickListener((View.OnClickListener) this);
}
.
.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, SingleTicket.class);
.
}
public void onClick(View v) {
if(v == buttonaddticket){
.
.
}
}
}
The question is how to implement onclicklistener for buttons in the same activity?
resolvent:
You can implement multiple listeners for your activity
public class MainActivity extends AppCompatActivity implements ListView.OnItemClickListener,View.OnClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// handle listview on item click
}
@Override
public void onClick(View view){
switch (view.getId()){
//handle multiple view click events
}
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码