I have a ListView with a gridview for each item:
So when I press the add button I need to add a new item to the gridView(scenesGridViewAdapter) adapter, but when I press the add button always is added to the last gridview.
public class ScenesAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<SceneObject> sceneObjects;
private ArrayList<ScenesGridViewAdapter> scenesGridViewAdapters;
private ScenesGridViewAdapter scenesGridViewAdapter;
public ScenesAdapter(Activity activity, ArrayList<SceneObject> sceneObjects) {
this.activity = activity;
this.sceneObjects = sceneObjects;
this.scenesGridViewAdapters = new ArrayList<>();
}
#Override
public int getCount() {
return this.sceneObjects.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_scenes_item, null);
viewHolder = new ViewHolder();
viewHolder.tvStepIndex = (TextView) convertView.findViewById(R.id.tv_scene_index);
viewHolder.bmb1 = (BoomMenuButton) convertView.findViewById(R.id.bmb_open_item_menu);
viewHolder.scenesGridView = (ScenesGridView)convertView.findViewById(R.id.gv_scene_items) ;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
int index = position + 1;
viewHolder.tvStepIndex.setText("" + index);
scenesGridViewAdapter = new
ScenesGridViewAdapter(this.activity, sceneObjects.get(position).getScenes());
viewHolder.scenesGridView.setAdapter(scenesGridViewAdapter);
viewHolder.bmb1.clearBuilders();
viewHolder.bmb1.addBuilder(BuilderManager.getTextOutsideCircleButtonBuilder("Añadir acción"));
viewHolder.bmb1.addBuilder(BuilderManager.getTextOutsideCircleButtonBuilder("Eliminar paso"));
viewHolder.bmb1.setOnBoomListener(new OnBoomListener() {
#Override
public void onClicked(int index, BoomButton boomButton) {
switch (index){
case 0:
scenesGridViewAdapter.addNew("hello");
notifyDataSetChanged();
break;
case 1:
sceneObjects.remove(position);
notifyDataSetChanged();
break;
}
}
#Override
public void onBackgroundClick() {
}
#Override
public void onBoomWillHide() {
}
#Override
public void onBoomDidHide() {
}
#Override
public void onBoomWillShow() {
}
#Override
public void onBoomDidShow() {
}
});
return convertView;
}
class ViewHolder {
TextView tvStepIndex;
ScenesGridView scenesGridView;
BoomMenuButton bmb1;
}
public void addNewScene(SceneObject sceneObject){
sceneObjects.add(sceneObject);
this.notifyDataSetChanged();
}
}
Can you please help me to add and item to the gridview position?
Thank you.
Related
I'm a noob in android studios..and currently in learning stage.. i copied the code from #Nirav-Kalola in this community. I need help in passing a boolean value into the server whenever left or right is swiped in android swipe cards.
Below is the code:
public class MainActivity extends AppCompatActivity implements FlingCardListener.ActionDownInterface{
public static MyAppAdapter myAppAdapter;
public static ViewHolder viewHolder;
private ArrayList<Data> al;
private SwipeFlingAdapterView flingContainer;
public static void removeBackground() {
viewHolder.background.setVisibility(View.GONE);
myAppAdapter.notifyDataSetChanged();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add(new Data("http://www.drodd.com/images15/1-7.jpg", "Hello Anuj"));
al.add(new Data("http://www.drodd.com/images15/2-23.jpg", "Welcome to the final problem"));
al.add(new Data("http://www.drodd.com/images15/3-12.jpg","\t here goes the name of the person "));
al.add(new Data("http://www.drodd.com/images15/4-7.jpg", " buhahahahahahahaha"));
al.add(new Data("http://www.drodd.com/images15/5-18.jpg", "miss me?"));
myAppAdapter = new MyAppAdapter(al, MainActivity.this);
flingContainer.setAdapter(myAppAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
}
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
myAppAdapter.notifyDataSetChanged();
}
});
}
#Override
public void onActionDownPerform() {
Log.e("action", "bingo");
}
public static class ViewHolder {
public static FrameLayout background;
public TextView DataText;
public ImageView cardImage;
}
public class MyAppAdapter extends BaseAdapter {
public List<Data> parkingList;
public Context context;
private MyAppAdapter(List<Data> apps, Context context) {
this.parkingList = apps;
this.context = context;
}
#Override
public int getCount() {
return parkingList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
// configure view holder
viewHolder = new ViewHolder();
viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(MainActivity.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
}
Just do your network call in these two methods
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}
i am having a small problem with memory when i tried to used nested RecyclerView and it crashed because it was not recycling views and as a result it run out of memory. So i have to go with another solution. I have created my own layout which accepts a Adapter like this:
public class CustomTileLayout extends LinearLayout {
private Adapter mAdapter;
private Observer mObserver = new Observer();
public CustomTileLayout(Context context) {
super(context);
}
public CustomTileLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Adapter getAdapter() {
return mAdapter;
}
public void setAdapter(Adapter mAdapter) {
if (mAdapter != null)
mAdapter.unregisterDataSetObserver(mObserver);
this.mAdapter = mAdapter;
mAdapter.registerDataSetObserver(mObserver);
mObserver.onChanged();
}
private class Observer extends DataSetObserver {
#Override
public void onChanged() {
final List<View> oldViews = new ArrayList<View>(getChildCount());
for (int i = 0; i < getChildCount(); i++)
oldViews.add(getChildAt(i));
final Iterator<View> iter = oldViews.iterator();
removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
View convertView = iter.hasNext() ? iter.next() : null;
View newView = mAdapter.getView(i, convertView, CustomTileLayout.this);
addView(newView);
}
super.onChanged();
}
#Override
public void onInvalidated() {
removeAllViews();
super.onInvalidated();
}
}
}
and while i use it like:
tileHolder = (GameCategoriesTileViewHolder) holder;
section = (Section) items.get(position);
tileHolder.customTileLayout.setAdapter(new GameTileAdapter(section.getGames(), context, R.layout.row_small_tile));
Everything works fine so far but there is a problem. The problem is that it looks like this:
while i want it each row to have 3 tiles like this:
If i specify the child layout to be like the result since it is the same item it only populates the 1st tile and the next 2 are blank.
My adapter:
public class GameTileAdapter implements ListAdapter {
private List<Game> games;
private Context context;
private int resLayout = R.layout.row_small_tile;
private boolean isFavoriteFragmentView = false;
private PopupMenu popupMenu;
public GameTileAdapter(List<Game> games, Context context, int resLayout) {
this.games = games;
this.context = context;
this.resLayout = resLayout;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
#Override
public int getCount() {
return games.size();
}
#Override
public Object getItem(int position) {
return games.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
SingleItemViewHolder holder;
if (itemView == null){
LayoutInflater vi;
vi = LayoutInflater.from(context);
itemView = vi.inflate(resLayout, parent, false);
holder = new SingleItemViewHolder(itemView);
holder.game_name.setText(games.get(position).getName());
if (games.get(position).getImageUri().isEmpty()){
holder.game_imageuri.setImageResource(R.drawable.ic_image_placeholder);
}else {
Picasso.with(context).load(games.get(position).getImageUri()).into(holder.game_imageuri);
}
holder.itemLineColor.setBackgroundColor(GameManager.getmInstance(context).getGameColor(games.get(position)));
}else {
holder = (SingleItemViewHolder)itemView.getTag();
}
return itemView;
}
#Override
public boolean areAllItemsEnabled() {
return true;
}
#Override
public boolean isEnabled(int position) {
return true;
}
private static class SingleItemViewHolder{
CardView cv;
TextView game_name;
TextView game_is_new;
TextView game_jackpot;
ImageView game_imageuri;
View itemLineColor;
Button favorite_button;
Button menu_button;
SingleItemViewHolder(View itemView) {
cv = (CardView)itemView.findViewById(R.id.root_layout);
game_name = (TextView)itemView.findViewById(R.id.game_name_text);
game_is_new = (TextView)itemView.findViewById(R.id.game_new_ribbon);
game_jackpot = (TextView)itemView.findViewById(R.id.game_jackpot_ribbon);
game_imageuri = (ImageView)itemView.findViewById(R.id.game_imageuri);
itemLineColor = (View)itemView.findViewById(R.id.single_line_color);
favorite_button = (Button)itemView.findViewById(R.id.favButton);
menu_button = (Button)itemView.findViewById(R.id.menu_button_tile);
}
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return games.size();
}
#Override
public boolean isEmpty() {
return games.isEmpty();
}
}
Any ideas of how i can get the result?
You can use a layout for inner RecyclerView and add inflate a layout dynamically.
#Override
public void onBindViewHolder(RecentErrorsAdapter.ViewHolder holder, int position) {
try {
ArrayList<String> innerArrayList = new ArrayList<String>();
innerArrayList = mDataset.get(position).getInnerDataList();
for (int i = 0; i < innerArrayList.size(); i++) {
LayoutInflater inflater = null;
inflater = (LayoutInflater) context.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mLinearView = inflater.inflate(R.layout.your_layout, null);
/**
* getting id of row.xml
*/
TextView textName = (TextView) mLinearView
.findViewById(R.id.txt_name);
textName.setText(innerArrayList.get(i));
holder.lstProgramName.addView(mLinearView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I read How to create RecyclerView with multiple view type? but didn't understand few things.
First of all, my goal is:
I don't understand how getItemViewType (works in my case):
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
Because, I want to
set Item2 ONLY after the click not right at the beginning. This causes problems for me to understand, how to implement getItemViewType. Any pseudo code/prototype are more than welcome.
CODE: [FIRST APPROACH - Setting every second row as detail layout and try to hide it on default. Change visibility onClick method later on..]
public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Model> items;
FragmentActivity c;
public DifferentRowAdapter(List<Model> items, FragmentActivity c) {
this.items = items;
this.c = c;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case MEAL_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
return new ViewHolder1(view);
case DETAIL_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_test, parent, false);
return new ViewHolder2(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Model object = items.get(position);
final int currentPosition = position;
if (object != null) {
switch (object.getmType()) {
case MEAL_TYPE:
((ViewHolder1) holder).mNameTextView.setText(object.getName());
((ViewHolder1) holder).img.setImageResource(R.mipmap.ic_launcher);
break;
case DETAIL_TYPE:
((ViewHolder2) holder).detailsText.setText(object.getDescription());
break;
}
}
}
#Override
public int getItemCount() {
if (items == null)
return 0;
return items.size();
}
#Override
public int getItemViewType(int position) {
// I need ITEM2 to be at third/sixth/nineth ect. position
if(position % 3 == 0){
return DETAIL_TYPE;
}
else{
return MEAL_TYPE;
}
}
public class ViewHolder1 extends RecyclerView.ViewHolder {
TextView mNameTextView;
ImageView img;
mealsItemClickListener icl;
public ViewHolder1(View itemView) {
super(itemView);
mNameTextView = (TextView) itemView.findViewById(R.id.name);
img = (ImageView) itemView.findViewById(R.id.category_image);
}
public TextView getmNameTextView() {
return mNameTextView;
}
public void setmNameTextView(TextView mNameTextView) {
this.mNameTextView = mNameTextView;
}
public ImageView getImg() {
return img;
}
public void setImg(ImageView img) {
this.img = img;
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder {
TextView detailsText;
mealsItemClickListener icl;
public ViewHolder2(View v) {
super(v);
detailsText = (TextView) itemView.findViewById(R.id.desc);
}
public TextView getDetailsText() {
return detailsText;
}
public void setDetailsText(TextView detailsText) {
this.detailsText = detailsText;
}
}
}
[2 APPROACH - Try to add ITEM2 onClick]
public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Model> items;
FragmentActivity c;
public DifferentRowAdapter(List<Model> items, FragmentActivity c) {
this.items = items;
this.c = c;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case MEAL_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
return new ViewHolder1(view);
case DETAIL_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_test, parent, false);
return new ViewHolder2(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final Model object = items.get(position);
final int currentPosition = position;
if (object != null) {
switch (object.getmType()) {
case MEAL_TYPE:
((ViewHolder1) holder).mNameTextView.setText(object.getName());
((ViewHolder1) holder).img.setImageResource(R.mipmap.ic_launcher);
break;
case DETAIL_TYPE:
((ViewHolder2) holder).detailsText.setText(object.getDescription());
break;
}
//THIS METHOD WORKS FINE POSITIONING WISE, BUT IT DUPLICATES CLICKED ITEM INSTEAD OF PUTTING ITEM2
((ViewHolder1) holder).img.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast.makeText(c, "Position: " + currentPosition, Toast.LENGTH_SHORT).show();
addItem(currentPosition, object);
}
});
}
}
#Override
public int getItemCount() {
if (items == null)
return 0;
return items.size();
}
#Override
public int getItemViewType(int position) {
if (items != null) {
Model object = items.get(position);
if (object != null) {
return object.getmType();
}
}
return 0;
}
// Check if clicked item is left/right
private boolean isLeft(final int position){
if (position % 2 == 0){
return true; // Returns 1 if it's left columns item
}
else return false;
}
// Add item at wanted position. If left pos+2, if right+1.
private void addItem(final int position, Model object){
if(isLeft(position)){
items.add(position+2, object);
notifyItemInserted(position+2);
}
else{
items.add(position+1, object);
notifyItemInserted(position+1);
}
}
public class ViewHolder1 extends RecyclerView.ViewHolder {
TextView mNameTextView;
ImageView img;
mealsItemClickListener icl;
public ViewHolder1(View itemView) {
super(itemView);
mNameTextView = (TextView) itemView.findViewById(R.id.name);
img = (ImageView) itemView.findViewById(R.id.category_image);
}
public TextView getmNameTextView() {
return mNameTextView;
}
public void setmNameTextView(TextView mNameTextView) {
this.mNameTextView = mNameTextView;
}
public ImageView getImg() {
return img;
}
public void setImg(ImageView img) {
this.img = img;
}
}
public class ViewHolder2 extends RecyclerView.ViewHolder {
TextView detailsText;
mealsItemClickListener icl;
public ViewHolder2(View v) {
super(v);
detailsText = (TextView) itemView.findViewById(R.id.desc);
}
public TextView getDetailsText() {
return detailsText;
}
public void setDetailsText(TextView detailsText) {
this.detailsText = detailsText;
}
}
}
TL;DR ON MY SITUATION:
Approach1: Gives me
java.lang.ClassCastException: com.example.mrti.menuapp.MULTI.DifferentRowAdapter$ViewHolder2 cannot be cast to com.example.mrti.menuapp.MULTI.DifferentRowAdapter$ViewHolder1
at com.example.mrti.menuapp.MULTI.DifferentRowAdapter.onBindViewHolder(DifferentRowAdapter.java:62)
Approach2: Adding item at certain position works fine, but it adds clicked item, not item2. I'd like to focus more on approach2, but if someone can explain first one - I'm okay with that as well. I'm not sure why it is not working, because it should be - or did I miss smth?
PROBABLY APPROACH1 SOLUTION:
In First approach,
with the same position value, the value which getItemViewType(position) returns would not same to the value which items.get(position).getmType() returns.
I have to make values of type same in both getItemViewType and onBindViewHolder method;
How to do it tho?
I implemented a listview inside DialogFragment. Each item has a textview with a selector to simulate a radio group with option to select only one item at a time.
The problem is when the user clicks on an item I am updating the data on the adapter then calling to notifyDataSetChanged() and calling dialog.dismiss().
The user don`t see the selection of item before the dialog is dismissed.
I fixed this by adding postdelay to dismiss functionality, Maybe there is a better solution for this problem?
This is my code:
public class SettingsPopupAdapter extends BaseAdapter
{
private List<SettingsItem> _data;
private Context _con;
public SettingsPopupAdapter(Context con, List<SettingsItem> data)
{
_con = con;
_data = data;
}
#Override
public int getCount()
{
return _data.size();
}
#Override
public SettingsItem getItem(int position)
{
return _data.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(_con).inflate(R.layout.settings_popup_checkbox_row_item, parent, false);
holder._text = (TextView) convertView.findViewById(R.id.option_text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
SettingsItem item = getItem(position);
holder._text.setText(item.getItemLabel());
holder._text.setSelected(item.isSelected());
return convertView;
}
private static class ViewHolder
{
public TextView _text;
}
public String getSelectedItem()
{
String selectedItem = null;
for (SettingsItem item : _data)
{
if (item.isSelected())
{
selectedItem = item.getItemLabel();
break;
}
}
return selectedItem;
}
public List<String> getSelectedItems()
{
List<String> selectedOptions = new ArrayList<String>();
for (SettingsItem item : _data)
{
if (item.isSelected())
{
selectedOptions.add(item.getItemLabel());
}
}
return selectedOptions;
}
public void setData(ArrayList<SettingsItem> items)
{
_data = items;
notifyDataSetChanged();
}
}
This code is in the fragment from where I am opening the dialog :
private SettingsPopupAdapte _modAdapter;
private ArrayList<SettingsItem> _items;
_modAdapter = new SettingsPopupAdapter(getActivity(),_items);
_modificationListView.setAdapter(_modAdapter);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = _items.get(position).getItemLabel();
selectExclusiveList(selectedItem);
Bundle b = new Bundle();
b.putInt(Consts.MODIFICATION_TYPE_KEY, _type);
b.putString(Consts.RESPONSE_DATA_KEY,selectedItem);
_listener.onRetrieveData(b);
//show the change before the dialog dismissed
getView().postDelayed(new Runnable()
{
#Override
public void run()
{
dismiss();
}
},200);
}
private void selectExclusiveList(String selectedItem)
{
for (SettingsItem item : _items)
{
item.setSelected(false);
if (item.getItemLabel().equals(selectedItem))
{
item.setSelected(true);
}
}
_modAdapter.setData(_items);
}
I wanna to implement multiple button in my ListView items. But the problem is that i couldn't handle click event when button is clicked, i mean they are Play button and Image button like this
Here is my Adapter:
public class ArtistsAdapter extends BaseAdapter implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
private int resId;
private Context context;
private ArrayList<Artist> artistList;
public ArtistsAdapter(Context context, int resId, ArrayList<Artist> artistList) {
this.resId = resId;
this.context = context;
this.artistList = artistList;
this.inflater = LayoutInflater.from(context);
}
#Override
public Object[] getSections() {
return sections;
}
#Override
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
#Override
public int getSectionForPosition(int position) {
return 1;
}
private static class ViewHolder {
ImageView artistImgItem;
ImageButton artistPlayItem;
TextView artistNameItem;
TextView artistAgeItem;
LinearLayout artistItem;
}
#Override
public int getCount() {
return artistList.size();
}
#Override
public Object getItem(int position) {
return artistList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Artist artist = (Artist) getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = this.inflater.inflate(resId, parent, false);
viewHolder = new ViewHolder();
viewHolder.artistImgItem = (ImageView) convertView.findViewById(R.id.artistImg);
viewHolder.artistPlayItem = (ImageButton) convertView.findViewById(R.id.artistPlay);
viewHolder.artistNameItem = (TextView) convertView.findViewById(R.id.artistName);
viewHolder.artistAgeItem = (TextView) convertView.findViewById(R.id.artistAge);
viewHolder.artistItem = (LinearLayout) convertView.findViewById(R.id.artistItem);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
viewHolder.artistNameItem.setText(artist.getName());
viewHolder.artistPlayItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("aaaaa", position+"ssss");
Toast.makeText(context, "Music playin " + position+1 +"", Toast.LENGTH_SHORT).show();
}
});
viewHolder.artistItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
viewHolder.artistImgItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
return convertView;
}
}
And my activity looks like that
public class ArtistsActivity extends Activity {
private IndexableListView artistListUI;
private ImageView maleArtist, femaleArtist, groupArtist, artistImg;
private ArtistsAdapter artistsAdapter;
private ArrayList<Artist> maleArtistList, femaleArtistList, groupArtistList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_list);
artistListUI = (IndexableListView) findViewById(R.id.artistListView);
maleArtist = (ImageView) findViewById(R.id.maleArtist);
femaleArtist = (ImageView) findViewById(R.id.femaleArtist);
groupArtist = (ImageView) findViewById(R.id.groupArtist);
artistImg = (ImageView) findViewById(R.id.artistImg);
artistListUI.setFastScrollEnabled(true);
artistsAdapter = new ArtistsAdapter(ArtistsActivity.this, R.layout.artist_item, artistList);
artistListUI.setAdapter(artistsAdapter);
public void showArtistView() {
startActivity(new Intent(ArtistsActivity.this, AlbumsActivity.class));
}
}
you have to set clickable property of listview to false
because of entire listview click is fire
This is resolved in the adapter. Look at this example:
https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/tracks/TrackAdapter.java
In the bindView or getView method you have to define your clickhandler linked to your view:
holder.hLayout.setOnClickListener(new MentionClickhandler());
And do sour stuff in this function:
public class MentionClickhandler implements View.OnClickListener
{
public void onClick( View view ){
Track trackTag = new Track();
trackTag = (Track) view.getTag();
Log.i(TAG, "item: "+trackTag.getSearchTrack());
Bundle bundle = new Bundle();
bundle.putInt("idTrack", trackTag.getIdTrack());
bundle.putString("searchTrack", trackTag.getSearchTrack());
bundle.putInt("topTrack", trackTag.getTopTrack());
bundle.putLong("lastId", trackTag.getLastId());
bundle.putInt("count", trackTag.getCount());
bundle.putString("type", "LastMentions");
Intent i = new Intent(context, GDMentionsList.class);
i.putExtras(bundle);
context.startActivity(i);
}
}
Hope it helps...