I'm trying to implement Onclicklistener for button inside Listview item but unable to get OnitemClick method called in listview.
I got the following code snippet from stack overflow
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
( break point)
click++;
}
});
The break point never goes off when i click the button.
Activity Code
public class MainActivity extends AppCompatActivity {
long click=0;
String[] aOrderID = {"1-AA-355433", "2-AA-456474", "3-AA-43546464", "4-A-446464E6","5-A-43555" };
String[] aOrderType = {
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY"
};
double[] aPrice={3353.33, 4567.77, 335.66, 535.77, 678.67};
double[] aVOL={93353.33, 94567.77, 9335.66, 9535.77, 9678.67};
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter whatever = new CustomListAdapter(this,
aOrderID, aOrderType,aPrice, aVOL );
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(whatever);
////////////////////////
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
// CODE NEVER GETS HER AFTER BUTTON IS CLICKED ON
click++;
}
});
}
}
CUSTOM ADAPTER
public class CustomListAdapter extends ArrayAdapter {
int ted;
//to reference the Activity
private final Activity context;
//to store the animal images
private Integer[] imageIDarray = null;
//List of order ids
private String[] aOrderID = null;
// Type of order id limit buy
private String[] aOrderType = null;
// Price, price to sell of buy at
private double[] aPrice = null;
// Amount of bitcoins to buy or sell
private double[] aVol = null;
public CustomListAdapter(Activity context,
String[] aOrderID, String[] aOrderType,
double[] aPrice, double[] aVol) {
super(context, R.layout.row2, aOrderID);
this.context = context;
this.aOrderID = aOrderID;
this.aOrderType = aOrderType;
this.aPrice = aPrice;
this.aVol = aVol;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
return rowView;
}
}
XML FOR LISTVIEW
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView33" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Oder Type "
app:layout_constraintStart_toEndOf="#+id/textView33"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/IDtYPE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limit Order"
app:layout_constraintStart_toEndOf="#+id/textView35"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Volume "
app:layout_constraintStart_toEndOf="#+id/textView34"
app:layout_constraintTop_toBottomOf="#+id/textView35" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type::"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/idOrderID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limmit"
app:layout_constraintStart_toEndOf="#+id/textView40"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView40" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_marginTop="4dp"
android:text="Delete Order"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView34" />
<TextView
android:id="#+id/textView45"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="-------------------------------------------------"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<TextView
android:id="#+id/idPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000.00"
app:layout_constraintStart_toEndOf="#+id/textView43"
app:layout_constraintTop_toBottomOf="#+id/idOrderID" />
<TextView
android:id="#+id/idVol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.9999"
app:layout_constraintStart_toEndOf="#+id/textView37"
app:layout_constraintTop_toBottomOf="#+id/textView35" />
You should use button click event from this method. Try this.
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
Button btn = (Button) rowView.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff.
}
});
return rowView;
}
Related
I have a Fragment class that can display everything in my database in a ListView, and I have an inner class that extends SimpleCursorAdapter to use buttons within my ListView.
So in each element of my list view, I have several TextView's and 2 button. With my following code, I can use a button listener for a button inside my ListView. But inside my listener, I want to get the content of the TextView inside the same element that the button I click on. But When I click on a button to display my TextView, it display the last element of my ListView and I don't know why !
For example if I have 3 elements in my ListView like this :
_id = 1, name = "bob"
_id = 2, name = "john"
_id = 3, name = "bobby"
Each of these element are displayed with a button to display there ID, but if I click on the button inside bob, I get "id = 3". Same for john and bobby. And if I had a new element, I get his ID
My listener is in the bind function in my inner class MySimpleCursorAdapter.
Here is my Fragment Class :
public class ViewCardEditor extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String authority = "com.example.jean.cartememoire.CardContentProvider";
public String[] from;
public final int[] to = {R.id.idList, R.id.themeList, R.id.questionList, R.id.reponseList, R.id.difficultList};
StockCard stock;
ViewGroup container;
ListView listView;
MySimpleCursorAdapter adapter;
private ArrayList<String> data = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup c,
Bundle savedInstanceState) {
container = c;
View view = inflater.inflate(R.layout.fragment_view_card_editor, container, false);
stock = new StockCard(c.getContext());
from = new String[]{stock._ID,
stock.THEME,
stock.QUESTION,
stock.REPONSE,
stock.DIFFICULTE};
// Inflate the layout for this fragment
if (container != null) {
container.removeAllViews();
}
//listView.setAdapter(new MyListAdapter(container.getContext(), R.layout.card_stock, data));
//View view_cs = LayoutInflater.from(container.getContext()).inflate(R.layout.card_stock, null);
//Supprimer = (Button) view_cs.findViewById(R.id.buttonDelete);
//Modifier = (Button) view_cs.findViewById(R.id.buttonModifier);
databaseView(view);
return view;
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri.Builder builder = new Uri.Builder();
Uri uri = builder.scheme("content").authority(authority)
.appendPath(stock.STOCK_TABLE).build();
return new CursorLoader(container.getContext(), uri, from,
null, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
public void databaseView(View view)
{
ArrayList<String> list;
Cursor cursor = stock.getData();
listView = (ListView) view.findViewById(R.id.listView);
adapter = new MySimpleCursorAdapter(container.getContext(), R.layout.card_stock, null, from, to,0);
listView.setAdapter(adapter);
LoaderManager manager = getLoaderManager();
manager.initLoader(0, null, this);
}
public void deleteOneCard(int id)
{
Uri.Builder builder = new Uri.Builder();
builder.scheme("content").authority(authority).appendPath(stock.STOCK_TABLE);
ContentUris.appendId(builder, id);
Uri uri = builder.build();
ContentResolver resolver = container.getContext().getContentResolver();
resolver.delete(uri, null, null);
}
private class MySimpleCursorAdapter extends SimpleCursorAdapter
{
ViewHolder vh;
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.card_stock, parent, false);
vh = new ViewHolder();
vh.idList = (TextView) view.findViewById(R.id.idList);
vh.themeList = (TextView) view.findViewById(R.id.themeList);
vh.questionList = (TextView) view.findViewById(R.id.questionList);
vh.reponseList = (TextView) view.findViewById(R.id.reponseList);
vh.difficulteList = (TextView) view.findViewById(R.id.difficultList);
vh.Supprimer = (Button) view.findViewById(R.id.buttonDelete);
vh.Modifier = (Button) view.findViewById(R.id.buttonModifier);
view.setTag(vh);
return view;
}
public void bindView(View view, Context Context, Cursor cursor) {
vh.idList.setText(cursor.getString(cursor.getColumnIndex(stock._ID)));
vh.themeList.setText(cursor.getString(cursor.getColumnIndex(stock.THEME)));
vh.questionList.setText(cursor.getString(cursor.getColumnIndex(stock.QUESTION)));
vh.reponseList.setText(cursor.getString(cursor.getColumnIndex(stock.REPONSE)));
vh.difficulteList.setText(cursor.getString(cursor.getColumnIndex(stock.DIFFICULTE)));
vh.Supprimer.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int id = Integer.parseInt(vh.idList.getText().toString());
Toast.makeText(container.getContext(), "bouton delete : "+id, Toast.LENGTH_LONG).show();
//Here everytime I hit the button, the last ID i put on the listView is displayed, no matter what Supprimer button I click
}
});
}
}
public class ViewHolder
{
Button Supprimer, Modifier;
TextView idList, themeList, questionList, reponseList, difficulteList;
}
}
And here is my layout for my TextView :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeList"
android:descendantFocusability="blocksDescendants">
<TextView
android:text="#string/difficult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView8"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp" />
<TextView
android:text="#string/r_ponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView7"
android:layout_marginTop="20dp"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idList"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/themeList"
android:layout_alignStart="#+id/themeList"
tools:ignore="HardcodedText" />
<TextView
android:text="Question :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:layout_marginTop="14dp"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:ignore="HardcodedText" />
<TextView
android:text="#string/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textView4"
android:layout_alignEnd="#+id/textView4" />
<TextView
android:text="Thème :"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_alignRight="#+id/textView6"
android:layout_alignEnd="#+id/textView6"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView6"
android:id="#+id/questionList"
android:layout_toRightOf="#+id/themeList"
android:layout_toEndOf="#+id/themeList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView8"
android:layout_alignLeft="#+id/questionList"
android:layout_alignStart="#+id/questionList"
android:id="#+id/reponseList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView8"
android:layout_alignRight="#+id/reponseList"
android:layout_alignEnd="#+id/reponseList"
android:id="#+id/difficultList"
android:layout_toEndOf="#+id/reponseList"
android:layout_toRightOf="#+id/reponseList" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/themeList"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
tools:ignore="HardcodedText"
android:layout_below="#+id/idList"
android:layout_toRightOf="#+id/textView8"
android:layout_toEndOf="#+id/textView8" />
<Button
android:text="#string/supprimerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/buttonDelete"/>
<Button
android:text="#string/modifierB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonModifier"
android:layout_alignTop="#+id/questionList"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/buttonDelete"
android:layout_alignStart="#+id/buttonDelete" />
</RelativeLayout>
Thank you for help !
Probably vh.idList is containing last item view which is provided by last call of newView .
Get clicked row item id using findViewById and v parameter of onClick method:
public void onClick(View v) {
View parentView = (View)v.getParent();
TextView idList = parentView.findViewById(R.id.idList);
int id = Integer.parseInt(idList.getText().toString());
...
}
I am trying to populate a ListView and display it under Fragment. I see that the count is returning correctly (greater than 0) after calling the adapter. Initially I had an issue with getView not being called but it was in a way resolved by changing layout_height to wrap_content as suggested by one of the article. I see that getView is now called while swiping for one Fragment to another. But getView doesn't get called when the Fragment is initially loaded. and also I don't ListView is displayed on screen regardless of any action.
First Fragment:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
//private ListView mListView;
GridView grid;
String[] web = {
"Doctors","Movies","Dentists"} ;
int[] imageId = {
R.mipmap.doctors,
R.mipmap.dentists,
R.mipmap.restaurants
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
CustomGrid adapter = new CustomGrid(view.getContext(), iconSize);
GridView gridview = (GridView) view.findViewById(R.id.grid);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
ListItemMainFragment nextFrag = new ListItemMainFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), nextFrag);
ft.setTransition(FragmentTransaction.TRANSIT_NONE);// it will anim while calling fragment.
ft.addToBackStack(null); // it will manage back stack of fragments.
ft.commit();
}
});
return view;
}
}
Next Fragment:
public class ListItemMainFragment extends Fragment implements Item.DataListener {
private ListView mListView;
private ItemAdapter adapter;
public ListItemMainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item_main, container, false);
mListView = (ListView) view.findViewById(R.id.item_list_view);
mListView.setAdapter(adapter);
//final String DBReference = "all/" + position + "/" + web[+position];
final String DBReference = "all/0/Doctors";
Item.getRecipesFromDB(ListItemMainFragment.this, DBReference);
return view;
}
#Override
public void newDataReceived(ArrayList<Item> itemList) {
adapter = new ItemAdapter(getActivity(), itemList);
mListView.setAdapter(adapter);
Log.d("records ", String.valueOf((adapter.getCount())));
}
}
Adapter:
public class ItemAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<Item> mDataSource;
public ItemAdapter(Context context, ArrayList<Item> items) {
Log.d("MyApp", "I am here1000A");
mContext = context;
mDataSource = items;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//1
#Override
public int getCount() {
return mDataSource.size();
}
//2
#Override
public Object getItem(int position) {
return mDataSource.get(position);
}
//3
#Override
public long getItemId(int position) {
return position;
}
//4
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.list_item_detail, parent, false);
Log.d("MyApp", "I am here1000");
TextView titlePrefixTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_title_prefix);
TextView titleTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_title);
TextView specialityTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_speciality);
TextView addressTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_address);
TextView distanceTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_distance);
TextView timingsTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_timings);
TextView contactTextView =
(TextView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_contact);
// Get thumbnail element
//ImageView thumbnailImageView =
// (ImageView) rowView.findViewById(com.ferainc.kudla.R.id.item_list_thumbnail);
Item item = (Item) getItem(position);
titlePrefixTextView.setText(String.valueOf(position+1) + ".");
titleTextView.setText(item.title);
specialityTextView.setText(item.speciality);
addressTextView.setText(item.address);
distanceTextView.setText(item.distance);
timingsTextView.setText(item.timings);
contactTextView.setText(item.contact);
return rowView;
}
}
Item.java:
public class Item {
public String title;
public String speciality;
public String timings;
public String address;
public String contact;
public String distance;
public interface DataListener {
void newDataReceived(ArrayList<Item> itemList);
}
public Item () {
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
}
public String getTitle() {
return title;
}
public String getSpeciality() { return speciality; }
public String getTimings() {
return timings;
}
public String getAddress() { return address; }
public String getContact() { return contact; }
public String getDistance() { return distance; }
public static void getRecipesFromDB(final DataListener dataListener, String DBReference){
final ArrayList<Item> itemList = new ArrayList<>();
final DatabaseReference mDatabase;
final Item item = new Item();
mDatabase = FirebaseDatabase.getInstance().getReference(DBReference);
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " recipes");
Log.d("MyApp", "I am here100");
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Item post = postSnapshot.getValue(Item.class);
Item item = new Item();
item.title = post.getTitle();
item.speciality = post.getSpeciality();
item.timings = post.getTimings();
item.address = post.getAddress();
item.contact = post.getContact();
item.distance = post.getDistance();
itemList.add(item);
}
// Transaction complete, sending to listener
dataListener.newDataReceived(itemList);
}
#Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getMessage());
}
});
}
}
ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.ferainc.kudla.ListItemMainFragment">
<ListView
android:id="#+id/item_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
ListView Item detail:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#android:color/holo_green_light"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:id="#+id/list_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#android:color/holo_green_light">
<TextView
android:id="#+id/item_list_title_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"
tools:text="Title" />
<TextView
android:id="#+id/item_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_toEndOf="#id/item_list_title_prefix"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"
tools:text="Title" />
<TextView
android:id="#+id/item_list_speciality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_title"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:textColor="#000000" />
<View
android:id="#+id/center_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/item_list_speciality"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/item_list_contact_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/center_divider"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Ph:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/center_divider"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_contact_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_address_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_contact"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Address:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_contact"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_address_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_distance_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_address"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Distance:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_address"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_distance_prefix"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_timings_prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_distance"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:text="Timings:"
android:textColor="#fff092b0" />
<TextView
android:id="#+id/item_list_timings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_list_distance"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#id/item_list_timings_prefix"
android:textColor="#fff092b0" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
I am not sure what you wanted by Item.getRecipesFromDB(ListItemMainFragment.this, DBReference);
as there is no code regarding "Items" it your Fragment.
But it think it doesn't populate your ListView.
So after ListView initialisation you have to create an adapter (it is missing in your onCreateView). And then set it onto ListView.
You also may try to call newDataReceived() from onCreteView.
But I suggest you to create the Adapter in onCreteView and then in newDataReceived() you can just update data and call notifyDataSetChanged() instead of creating new one each time
I have a custom ListView with a button and when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it.
I have list_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_width="fill_parent"
android:id="#+id/TableRow_list_item"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:id="#+id/imageView_logo_utente"
android:src="#drawable/icon_user_green"
android:layout_column="1"
android:layout_weight="1"/>
<TextView
android:textColor="#000000"
android:id="#+id/nomec"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Nome Completo"
android:layout_weight="2"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<TextView
android:textColor="#000000"
android:id="#+id/tiposogget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Tipo Soggetto Etichetta"
android:layout_weight="2"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<TextView
android:textColor="#000000"
android:id="#+id/cf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Codice Fiscale"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"
android:layout_weight="2" />
<TextView
android:textColor="#000000"
android:id="#+id/idsogg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="ID Soggetto"
android:layout_weight="1"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<Button
android:layout_width="0dp"
android:layout_height="90dp"
android:id="#+id/button_scan"
android:layout_column="1"
android:layout_weight="0.63"
android:height="140dp"
android:background="#drawable/custom_button_scanner"
android:onClick="myClickHandler"
android:focusable="false"/>
</TableRow>
The button have "android:onClick="myClickHandler""..
In my activity i have this:
public class ActivityListview extends AppCompatActivity {
//************VARIABILI GLOBALI*************
ListView lv;
static final String KEY_CF = "CodiceFiscale";
static final String KEY_ID = "IDSoggetto";
static final String KEY_NOMEC = "NomeCompleto";
static final String KEY_TIPOSOGGET = "TipoSoggettoEtichetta";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_scanner);
lv = (ListView) findViewById(R.id.listView_resultXML);
Example();
}
public void Example() {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[]{KEY_CF, KEY_ID, KEY_NOMEC, KEY_TIPOSOGGET}, new int[]{
R.id.cf, R.id.idsogg, R.id.nomec, R.id.tiposogget});
// selecting single ListView item
ViewGroup headerview = (ViewGroup) getLayoutInflater().inflate(R.layout.header_listview, lv, false);
lv.setAdapter(null);
lv.addHeaderView(headerview);
lv.setAdapter(adapter);
public void myClickHandler(View v)
{
// HERE I WOULD LIKE TO get vaule as method setOnItemClickListener
Toast.makeText(ActivityListviewScanner.this, "value is:", Toast.LENGTH_LONG).show();
}
The method:
// listening to single listitem click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//Metodo per prelevare dati al click sulla casella della ListView
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String string_example = ((TextView) view.findViewById(R.id.cf)).getText().toString();
}
});
}
}
i want do it in onclick button..
how can I do?
Thankyou for all and sorry for my english
You will need to extend from SimpleAdapter so you can save the reference to the textview in your listener, which you would declare inside the getView method of the adapter.
If you need help creating the adapter, take a look at this other question:
Custom Adapter for List View
Create a class which extends BaseAdapter. And in the getview method regiter your button and set onClick function.
public class Holder {
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
Button bt;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.tv1 = (TextView) rowView.findViewById(R.id.nomec);
holder.tv2 = (TextView) rowView.findViewById(R.id.tiposogget);
holder.tv3 = (TextView) rowView.findViewById(R.id.cf);
holder.tv4 = (TextView) rowView.findViewById(R.id.idsogg);
holder.img = (ImageView) rowView.findViewById(R.id.imageView_logo_utente);
holder.bt = (Button) rowView.findViewById(R.id.button_scan);
holder.bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//write your code here
}
});
return rowView;
}
I have a problem. I started a few weeks ago learning Android. I am trying to create an adapter for a list. But the lists doesn't show up AT ALL. Any ideas why? The items are objects type Verb. Here is my code:
///my main activity///
public class Verbs extends Activity {
private ListView listViewVerbs;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verbs);
//instantiate Verb
Verb[] VerbData=new Verb[3];
VerbData[0] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[1] = new Verb("machen","mache","machen","mache","machen","mache","machen");
VerbData[2] = new Verb("machen","mache","machen","mache","machen","mache","machen");
//pass data to adapter
VerbAdapter adapter = new VerbAdapter(this,R.layout.row_verbs,VerbData);
listViewVerbs = (ListView)findViewById(R.id.list);
listViewVerbs.setAdapter(adapter);
listViewVerbs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String wasClicked = view.findViewById(R.id.nameVerb).toString();
Toast.makeText(Verbs.this,
"You clicked: " + wasClicked, Toast.LENGTH_LONG)
.show();
}
});
}
}
//class for an item of the list
public class Verb {
public String verbname;
public String fpersg;
public String fperspl;
public String sperssg;
public String sperspl;
public String tperssg;
public String tperspl;
//constructor
public Verb(String s, String s1, String s2, String s3, String s4, String s5,String s6){
this.verbname = s;
this.fpersg = s1;
this.fperspl = s2;
this.sperssg = s3;
this.sperspl = s4;
this.tperssg = s5;
this.tperspl = s6;
}
//setters
public void setVbName(String vbName){
this.verbname = vbName;
}
public void setFpersg(String fpersg){
this.fpersg = fpersg;
}
public void setFperpl(String fperpl){
this.fperspl = fperpl;
}
public void setSpersg(String spersg){
this.sperssg = spersg;
}
public void setSperspl(String sperpl){
this.sperspl = sperpl;
}
public void setTpersg(String tpersg){
this.tperssg = tpersg;
}
public void setTperpl(String tperpl){
this.tperspl = tperpl;
}
//getters
public String getVerbname(){
return this.verbname;
}
public String getFpersg(){
return this.fpersg;
}
public String getFperpl(){
return this.fperspl;
}
public String getSpersg(){
return this.sperssg;
}
public String getSperpl(){
return this.sperspl;
}
public String getTpersg(){
return this.tperssg;
}
public String getTperspl(){
return this.tperspl;
}
}
//the adapter
public class VerbAdapter extends ArrayAdapter<Verb>{
Context mContext;
int layoutResourceId;
Verb data[] = null;
public VerbAdapter(Context mContext, int layoutResourceId, Verb[] data){
super(mContext, layoutResourceId, data);
this.mContext = mContext;
this.layoutResourceId =layoutResourceId;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId,parent, false);
}
TextView textView0 = (TextView) convertView.findViewById(R.id.nameVerb);
TextView textView1 = (TextView) convertView.findViewById(R.id.firstpersonsg);
TextView textView2 = (TextView) convertView.findViewById(R.id.firstpersonpl);
TextView textView3 = (TextView) convertView.findViewById(R.id.secondpersonsg);
TextView textView4 = (TextView) convertView.findViewById(R.id.secondpersonpl);
TextView textView5 = (TextView) convertView.findViewById(R.id.thirdpersonsg);
TextView textView6 = (TextView) convertView.findViewById(R.id.thirdpersonpl);
Verb verb = data[position];
textView0.setText(verb.verbname);
textView1.setText(verb.fpersg);
textView2.setText(verb.fperspl);
textView3.setText(verb.sperssg);
textView4.setText(verb.sperspl);
textView5.setText(verb.tperssg);
textView6.setText(verb.tperspl);
return convertView;
}
}
------and two XML files named activity_verbs where the list is declared and row_verbs where the layout of each item is declared-----
row_verbs.xml ------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/nameVerb"
android:text="#string/nameVerb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="0dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/firstsg"
android:layout_below="#+id/nameVerb"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#cacaca"
android:textColor="#7663d5" />
<TextView
android:id="#+id/firstpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/firstpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/nameVerb"
android:layout_toRightOf="#+id/firstpersonsg"/>
<TextView
android:id="#+id/secondpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/secondsg"
android:layout_below="#+id/firstpersonsg"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#FFCACACA"
android:textColor="#7663d5" />
<TextView
android:id="#+id/secondpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/secondpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/firstpersonpl"
android:layout_toRightOf="#+id/secondpersonsg"/>
<TextView
android:id="#+id/thirdpersonsg"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:text="#string/thirdsg"
android:layout_below="#+id/secondpersonsg"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:background="#FFCACACA"
android:textColor="#7663d5" />
<TextView
android:id="#+id/thirdpersonpl"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="#string/thirdpl"
android:padding="5dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
android:background="#FFCACACA"
android:textColor="#7663d5"
android:layout_below="#+id/secondpersonpl"
android:layout_toRightOf="#+id/thirdpersonsg"/>
</RelativeLayout>
and the other one activity_verbs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${relativePackage}.${activityClass}"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textSize="20sp"
android:padding="10dp"
android:background="#7663d5"
android:textColor="#f8f8f8"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lessonId"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#292929"
android:textColor="#f0f0f0"
android:padding="10dp"/>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/lessonId"/>
</RelativeLayout>
I copied your code with only one change:
I removed
tools:context="${relativePackage}.${activityClass}"
from activity_verbs and it works like a charm for me:
I uploaded my test-project with your code to DropBox, feel free to download it:
https://www.dropbox.com/s/8fcdab3rmx8zb57/ElaPinkSO.zip?dl=0
P.S. Very well structured question, good job!
For some reason tapping a row in my list view doesn't seem to work even if I have the correct listener code. There are only textviews in the template of the lists. I know there's been discussion about assigning a listener when there is a button in the template of a list view. see here.
Here is my code:
ScheduleActivity:
public class ScheduleActivity extends AppCompatActivity {
private String url;
JSONObject data = null;
Toolbar toolbar;
Intent intent;
String userId;
int eventId;
ListView scheduleListView;
ScheduleAdapter scheduleAdapter;
ArrayList<Schedule> scheduleList = new ArrayList<>();
DBManager dbManager = new DBManager(ScheduleActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_layout);
Log.d("Test", ">>>ScheduleActivity<<<");
Bundle extras = getIntent().getExtras();
userId = extras.getString("userId");
eventId = extras.getInt("eventId");
toolbar = (Toolbar) findViewById(R.id.schedule_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Schedules");
scheduleList = dbManager.getSchedules(eventId);
scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, scheduleList);
scheduleListView = (ListView) findViewById(R.id.scheduleListView);
scheduleListView.setAdapter(scheduleAdapter);
scheduleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", scheduleList.get(position).toString());
intent = new Intent(ScheduleActivity.this, UpdateScheduleActivity.class);
intent.putExtra("userId", userId);
intent.putExtra("eventId", eventId);
startActivity(intent);
}
});
scheduleListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.w("Test", "Long click works");
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.schedule_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.new_schedule) {
intent = new Intent(ScheduleActivity.this, CreateScheduleActivity.class);
intent.putExtra("eventId", eventId);
startActivity(intent);
}else if(id == R.id.rankings){
Log.d("Test", "Rankings Activity clicked!");
}
return super.onOptionsItemSelected(item);
}
ScheduleAdapter:
public class ScheduleAdapter extends ArrayAdapter<Schedule>{
public ScheduleAdapter(Context context, List<Schedule> schedule) {
super(context, R.layout.schedule_list_row, schedule);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater eventInflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = eventInflater.inflate(R.layout.schedule_list_row, parent, false);
}
Schedule singleSchedule = getItem(position);
//TODO implement ViewHolder pattern
TextView club1Code = (TextView) convertView.findViewById(R.id.club1TextView);
TextView club2Code= (TextView) convertView.findViewById(R.id.club2TextView);
TextView club1Score = (TextView) convertView.findViewById(R.id.club1ScoreTextView);
TextView club2Score = (TextView) convertView.findViewById(R.id.club2ScoreTextView);
TextView club1SpiritScore = (TextView) convertView.findViewById(R.id.club1SpiritScoreTextView);
TextView club2SpiritScore = (TextView) convertView.findViewById(R.id.club2SpiritScoreTextView);
TextView time = (TextView) convertView.findViewById(R.id.timeTextView);
TextView day = (TextView) convertView.findViewById(R.id.dayTextView);
club1Code.setText(singleSchedule.getClub1Id());
club2Code.setText(singleSchedule.getClub2Id());
day.setText("Day " + singleSchedule.getDay());
time.setText(singleSchedule.getStartTime() + " - " + singleSchedule.getEndTime());
club1Score.setText(Integer.toString(singleSchedule.getClub1Score()));
club2Score.setText(Integer.toString(singleSchedule.getClub2Score()));
club1SpiritScore.setText(Integer.toString(singleSchedule.getClub1SpiritScore()));
club2SpiritScore.setText(Integer.toString(singleSchedule.getClub2SpiritScore()));
return convertView;
}
}
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kennanseno.ultimate_scoreboard_app.Activity.ScheduleActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/schedule_toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:title="#string/event_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scheduleListView"
android:layout_centerHorizontal="true"
android:layout_below="#+id/schedule_toolbar" />
</RelativeLayout>
Row Template:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_name_text"
android:id="#+id/club1TextView"
android:textAlignment="textStart"
android:layout_above="#+id/club1SpiritScoreTextView"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_name_text"
android:id="#+id/club2TextView"
android:layout_gravity="right"
android:textAlignment="textEnd"
android:layout_below="#+id/dayTextView"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/score_divider"
android:id="#+id/score_divider"
android:textSize="50sp"
android:layout_below="#+id/timeTextView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_score_text"
android:id="#+id/club1ScoreTextView"
android:textSize="50sp"
android:layout_marginEnd="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toStartOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_score_text"
android:id="#+id/club2ScoreTextView"
android:textSize="50sp"
android:layout_marginStart="22dp"
android:layout_below="#+id/timeTextView"
android:layout_toEndOf="#+id/score_divider"
android:textAlignment="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/start_time_text"
android:id="#+id/timeTextView"
android:textAlignment="center"
android:layout_below="#+id/dayTextView"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club1_spirit_score_text"
android:id="#+id/club1SpiritScoreTextView"
android:textAlignment="textStart"
android:textSize="30sp"
android:layout_alignTop="#+id/club2SpiritScoreTextView"
android:layout_alignStart="#+id/club1TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/club2_spirit_score_text"
android:id="#+id/club2SpiritScoreTextView"
android:textAlignment="textEnd"
android:textSize="30sp"
android:layout_below="#+id/club2TextView"
android:layout_alignEnd="#+id/club2TextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/schedule_day_text"
android:id="#+id/dayTextView"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignEnd="#+id/club2ScoreTextView"
android:layout_alignStart="#+id/club1ScoreTextView" />
</RelativeLayout>
In Row Template xml:
android:clickable="true"
causing issue because RelativeLayout is clickable with match_parent height-width.
if you want to get click event for whole row then no need to make parent layout clickable just set setOnItemClickListener to ListView otherwise you need to set onClickListener for RelativeLayout in ScheduleAdapter class.