ListView With 2 Button , one button need for FragmentTransaction - java

I'm new in android programming, my problem is this.
I have a ListView named(items), and 4 different fragments(tableFragment,bedFragment,DeskFragment,ChairFragment) where I implement this list. In the list I have 2 buttons with different roles. I want one button (favorites or ar doesn't matter) to make a transaction from one fragment where I have the listview to a new fragment detailsFragment and put info(like image, and textview from the list) in the details_fragment.xml . I appreciate any effort, thanks!
items.java
public class items{
// Name of the object
private String lName;
// Costs
private String lPrice;
// Details
private String lDetails;
// Image resource id
private int lImageId;
// Buttons resource id
private int lButtonF;
private int lButtonAr;
// Constructor
public items(String ObjectName,String ObjectPrice,int ImageResourceId,int ButtonFavorites,int ButtonAr,String DetailsItem){
lName=ObjectName;
lPrice=ObjectPrice;
lImageId=ImageResourceId;
lButtonAr=ButtonAr;
lButtonF=ButtonFavorites;
lDetails=DetailsItem;
}
// Getters
public String getlName() { return lName; }
public String getlPrice(){ return lPrice; }
public int getlImageId(){ return lImageId; }
public int getlButtonF() { return lButtonF; }
public int getlButtonAr() { return lButtonAr; }
public String getlDetails() { return lDetails; }
}
itemsAdapter.java
public class itemsAdapter extends ArrayAdapter<items>{
private static final String LOG_TAG = itemsAdapter.class.getSimpleName();
public itemsAdapter(Activity context, ArrayList<items> item){
super(context,0,item);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
items currentItems=getItem(position);
TextView nameTextView=(TextView)listItemView.findViewById(R.id.name_item);
nameTextView.setText(currentItems.getlName());
TextView priceTextView=(TextView)listItemView.findViewById(R.id.price_item);
priceTextView.setText(currentItems.getlPrice());
ImageView iconImageView=(ImageView)listItemView.findViewById(R.id.icon_item);
iconImageView.setImageResource(currentItems.getlImageId());
TextView detailsView=(TextView)listItemView.findViewById(R.id.details_item);
detailsView.setText(currentItems.getlDetails());
// Favorites and Ar buttons
Button buttonFavoritesItem=(Button)listItemView.findViewById(R.id.favorites_item);
buttonFavoritesItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button buttonArItem=(Button)listItemView.findViewById(R.id.ar_item);
buttonArItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return listItemView;
}
}
tableFragment.java
public class tableFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tables_fragment, container, false);
ArrayList<items> tables=new ArrayList<items>();
tables.add(new items("Table1","23$",R.drawable.table1,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table2","43$",R.drawable.table2,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table3","54$",R.drawable.table3,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table4","34$",R.drawable.table4,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table5","65$",R.drawable.table5,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
itemsAdapter itAdapter=new itemsAdapter(this.getActivity(),tables);
ListView listView=(ListView)view.findViewById(R.id.listview_tables);
listView.setAdapter(itAdapter);
return view;
}
}
detailsFragment.java
public class detailsFragment extends Fragment {
public detailsFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_fragment, container, false);
return view;
}
}
details_fragment.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:id="#+id/details_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/name_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
android:textStyle="bold" />
<TextView
android:id="#+id/details_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/ar_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAMERA AR" />
<Button
android:id="#+id/favorites_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="star" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

If you want to move data(image and textview data) between fragments, everything has to go through the base activity of both the fragments. Transferring data between 2 fragments directly is not allowed, i think(do correct me if wrong).

Related

Android - ArrayAdapter toggle ImageButton src between Play and Pause in ListView

I am still learning android. Here I need help to toggle between Play and Pause source in ImageButton in the ListView.
There should be only one song in Play state at a time. So if clicked other should stop.
SongAdapter.java
public class SongAdapter extends ArrayAdapter<Song> {
public SongAdapter(#NonNull Context context, int resource, #NonNull List<Song> objects) {
super(context, resource, objects);
}
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View playlistItemView = convertView;
if (playlistItemView == null) {
playlistItemView = LayoutInflater.from(getContext()).inflate(R.layout.playlist_item, parent, false);
}
Song currentSong = getItem(position);
// get list item elements
ImageView albumCoverThumbnail = playlistItemView.findViewById(R.id.playlist_album_thumbnail);
TextView songTitle = playlistItemView.findViewById(R.id.playlist_song_title);
TextView songAlbumTitle = playlistItemView.findViewById(R.id.playlist_song_album_title);
TextView songArtist = playlistItemView.findViewById(R.id.playlist_song_artist);
final ImageButton songPlayButton = playlistItemView.findViewById(R.id.playlist_play_button);
// set data to the list item
assert currentSong != null;
albumCoverThumbnail.setImageResource(currentSong.getSongAlbumCoverId());
songTitle.setText(currentSong.getSongTitle());
songAlbumTitle.setText(currentSong.getSongAlbumTitle());
songArtist.setText(currentSong.getSongSingers());
// set song button action
songPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
Toast.makeText(getContext(), "Button clicked for item " + position, Toast.LENGTH_LONG).show();
}
});
return playlistItemView;
}
}
Song.java
public class Song {
private String songAlbumTitle;
private String songTitle;
private String songSingers;
private int songAlbumCoverId;
public Song(String albumTitle, String title, String singers, int albumCoverId) {
songAlbumTitle = albumTitle;
songTitle = title;
songSingers = singers;
songAlbumCoverId = albumCoverId;
}
public String getSongAlbumTitle() {
return songAlbumTitle;
}
public String getSongTitle() {
return songTitle;
}
public String getSongSingers() {
return songSingers;
}
public int getSongAlbumCoverId() {
return songAlbumCoverId;
}
}
Activity.java
public class DreamVoyage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dream_voyage);
// get view ids
ImageView albumCoverImage = findViewById(R.id.album_cover);
// get intent extras
Bundle bundle = getIntent().getExtras();
// check if bundle in not null and containing value
if (bundle != null) {
String albumTitle = bundle.getString("album_one_title");
String albumBand = bundle.getString("album_one_band");
int albumCover = bundle.getInt("album_one_cover");
albumCoverImage.setImageResource(albumCover);
TextView albumTitleText = findViewById(R.id.album_title);
TextView albumBandText = findViewById(R.id.album_band);
albumTitleText.setText(albumTitle);
albumBandText.setText(albumBand);
ArrayList<Song> songs = new ArrayList<Song>();
songs.add(new Song(albumTitle, "I do it for you", "Bryn Adams", albumCover));
songs.add(new Song(albumTitle, "Here I am", "Bryn Adams", albumCover));
SongAdapter songAdapter = new SongAdapter(this, 0, songs);
ListView listView = findViewById(R.id.playlist_view);
listView.setAdapter(songAdapter);
}
}
}
playlist_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="#drawable/border_bottom"
android:orientation="horizontal">
<ImageView
android:id="#+id/playlist_album_thumbnail"
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/album_three"
android:scaleType="centerCrop"
android:contentDescription="#string/album_thumbnail_desc"/>
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/playlist_song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mon voyage de rĂªve"
android:layout_marginLeft="8dp"
android:textColor="#000000"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:id="#+id/playlist_song_album_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dream Voyage"
android:textSize="10sp"
android:textColor="#666666"
app:layout_constraintTop_toBottomOf="#+id/playlist_song_title"
app:layout_constraintStart_toStartOf="#id/playlist_song_title"/>
<TextView
android:id="#+id/playlist_song_credit_separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - "
android:textSize="10sp"
android:textColor="#666666"
app:layout_constraintTop_toBottomOf="#+id/playlist_song_title"
app:layout_constraintStart_toEndOf="#id/playlist_song_album_title"/>
<TextView
android:id="#+id/playlist_song_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" - John Doen, Jane Doe"
android:textSize="10sp"
android:textColor="#666666"
app:layout_constraintTop_toBottomOf="#+id/playlist_song_title"
app:layout_constraintStart_toEndOf="#id/playlist_song_credit_separator"/>
</android.support.constraint.ConstraintLayout>
<ImageButton
android:id="#+id/playlist_play_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:padding="0dp"
android:layout_gravity="center_vertical"
android:background="#00FFFFFF"
android:src="#drawable/ic_play_arrow_black_24dp"
android:scaleType="centerCrop"/>
</LinearLayout>
Use a variable to store current playing items index
SongAdapter {
int playingIndex = -1 //-1 means no song is playing
.
.
.
}
Set play/pause drawable based on playingIndex and set the playingIndex in the songPlayButton.setOnClickListener
public View getView(final int position, ...) {
if(playingIndex == position)
songPlayButton.setImageResource(R.drawable.ic_play_black_24dp);
else
songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
songPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(position == playIndex)
playIndex = -1;
else
playIndex = position;
notifyDataSetChanged();
}
});
}
It should get the job done. But it forces a redraw of all rows. In RecyclerView there is the notifyItemChanged method which can force redraw on single item.
You may try using the method here to update single row https://stackoverflow.com/a/3727813/4907678
My suggestion is migrating to RecyclerView and using notifyItemChanged.
cheers!

Android - Selecting a Radio Button in a group causes another Radio Button in another group to be selected

I am using a fragment layout to display a row in a ListView and I'm using a ListAdapter to populate the ListView. Each row contains a TextView and a RadioGroup, each radio group contains 2 RadioButtons for yes and no.
The problem is that when I run the application, when I select a RadioButton (like yes) on the first group it causes another RadioButton (also yes) to be selected in another group in another row of the ListView.
Here is the layout code for the row fragment:
<?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"
android:id="#+id/questionTextView">
<TextView
android:text="Dis one na question wey go need yes or no answer?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/questionView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="20sp" />
<RadioGroup
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="#+id/customRadioGroup"
android:layout_marginTop="16dp"
android:layout_below="#+id/questionView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:text="Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/yesRadioBtn"
android:layout_alignBaseline="#+id/noRadioBtn"
android:layout_alignBottom="#+id/noRadioBtn"
android:layout_toLeftOf="#+id/noRadioBtn"
android:layout_toStartOf="#+id/noRadioBtn"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
<RadioButton
android:text="No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/noRadioBtn"
android:layout_below="#+id/questionsTitle"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" />
</RadioGroup>
And my ListAdapter class:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private String[] answers;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
this.answers = new String[list.size()];
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
//return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_row, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.questionView);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.customRadioGroup);
//radioGroup.setId(position);
View vYes = radioGroup.getChildAt(0);
View vNo = radioGroup.getChildAt(1);
RadioButton yesRadioBtn = (RadioButton)vYes;//(RadioButton)view.findViewById(R.id.yesRadioBtn);
RadioButton noRadioBtn = (RadioButton)vNo;//(RadioButton)view.findViewById(R.id.noRadioBtn);
yesRadioBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
answers[position] = "yes";
notifyDataSetChanged();
}
});
noRadioBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
answers[position] = "no";
notifyDataSetChanged();
}
});
return view;
}
public String[] getAnswers()
{
return this.answers;
}
}
You need to reset the RadioGroup if you are reusing a View (convertView != null). The following change will reset the RadioGroup to "no selection", but you will need to set it to the value that is reflected in your internal data structure answers.
RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.customRadioGroup);
if (convertView != null) {
radioGroup.clearCheck();
}

Custom GridView Adapter not displaying

I'm creating a list of "channels" on an app that users can select. I want them in a grid view, (kind of like clickable tiles). There are supposed to be two separate Grids on this Fragment - default channels (channelListSupplied) and ones that the user will have to request subscriptions to (channelListEarned).
I used a custom adapter that I got from another answer here on SO, but I can't get it to work because it's in a Fragment instead of in the Activity, and I'm sure there's some reference I'm not passing correctly.
Below is a list of the relevant pieces of Java and XML...
FragmentChannels.java: (fragment to MainActivity.java)
public class FragmentChannels extends Fragment implements FragmentManager.OnBackStackChangedListener {
ViewGroup container;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
final JSONObject channelList = data.getJSONObject("channels");
final JSONArray channelListSupplied = channelList.getJSONArray("supplied");
final JSONArray channelListEarned = channelList.getJSONArray("earned");
GridView channelViewSupplied = (GridView) view.findViewById(R.id.channel_grid_supplied);
GridView channelViewEarned = (GridView) view.findViewById(R.id.channel_grid_earned);
if (Session.getSessionVar("canHasSpecial").equals("1")) {
FragmentChannels.this.createGridView(channelViewEarned, channelListEarned, FragmentChannels.this.container);
}
FragmentChannels.this.createGridView(channelViewSupplied, channelListSupplied, FragmentChannels.this.container);
...
return view;
}
public void createGridView(final GridView gridView, JSONArray list, final ViewGroup container) throws JSONException {
final String[] gridList = new String[list.length()];
final Activity activity = getActivity();
for (int i = 0; i < list.length(); i++) {
JSONObject channelData = (JSONObject) list.get(i);
gridList[i] = channelData.getString("source_name");
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
gridView.setAdapter(new GridViewAdapter(activity, gridList, container));
}
});
}
public boolean createChannelMenu(Menu menu) {
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_channels, menu);
super.onCreateOptionsMenu(menu, inflater);
return true;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final Activity activity = getActivity();
}
#Override
public void onBackStackChanged() {
}
}
GridViewAdapter.java:
public class GridViewAdapter extends BaseAdapter {
private Context context;
private final String[] textViewValues;
private ViewGroup container;
public GridViewAdapter(Context context, String[] textViewValues, ViewGroup container) {
this.context = context;
this.textViewValues = textViewValues;
this.container = container;
}
#Override
public int getCount() {
return this.textViewValues.length;
}
#Override
public Object getItem(int position) {
return textViewValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("CDFF", position+": "+this.textViewValues[position]);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
TextView titleView = (TextView) gridView.findViewById(R.id.grid_item_title);
titleView.setText(textViewValues[position]);
}
else {
gridView = convertView;
}
return gridView;
}
}
fragment_channels.xml:
<FrameLayout 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:name="FragmentChannels"
android:id="#+id/fragment_channel_container"
tools:context="xx.xxx.xxxx.FragmentChannels"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="My Channels"
android:background="#color/peach"
android:textSize="20sp"
android:textColor="#color/midnightBlue"
android:gravity="center"
android:textAppearance="#style/TextAppearance.FontPath"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_earned"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channel_grid_supplied"
android:layout_margin="5dp"
android:columnWidth="180dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</FrameLayout>
grid_item_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#color/white"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="150dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>
As of now, nothing displays on the Fragment except the "My Channels" TextView.
I greatly appreciate any help!
Fixed.
I changed the line:
gridView = inflater.inflate(R.layout.grid_item_layout, this.container);
To:
gridView = inflater.inflate(R.layout.grid_item_layout, null);
in the GridViewAdapter.java class.

Uncheck CheckTextView from ListView so there are only two checked boxes at any time

I have a list view from which the user must choose two teams by checking the boxes and then validate by pressing the OK button. The problem is I want to make it so that there can only be two checked boxes at any time
Example :
if the user picks team 1 and 2 then the boxes for 1 and 2 should be checked but if the user then picks team 3, 1 should un-check automatically.
I've already managed to isolate and store the position of the box that needs to be unchecked but I don't know what to do with it.
Thanks!!
heres the listView
<Button
android:id="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
/>
<ListView
android:id="#+id/listEquipe"
android:layout_below="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and the following layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txtIdEquipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<CheckedTextView
android:id="#+id/txtNomEquipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"
android:textSize="12pt"
android:textColor="#000000"
/>
</LinearLayout>
This is one way to achieve it (I used stupid data in the adapter):
public class MainActivity extends AppCompatActivity {
private List<Integer> selectedItems = new ArrayList<>();
private String[] teams = new String[]{ "Team A", "Team B", "Team C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.listEquipe);
final BaseAdapter adapter = new BaseAdapter() {
#Override
public int getCount() {
return teams.length;
}
#Override
public Object getItem(int position) {
return teams[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.item, null);
((TextView) view.findViewById(R.id.txtIdEquipe)).setText((String) getItem(position));
CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.txtNomEquipe);
ctv.setChecked(selectedItems.contains(position));
return view;
}
};
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(selectedItems.contains(position)) {
selectedItems.remove(position);
}
else {
selectedItems.add(position);
if(selectedItems.size() > 2) {
selectedItems.remove(0);
}
}
adapter.notifyDataSetChanged();
}
});
}
}

String Array data not showing in TextView

I'm in the process of learning android app development however I have come to an impasse. My RecyclerView is picking up how many items are in my String array however it is not showing the actual data in text1, any help would be much appreciated.
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
static String[] fakeData = new String[] {
"One","Two","Three","Four","Five","Ah..Ah..Ah"
};
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.titleView.setText(fakeData[i]);
}
#Override
public int getItemCount() {
return fakeData.length;
}
This is the corresponding XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:event_view="http://schemas.android.com/apk/res-auto"
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/event_view"
android:layout_marginTop="#dimen/task_card_half_spacing"
android:layout_marginBottom="#dimen/task_card_half_spacing"
android:layout_marginLeft="#dimen/gutter"
android:layout_marginRight="#dimen/gutter"
android:layout_gravity="center"
android:elevation="#dimen/task_card_elevation"
android:foreground="?android:attr/selectableItemBackground"
event_view:cardCornerRadius="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium.Inverse"
android:id="#+id/text1"
android:maxLines="1"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignTop="#+id/image"
android:layout_toRightOf="#+id/image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#android:style/TextAppearance.Inverse"
android:id="#+id/text2"
android:maxLines="3"
android:ellipsize="end"
android:padding="10dp"
android:layout_alignStart="#+id/text1"
android:layout_below="#+id/text1"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
and this is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
Hm... everything looks ok, can you try setting this to it:
android:textColor="#000000"

Categories

Resources