Get item selected in the custom adapter listview android - android

My every list view item has its own linearlayout component.The list view is in the activity with onItemClick on itself.
In my custom adapter file, I have the onclick on this linearlayout,
private ArrayList<Book> bookArray; // this is the data source
.........
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { // fire user Like
//*** how can I get which item is selected here?
But how can I get the item index whose linearlayout is clicked?
Regards
Hammer

Use setTag() and getTag() to remember positon and use it on click of LinerLayout.
in getView
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout .setTag(position);
and into onclick of LinerLayout take it as
imgLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
//....
}
}

You can set the tag for your linear layout and inside your onClick you can get the tag which will return the position for which clickEvent get Trigger.
See below code:
private ArrayList<Book> bookArray; // this is the data source
.........
LinearLayout imgLayout = (LinearLayout) rowView.findViewById(R.id.imageLayout);
imgLayout.setTag(position);
imgLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) { // fire user Like
//*** how can I get which item is selected here?
Log.d(TAG,"Clicked Pos of Row"+v.getTag().toString();

Related

How to get control to other item in recyclerview?

I have a horizontal recyclerView,when I clicked on each of the element,a LinearLayout inside it setVisibility(VISIBLE) ,this work fine.
So what I want now is,when I clicked in current item,LinearLayout setVisibilty(VISIBLE)(as usual) at the same time,if I clicked another item before,the LinearLayout inside it setVisibility(GONE).
What I tried so far
viewHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(viewHolder.linearLayout.getVisibility() == View.VISIBLE){
//hide
viewHolder.linearLayout.setVisibility(View.GONE);
}else{
int nowClickedId = 0;
//hide LinearLayout of other position that clicked before
if(nowClickedId != position){
viewHolder.linearLayout.setVisibility(View.GONE);
}
//show for current clicked item
viewHolder.linearLayout.setVisibility(View.VISIBLE);
//get current item position store at the array
nowClickedId = viewHolder.getAdapterPosition();
}
}
});
So far I just have control at the item in current position,how can get the control of the item of clicked previously?
Create a variable to store the position of the item clicked and declare it globally.
private int nowclicked=-1;
Now inside your viewholder whenever position is clicked store it to the global variable and call notifyDataSetChanged() which notifies the adapter that data is changed.
private class Sample extends RecyclerView.ViewHolder{
public Sample(View itemView) {
super(itemView);
viewHolder.container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nowclicked = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
In the onBindViewHolder if the posistion is equal show linearLayout else hide it.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(nowclicked==position){
viewHolder.linearLayout.setVisibility(View.VISIBLE);
}else {
viewHolder.linearLayout.setVisibility(View.GONE);
}
}
Try to write this code inside the onBindViewHolder() method and ensure to write
holder.viewHolder.container.setOnClickListener();
And according the position you can do whatever you want.

How to get position of the clicked item in Flexbox Layout in android?

I am using Android Flexbox Layout. I have managed to add custom views to the layout from a list and setting click listeners to each item. But the problem is getting the position of the clicked item. I am sharing my current code here.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
FlexboxLayout fLayout;
final ArrayList<String> strList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
for (int i=0 ;i<30; i++){
strList.add("Item "+i);
}
fLayout = (FlexboxLayout)findViewById(R.id.flexLayout);
for(int i=0;i<strList.size();i++){
addView(strList.get(i),i);
}
fab.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.fab:
addItem();
break;
}
}
private void addItem(){
strList.add("Item "+ (strList.size()));
addView(strList.get(strList.size()-1),strList.size()-1);
}
private void addView(String string, int pos){
LayoutInflater inflater = LayoutInflater.from(this);
final View item = (View) inflater.inflate(R.layout.flow_layout_item, fLayout, false);
TextView textView = (TextView) item.findViewById(R.id.textView);
textView.setText(string);
Button btn = (Button) item.findViewById(R.id.btnClose);
FlexboxLayout.LayoutParams layoutParams = (FlexboxLayout.LayoutParams) item.getLayoutParams();
item.setLayoutParams(layoutParams);
item.setClickable(false);
/*Here I set click listener for button in custom view*/
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
fLayout.addView(item);
}
}
How can I get the position of the clicked items? Thanks in advance.
While I'm not entirely sure of how to get the position of clicked items through your current method, I know this can be easily achieved when using the FlexboxLayout with a RecyclerView.
You will be able to get the position from the RecyclerView adapter's onBindViewHolder() method.
This was achieved following the answer of #moyheen
I used RecyclerView with FlexBoxLayoutManager.
RecyclerView recyclerView = (RecyclerView)
context.findViewById(R.id.recyclerview);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);

Android Listview - Change Item Button Text After Clicking

I am trying to convert my app to android version, and I am a bit new in android. I have a list view in which the items include buttons. It is a user list and you can follow each user, when the button is clicked, only in the item including the button, the button text should turn in to "followed". Retrieving the list works fine, but with my below code the button text is not changing. How can I make this happen? Thank you very much.
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter() {
super(getActivity().getApplicationContext(), R.layout.fragment_users_cell, myItemList);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View cellView = convertView;
if (cellView == null){
cellView = getActivity().getLayoutInflater().inflate(R.layout.fragment_users_cell, parent, false);
}
profileBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_profileBtn);
followBtn = (Button) cellView.findViewById(R.id.fragment_users_cell_followBtn);
profileBtn.setText(myItemList.get(position));
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
System.out.println(position);
}
});
followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
profileBtn.setText("followed");
}
});
return cellView;
}
}
You have to update your dataset and refersh the list after you make changes so that it reflects the latest changes. In your case the text changes.
followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
profileBtn.setText("followed");
myItemList.add(position, "followed");//Change your dataset
notifyDataSetChanged(); //And refresh the adapter
}
});
you need to change the value in myItemList so that next time when view load, the value from list set as text on profile button comes which ix followed
i.e. you need to update the list on click of follow button and notify the ListView.
followBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
System.out.println(myItemList.get(position));
myItemList.get(position).set("followed");
profileBtn.setText("followed");
}
});

Add views within the OnClick

I have a EditText box which is used to input names. Once the names are inputed and the add button is clicked I need the name to be saved into a Array that can be used in the next Activity and to also clear the EditText box. I think I have this working but not to sure if it is correct you can see the code below.
My actuall question is a little different when the add button is clicked I also need it to show the name that was just added next to the add button with its own remove button and if more are added they need to appear below like a kind of list view. If the remove button is clicked it needs to then remove the name and remove button from screen and the name out of the array.
Code has been updated I am trying to populate the ListView from a Adapter but with no sucsess so far
I am thinking something like adding a TextView and Button within the onClick method. And when the button Remove is clicked I would need to remove the TextView string from the playerList. I think I would also have to remove the View so it can no longer be seen.
Can any help me out on the code side of this can't seem to figure it out.
public class AddRemove extends Activity {
ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
ListView listView = (ListView)findViewById(R.id.namelistview);
listview.setAdapter(new myAdapter(getApplicationContext(), R.layout.listview_content, list));
Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
String name = playername.getText().toString();
playerList.add(name);
playername.setText("");
}});
Button play = (Button) findViewById(R.id.playnow);
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent( demo.AddRemove.this, demo.PasswActivity.class);
Bundle extras = new Bundle();
extras.putSerializable( "com.example.playerList", playerList );
i.putExtras( extras );
startActivity( i );
}});
}
class myAdapter extends ArrayAdapter{
List<String> users;
public myAdapter(Context context, int textViewResourceId, List<String> list) {
super(context, textViewResourceId);
// assign list
users = list;
// TODO Auto-generated constructor stub
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
}
}
}
listview_content XML
<TextView
android:id="#+id/playername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove" />
You should be using a ListView to display your list of people. Within this List, you would have a layout with 2 components in each row. These would include one TextView and one Button, the name of the person and the button to remove the component. When the button is clicked, remove the item from the Adapter and notify the ListView that it has changed.
When you are configuring your ListView your adapter will need to have a custom Adapter that will be used to configure each row of your ListView.
Update
Sample Code:
http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/
First of all, you need to initialize each TextView with a reference to the context. So, in your onClick, given that it is in ViewOnClick.java:
public class ViewOnClick extends Activity {
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.Button01);
ll = (LinearLayout)findViewById(R.id.ll);
layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
TextView view = new TextView(ViewOnClick.this);
view.setText(++i+" view");
ll.addView(view, layoutParams);
}});
}
}
Second, given that you have a call to setContentView in onCreate which also adds your linearlayout to the view, you do not need to call setContentView(linearLayout) each time the button is clicked.

How can i attach an listener to listview?

I have a ListView with a custom layout for each row having one TextView and three EditText. When I am clicking on individual row in ListView one activity is started and that takes you to another page.
I wrote some code but its not working. The code is shown below.
In adapter class getView() method i have place the below code
convertView = mInflater.inflate(R.layout.editcategorylist, null);
convertView.setClickable(true);
convertView.setOnClickListener(clickListener);
and I declare the click listener in your ListActivity as follows
lv=getListView();
myClickListener = new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
startActivity(intent);
}
};
Thank you.
Use something like this
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
//Here write your code for starting the new activity on selection of list item
}
public void onNothingSelected(AdapterView parentView)
{
}
});
Please use setOnItemClickListener

Categories

Resources