Spannable string not clickable in a custom ListView - java

Using Button on a row in the ListView and am able to make a single row Spannable and data from database but when i click it is not responding to click here is my code from MyAdapter Class where am entering values from database to Listview
public View getView(int i, View view, ViewGroup viewGroup) {
Realm realm=Realm.getDefaultInstance();
Word toEdit = realm.where(Word.class)
.equalTo("id", 10).findFirst();
int id_to_seperate=toEdit.getLang();
LayoutInflater inflater= (LayoutInflater) Main.context.getSystemService(Main.context.LAYOUT_INFLATER_SERVICE);
View row= inflater.inflate(R.layout.layout,viewGroup,false);
TextView word= (TextView) row.findViewById(R.id.word_name);
TextView meaning= (TextView) row.findViewById(R.id.word_define);
Word temp=list.get(i);
int idz=temp.getId();
word.setText(temp.getWord());
if(id_to_seperate==idz){
String span[] = temp.getMeaning().substring(1).split(" ") ;
SpannableString ss = new SpannableString(temp.getMeaning().substring(1));
ClickableSpan spans[] = new ClickableSpan[span.length];
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
#Override
public void onClick(View textView) {
TextView v = (TextView)textView ;
String text = v.getText().toString() ;
Log.d("View" , text);
}
};
}
int start = 0 ;
int end =0;
try {
for(int spanCount = 0 ; spanCount <span.length ; spanCount++){
if(spanCount==0) {
end += span[spanCount].length();
}else{
end += span[spanCount].length()+1;
}
ss.setSpan(spans[spanCount], start, end , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += span[spanCount].length()+1;
}
} catch (Exception e) {
e.printStackTrace();
}
meaning.setText(ss);
}else {
meaning.setText(temp.getMeaning().substring(1));
}
return row;
}
am able to get result from a specific row in a list
to this But click Functionality not working, I would be thankful if anyone can tell me what mistake I have done
Regards

Before return add
meaning.setMovementMethod(LinkMovementMethod.getInstance());
This method enabled clicable on link.

Related

How to get text from all TextViews RecyclerView

First of all sorry for the language. I have a RecyclerView with items. I have a checkbox in each item. I have onCheckedChangeListener inside RecyclerAdapter.
When I check 4 checkboxes I need to disable the remaining. So how can I get an access for them? I can do it when on create Recycler by checking how many items are selected. But can't find how to get access for every checkbox in onCheckedChange method. Screenshot example
public class EditAtributesAdapter extends RecyclerView.Adapter<EditAtributesAdapter.EditAtributesViewHolder> {
private ArrayList<AtributeEditItem> mEditAtributeList;
DBHelper dbHelper;
public static class EditAtributesViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public CheckBox mCheckBox;
public EditAtributesViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.edit_atribute_icon);
mCheckBox = itemView.findViewById(R.id.enabledAtribute);
}
}
public EditAtributesAdapter(ArrayList<AtributeEditItem> editAtributeList){
mEditAtributeList = editAtributeList;
}
#Override
public EditAtributesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.atribute_edit_item, parent, false);
final Context forClickCTX = parent.getContext();
final EditAtributesViewHolder eavh = new EditAtributesViewHolder(v);
eavh.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = eavh.getAdapterPosition();
// System.out.println("Hello, you clicked: " + mEditAtributeList.get(pos).getName());
dbHelper = new DBHelper(forClickCTX);
try {
dbHelper.createDataBase();}
catch (IOException ioe) {
throw new Error("Не удалось создать базу данных");}
try {
dbHelper.openDataBase();}
catch (SQLException sqle) {
throw sqle;}
int countSelected = 0;
if (countSelected == 0){
Cursor cursor = null;
try {
SQLiteDatabase db = dbHelper.getReadableDatabase();
cursor = db.query(DBHelper.TABLE_TASKS, null, "id = " + mEditAtributeList.get(pos).getTask_id(), null, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String ids = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.TASK_ATRIBUTE_ID));
String[] parts = ids.split(",");
int[] ints = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
ints[i] = Integer.parseInt(parts[i]);
}
countSelected = ints.length;
}
} finally {
if (cursor != null)
cursor.close();
}
if(countSelected>=5){
if(!eavh.mCheckBox.isChecked())
eavh.mCheckBox.setEnabled(false);
else
eavh.mCheckBox.setEnabled(true);
}
else
eavh.mCheckBox.setEnabled(true);
}
//here is saving new status to db ( a lot of code)
});
return eavh;
}
#Override
public void onBindViewHolder(EditAtributesViewHolder holder, int position) {
AtributeEditItem currentItem = mEditAtributeList.get(position);
holder.mImageView.setImageResource(currentItem.getImage());
holder.mCheckBox.setText(currentItem.getName());
holder.mCheckBox.setChecked(currentItem.isSelected());
int countSelected = 0;
for (int i = 0 ; i < mEditAtributeList.size() ; i++){
if(mEditAtributeList.get(i).isSelected())
countSelected++;
}
if(countSelected>=5){
if(!holder.mCheckBox.isChecked())
holder.mCheckBox.setEnabled(false);
else
holder.mCheckBox.setEnabled(true);
}
else
holder.mCheckBox.setEnabled(true);
}
#Override
public int getItemCount() {
return mEditAtributeList.size();
}
}
Your adapter and viewholder are actually doing too much, but for a small list you can probably get away with it. Calling the sqldatabse like this onclick can be a bit awkward, i am not sure, but i think you are blocking the main thread with this which means you may notice that onclick slightly freezes your app.
But the simplest way disable the items in your recyclerview is to update the items in your
private ArrayList<AtributeEditItem> mEditAtributeList
and call
notifyDataSetChanged()
on the adapter, as you can then derive the state based on the data

Best solution for Checkbox in LinearLayout

In my LinearLayout, there's a variable number of CheckBoxes. In a question I had a month ago someone said it´s better to add checkboxes dynamicly instead of make them not visible.
Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10};
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {
cBox = new CheckBox(this);
cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
cBox.setId(count[i]);
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
antwortencode[position] += "" + buttonView.getId();
frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
} else {
String id = Integer.toString(buttonView.getId());
antwortencode[position] = antwortencode[position].replaceAll(id,"");
if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
} else {
frageBeantworten.setText("Keine Checkbox(en) gewählt");
}
}
}
});
antworten.addView(cBox);
Currently, I'm able to save a string with the checked checkboxes, if I un-check a checkbox, it deletes it's value out of the string.
If I update the activity, the string is saved, and the checkboxes get a new List from the mFrageList.get(position)getAuswahlList(); and fill a new string in the "antwortencode" List with their values.
If I go back to the last position, I have the string which was generated but the checkboxes aren't checked anymore. But they have the Strings from the old position. that means everything is saved except the state of the checkboxes. I cant set a cBox.setChecked(isChecked) or buttonView.setChecked(isChecked) or buttonView.setChecked(buttonView.isChecked()) or something which is nearly the same in syntax.
I don't know what I can do besides declaring 10 Checkboxes in a xml file to talk to them one by one and set the VISIBLE.false if the auswahlList.get(position).isEmpty().
IMPORTANT: My XML is a Scrollable Activity because the size of the content overextended the screen. Thats why i didn´t and can´t use a Listview. So i need a solution that uses a LinearLayout
The truth is, you should actually use a ListView. As long as you reuse a layout multiple times - do it.
There are 2 options:
ListView as root - add other contents of your layout as different types of view
ListView inside a scrollable layout - there are many lightweight implementations of ListView that allow it to wrap content, e.g. https://github.com/paolorotolo/ExpandableHeightListView
The other thing is how to maintain the state of Checkboxes - use model classes. It's extremely easy with a ListView as it forces you to use an Adapter which provides methods to iterate over all positions.
Example of an adapter:
public class CheckableItemAdapter extends BaseAdapter {
private List<Pair<Integer, Boolean>> items = new ArrayList<>();
public void setItems(List<Pair<Integer, Boolean>> items) {
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checkable, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Pair<Integer, Boolean> item = items.get(position);
holder.itemCheck.setChecked(item.second);
return view;
}
static class ViewHolder {
CheckBox itemCheck;
public ViewHolder(View itemView) {
itemCheck = (CheckBox) itemView.findViewById(R.id.check);
}
}
}
I´ve managed to solve my problem alone, and now i want to share it, even if it isn´t the best example of programming.
Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10}; //maximum of 10 Checkboxes
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {
cBox = new CheckBox(this);
cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
cBox.setId(count[i]);
try{ //this is where the magic happens
if(antwortencode[position] != ""){ //cause i won´t want null in my db i´ve set "" as standard string in my activity for the List<String>
String code = antwortencode[position];
char[] c = code.toCharArray();
for(int j=0;j<=c.length;j++){
int x = c[j] -'0'; // 'char 1' - 'char 0' = Integer 1 , lol
if(cBox.getId()== x){ //compare them
cBox.toggle(); //if it fits, toggle
}
}
}
} catch (Exception e){
e.printStackTrace();
} //and here it ends
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
antwortencode[position] += "" + buttonView.getId();
frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
} else {
String id = Integer.toString(buttonView.getId());
antwortencode[position] = antwortencode[position].replaceAll(id,"");
if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
} else {
frageBeantworten.setText("Keine Checkbox(en) gewählt");
}
}
}
});
antworten.addView(cBox);
Ty for the answers and for the correction of my question.
Nostramärus

Spannable string returning whole string on click

Using Button on a row in the ListView and am able to make a single row Spannable and data from database but when i click it is returning whole string than a word here is my code from MyAdapter Class where am entering values from database to Listview
public View getView(int i, View view, ViewGroup viewGroup) {
Realm realm=Realm.getDefaultInstance();
Word toEdit = realm.where(Word.class)
.equalTo("id", 10).findFirst();
int id_to_seperate=toEdit.getLang();
LayoutInflater inflater= (LayoutInflater) Main.context.getSystemService(Main.context.LAYOUT_INFLATER_SERVICE);
View row= inflater.inflate(R.layout.layout,viewGroup,false);
TextView word= (TextView) row.findViewById(R.id.word_name);
TextView meaning= (TextView) row.findViewById(R.id.word_define);
Word temp=list.get(i);
int idz=temp.getId();
word.setText(temp.getWord());
if(id_to_seperate==idz){
String span[] = temp.getMeaning().substring(1).split(" ") ;
SpannableString ss = new SpannableString(temp.getMeaning().substring(1));
ClickableSpan spans[] = new ClickableSpan[span.length];
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
#Override
public void onClick(View textView) {
TextView v = (TextView)textView ;
String text = v.getText().toString() ;
Log.d("View" , text);
}
};
}
int start = 0 ;
int end =0;
try {
for(int spanCount = 0 ; spanCount <span.length ; spanCount++){
if(spanCount==0) {
end += span[spanCount].length();
}else{
end += span[spanCount].length()+1;
}
ss.setSpan(spans[spanCount], start, end , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += span[spanCount].length()+1;
}
} catch (Exception e) {
e.printStackTrace();
}
meaning.setText(ss);
meaning.setMovementMethod(LinkMovementMethod.getInstance());
}else {
meaning.setText(temp.getMeaning().substring(1));
}
return row;
}
am able to get result from a specific row in a list
to this But click Functionality returning whole sentense than a span or word, I would be thankful if anyone can tell me what mistake I have done
Regards
here is wrong in your code
TextView v = (TextView)textView ;
String text = v.getText().toString() ;
TextView.getText().toString() is return full text. if you want to get specific ClickableSpan then use below ClickableSpan
public class SpecialClickableSpan extends ClickableSpan {
String text;
public SpecialClickableSpan(String text){
super();
this.text = text;
}
#Override
public void onClick(View widget) {
Log.d(TAG, "onClick [" + text + "]");
}
}
replace
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
#Override
public void onClick(View textView) {
TextView v = (TextView)textView ;
String text = v.getText().toString() ;
Log.d("View" , text);
}
};
}
with
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new SpecialClickableSpan (span[spanCount ]);
}

Common TextWatcher Saving Values in JSON Improperly (Replacing old values)

I have created Adapter to store Cart Items with there dynamic quantities.
Adding custom views which Contains Size, No. of Print, Price. In that I have to take No. of Print in EditText from User Input.
onBindViewHolder() of Adapter: In that I have added common TextWatcher for every EditText which is separated by ID passed in Constructor.
/*
* Add Custom View For Size and Price
* */
if (cart.getCartPhotoPrintSize() != null) {
try {
data = cart.getCartPhotoPrintSize().get(position);
if (data != null) {
// Remove the custom view
holder.cartPhotoSizrPriceCustome.removeAllViews();
holder.cartPhotoSizrPriceCustome.setVisibility(View.VISIBLE);
// Local Variable for Storing the size of StudioData Size
int size = cart.getCartPhotoPrintSize().size();
// Loop for set the custom layout
for (int i = 0; i < size; i++) {
// inner Loacl Variable
Studio.StudioSize studioSize = cart.getCartPhotoPrintSize().get(i);
// Layout Inflater For Targeting The RootView Of Context
LayoutInflater inflater = LayoutInflater.from(mContext);
View sizePriceView = inflater.inflate(R.layout.size_price_quantity_layout, null, false);
// Binding the id of TextView
studioSizeTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_size);
studioPriceTextView = (TextView) sizePriceView.findViewById(R.id.studio_photo_album_price);
photoQuantity = (EditText) sizePriceView.findViewById(R.id.photo_quantity);
studioSizeTextView.setId(i);
studioSizeTextView.setText(studioSize.getSize());
studioPriceTextView.setText("" + studioSize.getPrice());
holder.cartPhotoSizrPriceCustome.setTag(cart.getId());
holder.cartPhotoSizrPriceCustome.addView(sizePriceView);
//Add TextWacher For Every EditTextView
photoQuantity.addTextChangedListener(new GeneralTextWatcher(holder, i));
}
}
} catch (Exception e) {
Log.w("TAG", e.getMessage());
}
}
GeneralTextWatcher
private class GeneralTextWatcher implements TextWatcher {
ViewHolder viewHolder;
int id;
public GeneralTextWatcher(ViewHolder viewHolder, int i) {
this.viewHolder = viewHolder;
this.id = i;
Log.e(TAG, "Constructor Calling..");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
totalAmount = 0;
int child = viewHolder.cartPhotoSizrPriceCustome.getChildCount();
int pos = viewHolder.getAdapterPosition();
Log.i("size position : ", "" + id);
Log.i("cart item position : ", "" + pos);
Cart tCart = tempValue.get(pos);
Studio.StudioSize ssTemp = tCart.getCartPhotoPrintSize().get(id);
ssTemp.setQuantity(!TextUtils.isEmpty(s) ? Integer.parseInt(s.toString()) : 0);
for (int j = 0; j < child; j++) {
View view = viewHolder.cartPhotoSizrPriceCustome.getChildAt(j);
EditText editText = (EditText) view.findViewById(R.id.photo_quantity);
TextView textView = (TextView) view.findViewById(R.id.studio_photo_album_price);
Double quantity = 0.0;
if (!editText.getText().toString().equals(""))
quantity = Double.parseDouble(editText.getText().toString());
Double price = Double.parseDouble(textView.getText().toString());
if (printAmount != 0)
tempTotal = printAmount;
printAmount = quantity * price;
totalAmount += printAmount;
}
tempValue.get(pos).setTotal(totalAmount);
((CartActivity) mContext).mValues = tempValue;
Log.e("Tag", "After tmpStr " + new Gson().toJson(
tempValue,
new TypeToken<ArrayList<Cart>>() {
}.getType()));
((CartActivity) mContext).findTotalAmount(tempValue);
}
#Override
public void afterTextChanged(Editable s) {
}
}
When I Change First Cart Item with all three EditText values and Saving new JSON is like Click to see JSON after 3rd EditText filled up Its Working Fine,
But
After changing Second Cart Item fourth EditText Value and Saving JSON like as Click to see after changing 4th EditText in Second Cart Its replacing old Values.
I am getting this problem since last two days. Any help would be appreciated.
Thank you.

ListView with Custom Layout including two RadioButtons

I am tryin to make a quiz app with translations. So there are two ArrayLists with Question Loaded in differnt languages.
I can show them in the List View as well. My problem is when I click on One Radiobutton I want the other Radiobutton to get selected as well.
I am using a ListView with a custom layout with 2 Radiobuttons .
This is my ArrayAdater for ListView
private class RBAdapterClass extends ArrayAdapter<cOptions> {
private ArrayList<cOptions> fOptions;
private ArrayList<cOptions> lOptions;
private ArrayList<Boolean> mChecked;
private int mfSelectedPosition = -1;
private RadioButton mfSelectedRB;
private int mlSelectedPosition = -1;
private RadioButton mlSelectedRB;
public RBAdapterClass(Context context, int textViewResourceId, ArrayList<cOptions> lOpt, ArrayList<cOptions> fOpt)
{
super(context,textViewResourceId,fOpt);
this.fOptions = new ArrayList<cOptions>();
this.fOptions.addAll(fOpt);
this.lOptions = new ArrayList<cOptions>();
this.lOptions.addAll(lOpt);
mChecked = new ArrayList<Boolean>();
}
class ViewHolder {
TextView ForeignCode;
RadioButton ForeignName;
TextView LocalCode;
RadioButton LocalName;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holderf ;
Log.v("Convert View",String.valueOf(position));
if(convertView==null){
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.activity_activity_rb,null);
holderf=new ViewHolder();
holderf.LocalCode = (TextView) convertView.findViewById(R.id.codeLocal);
holderf.LocalName = (RadioButton)convertView.findViewById(R.id.radioBLocal);
holderf.ForeignCode = (TextView) convertView.findViewById(R.id.codeForeign);
holderf.ForeignName = (RadioButton)convertView.findViewById(R.id.radioBForeign);
convertView.setTag(holderf);
holderf.ForeignName.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
RadioButton rb = (RadioButton) v ;
if(position != mfSelectedPosition && mfSelectedRB!=null )
{
mfSelectedRB.setChecked(false);
if(mlSelectedRB!=null)
{
mlSelectedRB.setChecked(false);
}
}
mfSelectedPosition = position;
mfSelectedRB = (RadioButton)v;
cOptions optSelected = (cOptions) rb.getTag();
Toast.makeText(getApplicationContext(),"Clicked on RadioButton: " + rb.getText() +" is " + rb.isChecked(),
Toast.LENGTH_LONG).show();
optSelected.setSeleted(rb.isChecked());
holderf.ForeignName.setChecked(rb.isChecked());
holderf.LocalName.setChecked(rb.isChecked());
}
});
holderf.LocalName.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
if(position != mlSelectedPosition && mlSelectedRB!=null)
{
mlSelectedRB.setChecked(false);
if(mfSelectedRB!=null)
{
mfSelectedRB.setChecked(false);
}
}
mlSelectedPosition = position;
mlSelectedRB = (RadioButton)v;
RadioButton rb = (RadioButton) v ;
cOptions optSelected = (cOptions) rb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on RadioButton: " + rb.getText() +" is " + rb.isChecked(),Toast.LENGTH_LONG).show();
optSelected.setSeleted(rb.isChecked());
holderf.ForeignName.setChecked(rb.isChecked());
holderf.LocalName.setChecked(rb.isChecked());
}
});
}
else
{
holderf = (ViewHolder) convertView.getTag();
}
cOptions optfSelected = fOptions.get(position);
cOptions optlSelected = lOptions.get(position);
if(mlSelectedPosition!=position)
{
holderf.ForeignCode.setText(" (" + optfSelected.getsOptionsText() + ")");
holderf.ForeignName.setText(optfSelected.getsOptionsText());
holderf.ForeignName.setChecked(optfSelected.getSeleted());
holderf.ForeignName.setTag(optfSelected);
holderf.LocalCode.setText("("+optlSelected.getsOptionsText()+")");
holderf.LocalName.setText(optlSelected.getsOptionsText());
holderf.LocalName.setChecked(optlSelected.getSeleted());
holderf.LocalName.setTag(optlSelected);
}
else
{
holderf.ForeignCode.setText(" (" + optfSelected.getsOptionsText() + ")");
holderf.ForeignName.setText(optfSelected.getsOptionsText());
holderf.ForeignName.setChecked(optfSelected.getSeleted());
holderf.ForeignName.setTag(optfSelected);
holderf.LocalCode.setText("("+optlSelected.getsOptionsText()+")");
holderf.LocalName.setText(optlSelected.getsOptionsText());
holderf.LocalName.setChecked(optlSelected.getSeleted());
holderf.LocalName.setTag(optlSelected);
}
return convertView;
}
}
}
There is no check box in your code. Think it will be Radio button instead. I can't understand why do you want both radio buttons to be checked. Anyway, if you are setting those separately, (Not inside a RadioGroup), then you can make both checked at a time.
One suggession: write your onClick listeners outside the if-else block

Categories

Resources