ListView does not react to selection - android

I have a ListView in an activity which does note react to being selected. This could be a small thing as I am relatively new to Android, but don't seem to be able to figure it out myself.
My onLoad() in my Activity loads like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_list);
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey("categoryId")) categoryId = extras.getLong("categoryId");
}
ListView lv = (ListView) findViewById(R.id.article_list_activity);
iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext());
try {
objects = app.getDataManager().getArticlesForCategory(categoryId, 15);
adapter = new ArticleListAdapter(this, R.layout.article_list_cell, objects, categoryId);
lv.setOnScrollListener(adapter);
lv.setAdapter(adapter);
} catch (Exception e){
Log.e(iDomsAndroidApp.TAG, "Error " + e.getMessage(), e);
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), objects.get(position).getTitle(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), ArticleActivity.class);
intent.putExtra("articleId", objects.get(position).getId());
startActivity(intent);
}
});
}
Then in my Adapter (ArrayAdapter)
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inflater.inflate(R.layout.article_list_cell, viewGroup, false);
}
Article article = getItem(i);
TextView titleText = (TextView)view.findViewById(R.id.article_list_titleText);
try{
TextView siteText = (TextView)view.findViewById(R.id.article_list_siteText);
TextView summaryText = (TextView)view.findViewById(R.id.article_list_summaryText);
RadioButton readBttn = (RadioButton) view.findViewById(R.id.article_list_readButton);
TextView updatedText = (TextView) view.findViewById(R.id.article_list_updatedText);
TextView date = (TextView) view.findViewById(R.id.article_cell_date);
titleText.setText(article.getTitle());
siteText.setText(article.getSite().getName());
summaryText.setText(article.getSummary());
date.setText(DateFormat.getMediumDateFormat(this.getContext()).format(article.getPubDate()));
if(article.getRead()){
readBttn.setChecked(false);
} else {
readBttn.setChecked(true);
}
if(article.getUpdated()){
updatedText.setVisibility(View.VISIBLE);
} else {
updatedText.setVisibility(View.INVISIBLE);
}
} catch (Exception e) {
titleText.setText("Error loading article content!");
}
return view;
}
article_list_cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Site"
android:id="#+id/article_list_siteText" android:paddingLeft="10dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Today 12:34"
android:id="#+id/article_cell_date"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:gravity="center">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/article_list_readButton" android:checked="false"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Updated"
android:id="#+id/article_list_updatedText" android:textColor="#1898ff"
android:layout_gravity="right"
android:visibility="visible"
android:textSize="12dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/list_background_overlay">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:id="#+id/article_list_titleText" android:maxLines="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Summary"
android:id="#+id/article_list_summaryText" android:maxLines="3"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

My guess is RadioButton takes focus when you click list item.
So add
android:descendantFocusability="blocksDescendants"
to root element in article_list_cell.xml
Edit:

Try to add #Override to your onItemClick method
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {}

l1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String name=(String)parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(), name, Toast.LENGTH_LONG).show();
Intent i = new Intent(getBaseContext(),Webview.class);
//i.putExtra("USERNAME", name);
startActivity(i);
}
});

Related

How to add Different OnClickListeners on views of ListView item to perform different actions

Are there any possibilities to check what view inside ListView Item was clicked?
In other words, when you click on different Views inside ListView item app should perform a different action.
In details, I have a simple Book.java class that contains some book description.
Then I create ListView<Book> using BooksAdapter.class:
public class BooksAdapter extends ArrayAdapter<Book> {
public BooksAdapter (Context context, List<Book> books) {
super(context,0,books);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
}
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover") {
coverView.setImageResource(R.drawable.no_book_cover);
} else {
Picasso.get().load(currentBook.getImage()).into(coverView);
}
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
}
private String formatAuthor (String name,String date ) {
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
}
}
books_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="#color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="#drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#android:color/darker_gray"/>
</LinearLayout>
In MainActivity.java create mAdapter and override setOnItemClickListener:
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
}
});
And here are some general question can we set onClickListeners on different Views inside Item (#+id/autor_text and #+id/title_text for example) and perform different actions in these cases?
I'm reading about this several places but doesn't find any helpful things.Thanks for any help.
Inside your getView() method set onClickListener to all your different Views and perform respective action.
By implementing this code:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
}
}
};
Inside your getView
authorTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});
titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});

SetOnItemClickListener not responding in custom ListView of android

I am new to android,I am currently making a listview with custom list items,I want to do an action on Listview's item click event,I have searched so many similar threads with no luck,Can anyone help me to fix this.my code is as below,
<?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:background="#fff">
<TextView
android:id="#+id/tv_hdr"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:gravity="center"
android:text="SELECT YOUR CITY"
android:textColor="#color/white"
android:textSize="22dp" />
<EditText
android:id="#+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_hdr"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/et_selector"
android:drawableLeft="#drawable/search"
android:hint="City Or Postal Code"
android:padding="10dp"
android:textSize="16dp" />
<TextView
android:id="#+id/tv_loc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/et_search"
android:background="#color/grey_light"
android:drawableLeft="#drawable/ic_loc"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" My Location"
android:textColor="#color/black"
android:textSize="16dp"
android:textStyle="bold" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scr_location"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tv_loc"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_populcar_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" Popular Cities"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_populcar_city" />
<TextView
android:id="#+id/tv_states"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_city"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="States"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_states" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
adapter
public class CityAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<City> city;
Integer selected_position = -1;
public CityAdapter(Context c, ArrayList<City> city) {
mContext = c;
this.city = city;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return city.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.raw_city, parent, false);
} else {
v = (View) convertView;
}
TextView tv_city = (TextView) v.findViewById(R.id.tv_city);
tv_city.setText(city.get(position).getCity());
return v;
}
}
activity
lv_city.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID, cityList.get( position ).getCity_id() );
finish();//finishing activity
}
} );
You are wrong class in listview item click
lv_city.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID,
cityList.get( position ).getCity_id() );
finish();//finishing activity
}
});
#quick learner's was correct, But i put some idea for your ref..
If you use custom adapter class, In my suggestion use setOnClickListener in custom adapter class this is better way to do this type of case!!
For example,
mViewHolder.MyUI.setOnClickListener

ListView Toast Message

I have this code here. I am trying to display a toast message when a row in my list view is clicked but nothing shows up. Here is my code.
public class MainActivity extends AppCompatActivity {
ListView list;
String[] movieTitles;
int[] images = {R.drawable.batman_vs_superman, R.drawable.captain_america, R.drawable.deadpool,
R.drawable.jungle_book, R.drawable.xmen,R.drawable.zootopia, R.drawable.hail,R.drawable.allegent,
R.drawable.jason, R.drawable.lane};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res= getResources();
movieTitles = res.getStringArray(R.array.titles);
list = (ListView)findViewById(R.id.listView);
MoviesAdapter adapter = new MoviesAdapter(this,movieTitles, images);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();
}
});
}
}
EDIT I have two xml files here is the code:
"content_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.emmancipatemusemwa.task2.MainActivity"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clickable="true" />
</RelativeLayout>
And Here I have single_row.xml with the single row design.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:src="#drawable/captain_america"
android:paddingTop="10dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView" />
<RatingBar
android:layout_width="245dp"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="more info"
android:id="#+id/textView2"
android:layout_below="#+id/ratingBar"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textStyle="italic" />
</RelativeLayout>
Try this!!!!
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ position, Toast.LENGTH_SHORT).show();
}
});
DO LIKE THIS IN MAINACTIVITY
// Import
import android.widget.AdapterView;
.........
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected "+ movieTitles[position], Toast.LENGTH_SHORT).show();
}
});
OR IN UR CUSTOM ADAPTER LISTVIEW ALTER/ADD THIS
#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.program_list, null);
.....................
...................
...................
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You selected "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
NOTE: Do Not have two setOnClickListener, Only one is used,
According to ur problem, I think ur using two setOnClickListener
functions so Toast Not working.
U should use only one in MainActivity or ur custom class (MovieAdapter).

Tapping rows in Listview not working even w/ SetOnItemClicklistener in Android

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.

Get Item from a ListView

My app is displaying 3 String with 3 Text-view in a list-view.
My Problem is that I create on layout for my list-view and i am not able to get the item from the first text view in the onListItemClick method.
How can I achieve achievement this?
Layout of the ListView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outputlayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#color/ligthgrey">
<TextView
android:id="#+id/txOutputDeparture"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Abfahrt "
android:textColor="#color/black"
android:layout_alignParentLeft="true"
android:maxLength="#android:integer/config_shortAnimTime"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/txOutputDuration"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/txOutputDeparture"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dauer"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/txOutputTransition"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/txOutputDuration"
android:text="Umstieg"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="5dp" />
</RelativeLayout>
Method where i create and fill the listView and its and ListActivity
public void getRoute() {
mdbH = new DatabaseHelperActivity(this);
cursor = mdbH.fetchallRoutes(intent.getStringExtra("StartHaltestelle"),intent.getStringExtra("ZielHaltestelle"), intent.getStringExtra("Zeit"));
ArrayList<DefineRouteActivity> route = new ArrayList<DefineRouteActivity>();
while(cursor.moveToNext()) {
route.add(new DefineRouteActivity(cursor.getString(0),cursor.getString(2),cursor.getString(4)));
}
ArrayAdapter<DefineRouteActivity> adapter = new RouteAdapterActivity(this, route);
setListAdapter(adapter);
}
AdapterActivity:
Activity context;
ArrayList<DefineRouteActivity> arraylist;
public RouteAdapterActivity(Activity context, ArrayList list) {
super(context,R.layout.outputlayout,list);
this.context = context;
arraylist = list;
}
#Override
public int getCount() {
return arraylist.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View contentview, ViewGroup viewGroup) {
DefineRouteActivity routeItems = arraylist.get(position);
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.outputlayout,null);
TextView tvDeparture = (TextView)view.findViewById(R.id.txOutputDeparture);
tvDeparture.setText(routeItems.getAbfahrtszeit());
TextView tvDuration = (TextView)view.findViewById(R.id.txOutputDuration);
tvDuration.setText(routeItems.getDauer());
TextView tvTransition = (TextView)view.findViewById(R.id.txOutputTransition);
tvTransition.setText(routeItems.getUmstieg());
return view;
}
Here is my onClick Method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
((View)v).
Intent detail = new Intent(getApplicationContext(),DetailOutputActivity.class);
detail.putExtra("StartStop",l.getItemAtPosition(0).toString());
detail.putExtra("EndStop","ich");
detail.putExtra("Time","du");
detail.putExtra("Route","er");
startActivity(detail);
}
Here is my Layout containing the listView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
You need another xml containing the listView.
For example: sample.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In your mainActivity.java:
setContentView(R.layout.sample.xml);
lv = (ListView)findViewById(R.id.lv1);
lv.setOnItemClickListener(this);
Your listActivity must implement OnItemClickListener.
Then implements onItemClick method. Example:
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// TODO Auto-generated method stub
int id_object = ((object)a.getAdapter().getItem(position)).getIdInstalacion();
Intent i = new Intent(this, Other.class);
i.putExtra("id_", id_object);
startActivity(i);
}

Categories

Resources