I have a list view with that I have set up with 2 textview inside of it, on is on the right and one is on the left, and I want to set an onClickListerner for them and get the text of the one that was clicked, and also the row of the list view that was clicked.
I have been able to set up to get the row that was clicked but now I need help finding out which textview in the row was clicked.
Here is my set up for the cell in the list view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:id="@+id/1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"
android:id="@+id/2"
android:minHeight="60dp"
android:textSize="18dp"
android:gravity="center"
/>
</RelativeLayout>
Here is my MainActivity.java with the set up list view and what I have tried so far.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCarList();
populateListView();
registerClickCallback();
private void populateCarList() {
userDiningHall.add(new DiningHall("hall1", open1));
userDiningHall.add(new DiningHall("hall2", open2));
userDiningHall.add(new DiningHall("hall3", open3));
userDiningHall.add(new DiningHall("hall4", open4));
}
private void populateListView() {
ArrayAdapter<DiningHall> adapter = new MyListAdapter();
ListView list = (ListView)findViewById(R.id.homeListView);
list.setAdapter(adapter);
}
//THIS GETS WHAT ROW WAS CLICKED
private void registerClickCallback() {
ListView list = (ListView)findViewById(R.id.homeListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View ViewClicked, int position, long id) {
DiningHall clickedDiningHall = userDiningHall.get(position);
String message = "You clicked position " + position + " which is dining hall " +
clickedDiningHall.getDiningHallName();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
private class MyListAdapter extends ArrayAdapter<DiningHall> {
public MyListAdapter() {
super(MainActivity.this, R.layout.home_item_view, userDiningHall);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.home_item_view, parent, false);
}
//Find the dining hall
DiningHall currentDiningHall = userDiningHall.get(position);
//Fill the view
TextView diningHallText = (TextView)itemView.findViewById(R.id.itemDiningHallTextView);
diningHallText.setText(currentDiningHall.getDiningHallName());
TextView openText = (TextView)itemView.findViewById(R.id.itemOpenTextView);
openText.setText(currentDiningHall.getDiningHallOpen());
return itemView;
}
}
How can I get what row was clicked and what textview was clicked?
Thanks for the help in advance. :)
You can set the same View.OnClickListener
for both TextView
s. Then simply check the id of the clicked View
in onClick(View v)
by calling v.getId()
.