OClickItem of twitter listViwe Fabric Kit - android

I'm Using Fabric kit of twitter to my android app , and I have a listview of tweets but whene I try to click on a tweet (item) to open a new activity ( that's shows the details of the tweet) but whene I click to the item , an app chooser apear automatically
It seems that the twitter-api already set an onItemClickListener so I'm asking if someone had already use Fabric kit I need help
ListView tweetLV = (ListView) root.findViewById(R.id.list_tweets);
adapter = new TweetViewAdapter<CompactTweetView>(getActivity());
tweetLV.setAdapter(adapter);
tweetLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String hashtag = adapter.getItem(i);
Intent intent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, hashtag);
startActivity(intent);
}
});

As I suggest on this question you can try to create a custom adapter that overrides the default onClick behavior of tweet:
class CustomTweetTimelineListAdapter extends TweetTimelineListAdapter {
public CustomTweetTimelineListAdapter(Context context, Timeline<Tweet> timeline) {
super(context, timeline);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
//disable subviews to avoid links are clickable
if(view instanceof ViewGroup){
disableViewAndSubViews((ViewGroup) view);
}
//enable root view and attach custom listener
view.setEnabled(true);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO: implement your custom behavior when clicks
}
});
return view;
}
// see full example to find helper method disableViewAndSubViews
}
Full code example here
Hope it helps.

Related

Defining onlick for listview: Onclick at listView level vs Onclick inside custom view adapter

In custom adapters click event handling, which is better (considering performance and memory) , 1)Defining click handler at ListView level. 2) Defining a click handler inside custom adapter.
Will defining click handler at Custom Adapter level increase the memory consumption?.
Below is some sample pseudo code.
//Defining click handler at list level
layoutNotificationViewHolder.mNotificationListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final SampleItem selectedItem = itemList.get(position);
Intent itemInfoIntent = new Intent(context, itemMoreInfoActivity.class);
itemInfoIntent.putExtra("selected_item", selectedItem);
context.startActivity(itemInfoIntent);
}
});
//Defining click handler inside adapter.
public View getView(final int position, View convertView, ViewGroup parent) {
final SampleItem selectedItem = itemList.get(position);
convertView.mCameraIv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent itemInfoIntent = new Intent(context, itemMoreInfoActivity.class);
itemInfoIntent.putExtra("selected_item", selectedItem);
context.startActivity(itemInfoIntent);
}
});
}
First, List view is depracated, use RecyclerView instead.
For your question, if the handler of the clicks is the adapter (ex 1) so no new objects will be created which is good for memory, and if you will set it like in ex2 - new object will be created each time getView function will be called which less good. But in general it is not so expensive so both examples are fine.

OnItemClickListener for multiple AutoCompleteTextView with switch case not working

I have multiple AutoCompleteTextView in layout, so i've implemented AdapterView.OnItemClickListener globally.
Now issue is that,
i can't compare AutoCompleteTextView using switch case inside listener,
Code
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (v.getId()) {
case R.id.ac_education:
s_ac_education = testArray[position];
break;
case R.id.ac_ethincity:
s_ac_ethincity = testArray[position];
break;
case R.id.ac_languages:
s_ac_languages = testArray[position];
break;
case R.id.ac_location:
s_ac_location = testArray[position];
break;
case R.id.ac_religion:
s_ac_religion = testArray[position];
break;
case R.id.ac_travel:
s_ac_travel = testArray[position];
break;
}
}
Note: view.getId() always returning same value,
i've also tried if-else but that doesn't work also.
(I've already wasted couple of hours)
EDIT
private void init(View v) {
ac_languages = v.findViewById(R.id.ac_languages);
ac_religion = v.findViewById(R.id.ac_religion);
ac_location = v.findViewById(R.id.ac_location);
ac_travel = v.findViewById(R.id.ac_travel);
ac_ethincity = v.findViewById(R.id.ac_ethincity);
ac_education = v.findViewById(R.id.ac_education);
ptr.setACTVAdapter(getActivity(), ac_education, ac_ethincity, ac_languages, ac_location, ac_religion, ac_travel);
ac_languages.setHint(R.string.languages);
ac_religion.setOnItemClickListener(this);
ac_religion.setHint(R.string.religion);
ac_location.setOnItemClickListener(this);
ac_location.setHint(R.string.where_do_you_live);
ac_travel.setOnItemClickListener(this);
ac_travel.setHint(R.string.where_can_you_travel);
ac_ethincity.setOnItemClickListener(this);
ac_ethincity.setHint(R.string.ethincity);
ac_education.setOnItemClickListener(this);
ac_education.setHint(R.string.education);
}
setAdapter(is defined in another class)
public void setACTVAdapter(Activity activity, AutoCompleteTextView... actv) {
testArray = activity.getResources().getStringArray(R.array.testArray);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, testArray);
for (AutoCompleteTextView ac : actv) {
ac.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
Thanks
First, create your custom onItemClickListener:
public class MyClickListener implements AdapterView.OnItemClickListener {
AutoCompleteTextView ac;
public MyClickListener(AutoCompleteTextView myAc){
ac = myAc;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch(ac.getId()){
case R.id.autocomplete_1:
//your code
break;
case R.id.autocomplete_2:
//your code
break;
}
}
}
Use it for your AutoCompleteTextView:
autoCompleteTextView.setOnItemClickListener(new MyClickListener(autoCompleteTextView));
You should not rely on view when you're working with a list, specially when you're setting a general item click listener.
Instead get an instance to each view by their id on public View getView(int position, View convertView, ViewGroup parent) method of your AutoCompleteTextView adapter and declare a click listener to work with the position you got there, if you want to set multiple click listeners on a row individually.
For example:
public class ExampleAdapter extends BaseAdapter {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView.findViewById(R.id.ac_education);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
}
...
}
According to official doc for AdapterView.OnItemClickListener,
onItemClick() method have following parameters,
parent: The AdapterView where the click happened.
view: The view within the AdapterView that was clicked (this will be a
view provided by the adapter)
position: The position of the view in the adapter.
id: The row id of the item that was clicked.
So, you might want to use parent instead of view, to receive id of that parent AutoCompleteTextView.
(Check this answer to get more details.)
It's not the child that you want to track it's clicks, it's convertView.
So, if you really want to handle child clicks outside of your adapter, you can do like this:
public interface OnViewClickListener {
void onViewClick(View view, int position);
}
Then inside of your adapter:
private OnViewClickListener mListener;
public void setOnViewClickListener(final OnViewClickListener listener) {
mListener = listener;
}
And for every child you want:
child.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
if (mListener != null) {
mListener.onViewClick(v, position);
}
}
});
And with this you can get item clicks:
adapter.setOnViewClickListener(new OnViewClickListener() {
#Override
public void onViewClick(final View view, final int position) {
** PUT YOUR SWITCH CASE HERE! **
}
});
First setup data into Pojo class using array list. And return as string for autotextview. Match that string with arraylist's item value using for loop.
autoPartyName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
for (CustomersModel list : customersList) {
if(list.toString().equals(adapterView.getItemAtPosition(position))){
etContactPhone.setText(list.getContact());
etContactAddress.setText(list.getAddress());
}
}
}
});

Button on ListAdapter

I have button on items in my list adapter. The problem occurs when I click the button. The action is performed on the right item, but it disables wrong button.
My code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_friend, parent, false);
user = userList.get(position);
ImageView imageView = (ImageView)view.findViewById(R.id.user_image);
Picasso.with(JSONUserAdapter.this.getContext()).load("https://s3.amazonaws.com/profiles-pictures/"+ user.getPath()).into(imageView);
sendRequest = (ImageButton)view.findViewById(R.id.sendRequest);
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendFriendRequest();
disabledAdd();
}
});
return view;
}
Do not try to catch click events for the buttons inside the rows of ListView. Each row of ListView is already clickable. Set the onItemClickListener for the ListView:
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long l) {
//DO SOMETHING HERE
//Use "position" to get the index of item clicked
}
});
As i can see that we had defined the "User" object as a globel variable and it will store the last postions item data.
So when you will click on any button the last items data will be reflected.
{UserType}Object user = userList.get(position);
You can set Tag on View
Use like this:
sendRequest.setTag(user);
And When user clicks item let's get data from tag like this
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here you have user that was clicked.
User user = v.getTag();
sendFriendRequest();
disabledAdd();
}
});

retrieve value of one text view when there are many on ietm click listener

I have custom view defined for my list. There are 3 text fields in the custom view. On the item click listener i want to retrieve value of one of the text view. How can I do that?
use below code
mListviewObject.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
personId = (TextView) view.findViewById(R.id.personId);
String val_personid = personId.getText().toString();
}
});
In this way you can chose whichever textview you want to refer
textView.getText().toString() can be used to retrieve value
Something like this:
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.textview1:
mTextview1.getText().toString();
break;
case R.id.textview2:
mTextview2.getText().toString();
break;
case R.id.textview3:
mTextview3.getText().toString();
break;
}
try this I used this code in my project :)
final View tv = v.findViewById(R.id.text);
final String s = (String) ((TextView) tv.findViewById(R.id.text)).getText();
All Views have a findViewById(int id) method. You can find the smaller View within your custom View by using this method like so:
View customView;
customView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
View innerView = customView.findViewById(R.id.innerView);
}
});
Then you will have a handle on your inner view.

How to handle ListView click in Android

How do I listen to click event on a ListView?
This is what I have now
ListView list = (ListView)findViewById(R.id.ListView01);
...
list.setAdapter(adapter);
When I do the following
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView,
int position, long id)
{
setDetail(position);
}
public void onNothingSelected(AdapterView parentView) {
}
});
That doesn't seem to do anything on click.
And all those code live within a class that extends Activity.
On your list view, use setOnItemClickListener
Suppose ListView object is lv, do the following-
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv.getItemAtPosition(position);
/* write you handling code like...
String st = "sdcard/";
File f = new File(st+o.toString());
// do whatever u want to do with 'f' File object
*/
}
});
You need to set the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.list_item_text, null);
convertView.setClickable(true);
convertView.setOnClickListener(myClickListener);
and declare the click listener in your ListActivity as follows,
public OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
//code to be written to handle the click event
}
};
This holds true only when you are customizing the Adapter by extending BaseAdapter.
Refer the ANDROID_SDK/samples/ApiDemos/src/com/example/android/apis/view/List14.java for more details
The two answers before mine are correct - you can use OnItemClickListener.
It's good to note that the difference between OnItemClickListener and OnItemSelectedListener, while sounding subtle, is in fact significant, as item selection and focus are related with the touch mode of your AdapterView.
By default, in touch mode, there is no selection and focus.
You can take a look here for further info on the subject.
This solution is really minimalistic and doesn't mess up your code.
In your list_item.xml (NOT listView!) assign the attribute android:onClick like this:
<RelativeLayout android:onClick="onClickDoSomething">
and then in your activity call this method:
public void onClickDoSomething(View view) {
// the view is the line you have clicked on
}
You have to use setOnItemClickListener someone said.
The code should be like this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text or do whatever you need.
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
First, the class must implements the click listenener :
implements OnItemClickListener
Then set a listener to the ListView
yourList.setOnItemclickListener(this);
And finally, create the clic method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +countries[+ position], Toast.LENGTH_SHORT).show();
}
you can take a look and download code here
Use setOnItemClickListener() api in your activity. Following is the sample.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<> parent, View view, int position, long id)
{
// your code here.
}
});
In Kotlin, add a listener to your listView as simple as java
your_listview.setOnItemClickListener { parent, view, position, id ->
Toast.makeText(this, position, Toast.LENGTH_SHORT).show()
}

Categories

Resources