flofreelance
flofreelance

Reputation: 944

android:onClick get the id of the clicked item

I have a list of posts with the autor name. I want to know which author name had been clicked.

example of the list of posts

The layout :

<TextView
    android:id="@+id/user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:layout_marginLeft="10dp"
    android:textStyle="bold"
    android:onClick="userClick"
    tools:text="User" />

<TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:layout_alignLeft="@+id/user"
    android:layout_below="@+id/user"
    android:onClick="postClick"
    tools:text="Content" />

When an author name is clicked this function is called :

public void userClick(View v) {
    //Is it possible to know which author name had been clicked from here?
}

What is the correct method to identify on which author name the user had clicked.

EDIT

I've tryied the getId() method

public void userClick(View v) {
Log.d("userClick called :",String.valueOf(v.getId()));
}

It always return the same id even if I click on different users.

enter image description here

Upvotes: 2

Views: 10810

Answers (4)

user7343325
user7343325

Reputation:

If you are using listview, you can use the following:

String author ;
yourListView.setOnItemClickListener (new AdapterView.OnItemClickListener(){
@Override 
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Textview user = (TextView) view.findViewById(R.id.user);
author=user.getText().toString();
}
});

Update : Did you try that ?

public void userClick(View view){
TextView textview = (TextView) view;
author=textview.getText().toString();
}

Upvotes: 7

Sumukh Bhandarkar
Sumukh Bhandarkar

Reputation: 384

In the java file, after calling the onClickListener, try to access the button or clickable using the this keyword. It refers the the context of the clicked item. Works in Java and JavaScript.

Upvotes: 0

Ben P.
Ben P.

Reputation: 54194

I would solve this problem by implementing an AdapterView.OnItemClickListener and adding it to my ListView. The following solution works because both MyAdapter and MyListener have access to the Activity's posts field.

public class MainActivity extends AppCompatActivity {

    private List<Post> posts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        posts = new ArrayList<>();
        posts.add(new Post("author1", "title1"));
        posts.add(new Post("author1", "title2"));
        posts.add(new Post("author2", "title3"));
        posts.add(new Post("author3", "title4"));
        posts.add(new Post("author2", "title5"));
        posts.add(new Post("author1", "title6"));
        posts.add(new Post("author3", "title7"));
        posts.add(new Post("author3", "title8"));

        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(new MyAdapter());
        list.setOnItemClickListener(new MyListener());
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return (posts != null) ? posts.size() : 0;
        }

        @Override
        public Post getItem(int position) {
            return posts.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post, parent, false);
            }

            ViewHolder holder = (ViewHolder) convertView.getTag();

            if (holder == null) {
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }

            Post item = getItem(position);
            holder.user.setText(item.user);
            holder.title.setText(item.title);

            return convertView;
        }
    }

    private class MyListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Post item = posts.get(position);
            Toast.makeText(MainActivity.this, item.user, Toast.LENGTH_SHORT).show();
        }
    }

    private static class ViewHolder {

        private final TextView user;
        private final TextView title;

        public ViewHolder(View v) {
            this.user = (TextView) v.findViewById(R.id.user);
            this.title = (TextView) v.findViewById(R.id.title);
        }
    }

    private static class Post {

        private final String user;
        private final String title;

        public Post(String user, String title) {
            this.user = user;
            this.title = title;
        }
    }
}

Upvotes: 0

Barış S&#246;be
Barış S&#246;be

Reputation: 468

I assume that this is the RecyclerView.

And you can use

RecyclerViewAdapter.class

private View.OnClickListener globalClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int clickedPosition = (int) v.getTag();
                // do something with position
            }
        }

RecyclerViewAdapter.onBindViewHolder

holder.itemView.setTag(position);
holder.itemView.setOnClickListener(globalClickListener);

Also if you want to get view id,

private View.OnClickListener globalClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             if(R.id.title == v.getId()){
             // Clicked child view (R.id.title which is implemented in XML)
        }

Upvotes: 0

Related Questions