Strikethrough text ListView cannot find symbol method - java

I'm doing a to-do list and I wanted add a strikethrough effect after double tapping the ListView but it gave me an error which says the following.
Cannot find symbol listView.setPaintFlags(listView.getPaintFlags() _ Paint.STRIKE_THRU_TEXT_FLAG)
How Can I solve it?
listView.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gd = new GestureDetector(MainActivity.this,new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
ListView listView = (ListView) findViewById(R.id.listView);
listView.setPaintFlags(listView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
return true;
}
});
#Override
public boolean onTouch(View view, MotionEvent event) {
return gd.onTouchEvent(event);
}
});
}

I do not think the ListView has the method which provides the strikethrough feature on its items. I think the TextView inside an item of your ListView has the attribute which should work like the following.
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
You might also consider implementing your custom adapter to get the reference of the TextView and strikethrough the view using the code above.

Related

How to setText in selected textView

Needed some help here. In my map test app I have 2 Autocomplete textViews. First textView gets the address of marker on load as well as chages the address if map is dragged.
But I am struggling for second textView where I need the same feature depending which textView is active.
I tried it like:
private AutoCompleteTextView mFromAddress;
private AutoCompleteTextView mToAddress;
mFromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress);
mToAddress = (AutoCompleteTextView) findViewById(R.id.toAddress);
// Decoder Coding.. str prints address
if (mFromAddress.isSelected()) {
mFromAddress.setText(str);
} else if (mToAddress.isSelected()) {
mToAddress.setText(str);
}
But no expected results. Even the first textView stops working.
I am new to java and Android Studio. I am looking for some suggestions here.
Try hasFocus() function
if (mFromAddress.hasFocus()) {
mFromAddress.setText(str);
} else if (mToAddress.hasFocus()) {
mToAddress.setText(str);
}
Another way is that define another TextView in code and set it as active TextView when focus change in OnFocusChangeListener
AutoCompleteTextView activeET;
activeET = mFromAddress; // set mFromAddress as active text in start
mFromAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus)
activeET = mFromAddress;
}
});
mToAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus)
activeET = mToAddress;
}
});
Now you just need to set text to activeET
activeET.setText(str);

Get an Edittext value of an listview when click on it's item

I have a litview in my code and into each item there is an edittext with a button , So what i want to do is get the edittext's value when press on the button of an listview's item.
I'v tried to get the value by declaring it like this , but it returns null value !
public View getView(int i, View view, ViewGroup viewGroup){
LayoutInflater linflater = getLayoutInflater();
View view1 = linflater.inflate(R.layout.row_view, null);
final EditText replyfld = (EditText) view1.findViewById(R.id.replyfld);
final Button sendreplybtn = (Button)view1.findViewById(R.id.sendreplybtn);
sendreplybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String thereplyvalue = replyfld.getText().toString();
Toast.makeText(MessagesActivity.this, thereplyvalue, Toast.LENGTH_SHORT).show();
}
});
}
What to do please ?
From the code at pastebin.com/HejVn4bb .
Looks like calling replytogglebtn.setFavorite(false,true); (Line:101) in onClickListener before getting text is the problem because setOnFavoriteChangeListener() clears the edittext.
just get text before the setFavorite(false,true);
You have to do it inside your Adapter. Can you past the code of it?

Custom adapter's asynchronous behaviour

I have created my own custom adapter class in my android app and I am calling it from one of my activity. I am adding some elements to the view from the adapter class and I need to access those variables from my activity class.
Now, ideally I would expect it to fill the view and then execute the further code in my activity class, but adapter class is taking some time to populate the view and in the meanwhile further code in my activity class is getting executed where no such elements have been added yet.
How do I handle this situation? I come from a js background. Do we have something like promises in java?
According to the answers I have my changed my code to this:
public class HomeActivity extends Activity {
GridView grid;
String text[] = {"Calendar","Uber","Weather","News","Youtube","Clock","Email","Maps","Twitter","Facebook"};
String list_app_name[] = {"calendar","uber","weather","news","youtube","clock","email","maps","twitter","facebook"};
String id_button[] = {"button_calendar","button_uber","button_weather","button_news","button_youtube","button_clock","button_email","button_maps","button_twitter","button_facebook"};
int image[] = {R.drawable.social_icons1,R.drawable.social_icons2,R.drawable.social_icons3,R.drawable.social_icons4,
R.drawable.social_icons5,R.drawable.social_icons6, R.drawable.social_icons7,R.drawable.social_icons8,
R.drawable.social_icons9,R.drawable.social_icons10};
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
//setting up the adapter for gridView
grid = (GridView)findViewById(R.id.simpleGrid);
ImageAdapter ia = new ImageAdapter(this,image,text,id_button);
grid.setAdapter(ia);
ia.notifyDataSetChanged();
try {
initStateOfApps();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void initStateOfApps() throws JSONException {
Log.d("here","here");
ArrayList<String> list = getEnabledApps();
Log.d("apps",list.toString());
for(int i=0;i<list.size();i++) {
String app_name = list.get(i);
ToggleButton button=null;
if(app_name.equals("calendar")) {
button = (ToggleButton)findViewById(R.id.button_calendar);
button.setChecked(true);
}
}
}
}
So what is happening is that I am creating some toggle buttons that are getting populated in the ImageAdapter class that I wrote.
Once the ImageAdapter is called, I call the notifydatasetchanged() on the adapter in order to update the view.
What I am doing inside the adapter is giving each of the toggle buttons some custom ID I wrote in res/values/ids.xml.
After using setId on each of the toggle buttons, I try using that ID in my activity class but it gives me nullPointerException in the initStateOfApps() where I am trying to change the state of button.
So even after using the notifyDataSetChanged it is still behaving the same.
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
private final int item_image[];
private final String item_text[];
private final String button_id[];
public ImageAdapter(Context context, int item_image[], String[] item_text,String[] button) {
this.context = context;
this.item_image = item_image;
this.item_text = item_text;
this.button_id = button;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from custom_gridview.xml
gridView = inflater.inflate(R.layout.item, null);
// set value into imageview
final ImageView image = (ImageView) gridView.findViewById(R.id.item_image);
image.setImageResource(item_image[position]);
// set value into textview
TextView text = (TextView) gridView.findViewById(R.id.item_text);
text.setText(item_text[position]);
final ToggleButton button_ = (ToggleButton) gridView.findViewById(R.id.item_button);
if(position==0) {
button_.setId(R.id.button_calendar);
image.setId(R.id.image_calendar);
}
button_.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(context.getResources().getResourceEntryName(toggleButton.getId()).equals("button_calendar")) {
if(isChecked) {
try {
setStateOfApp("calendar","true");
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(context, GoogleApp.class);
((Activity) context).startActivityForResult(intent,10);
} else {
try {
setStateOfApp("calendar","false");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
} else {
gridView = (View) convertView;
}
return gridView;
}
}
You are trying to access View which is not a part of Activity's content view. So you can't access that view directly.
button = (ToggleButton)findViewById(R.id.button_calendar); // will return null
This ToggleButton will be null because findViewById will fail to find out ToggleButton in current content view because that view is present in your Adapter not in content view.
And you are getting nullpointerException because you are trying to access property on null view.
button.setChecked(true); // This button is null
In java we have <Future>, but I don't think it's what you're looking for.
The adapter (extending BaseAdaper) behaviour lets you create the adapter and, even in a second moment, change underlying data via getAdapter().setData() or whatever method you choose to add.
From this perspective, the adapter is a "stupid" component acting as A View containers, you should retrieve data elsewhere (CursorAdapter is different).
So, in your Activity, fill the adapter with needed data and, when finished, call adapter.notifyDatasetChanged(). This will inform the adapter that its own data has changed and it must refresh views
Yes, ideally, the population of the adapter should be coming from the outside. The adapter should really just take in a list of data and map that data to the views. For example, some method or task in the Activity could produce a list of data (probably asynchronously...since you mentioned it) that you then pass into the adapter and then you can notifyDataSetChanged() if you need to.
I can't see your code, but if for some reason the data is truly required to be populated from inside the adapter, you could use an event bus and subscribe to it in the Activity. I would recommend going with the first option, but here are some links if you choose to use an event bus:
https://github.com/greenrobot/EventBus
http://square.github.io/otto/
As per my understanding with your question
You are not properly managed the adapter data in your activity.
If any of the data or code interlinked with your adapter data or values
Then you can start those code after you retrieve the values or data and update the view in your activity.
Please note that use Viewholders in adapter to avoid slow populating and scrolling in listviews.
Viewholders will smooth your process.
I personally suggest you that
Please go with Recyclerview and RecyclerViewAdapter.
So many Android developers are using it.
If you have background tasks in adapter you can prefer to use RX Java or EventBus
If you provide the code
It's better for us to suggest exact solution

How to target an element in a ListView with ShowcaseView in Android?

I targeted the items in an action bar using ShowcaseView, but I can't target elements of a ListView! I tried that and it didn't work:
ShowcaseView sv = new ShowcaseView.Builder(this,true)
.setTarget(new ViewTarget(lv.getChildAt(1).findViewById(R.id.heart)))
// Here is where you supply the id of the action bar item you
// want to display
.setContentTitle("Heart")
.setContentText("Check the venues of your city")
.hideOnTouchOutside()
.build();
Very simple. Take the element in your ViewHolder then place the code for showing the ShowCaseView inside your bindView.
Don't forget to put different a different ID for each element in the list.
So you may try something like this.
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView button;
public ViewHolder(final View itemView) {
super(itemView);
button = (Button) itemView.findViewById(R.id.list_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MaterialShowcaseView.Builder(getActivity())
.setTarget(verifyButton)
.setDismissText("GOT IT")
.setContentText("Your text goes here")
.setDelay(100) // optional but starting animations immediately in onCreate can make them choppy
.singleUse(id + " define an unique id for each item of the list") // provide a unique ID used to ensure it is only shown once
.show();
}
});
}
}
}
To get more information you can check the library here.

Using setOnItemClickListener, is undefined for the type TextView?

I have a ListView with Checkboxes, (TextView+Checkboxes) and I did the setOnLongClickListener for the items itself:
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.text.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Viewholder comes from my custom adapter to my Listview, no need to specify details I guess. This works pretty good, problem is: if I try to to the same just with setOnItemClickLister (I just want to do a "small" click) I get the error:
The method setOnItemClickListener(new OnItemClickListener(){}) is
undefined for the type TextView
Tried this way:
viewHolder.text.setOnItemClickListener(new OnItemClickListener() {
and
viewHolder.text.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){
But I always get that error. What can I do ?
I think you need a plain OnClickListener instead of an OnItemClickListener
viewHolder.text.setOnClickListener(new OnClickListener() {
You are casting your "text" argument to a "TextView". This Class has by definition no items.
So there will never be a "OnItemClick" event.
You want do define this behavior on your ListView, not on your text inside the items.
Example:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
...
}
);
In your case you want to act on what you described as "...my Listview...".
On the other hand, if you realy want to act on the TextView inside your item, then you should implement the "setOnClickListener" method for your TextView.
Example:
viewHolder.text.setOnClickListener(new OnClickListener(){
...
}
);

Categories

Resources