0

My ListView implements the ContextMenu when each row is LongPressed.

I decided to include more static data separated by commas along with each string taken from the ArrayList to populate the contextMenu - and it works fine.

Old ListView, New ListView

My question is how do I correctly use substring INSIDE my Custom Adapter so only the first value (before the first comma) appears in the Listview?

My attempt at using .split on p.getName() using something like below met with little success.

String[] temp;
for (String s: selectedItems) {
   temp = s.split(" ");

Here is my getViews code:

public View getView(final int position, View convertView, ViewGroup parent) {

    View v = convertView;
    NameHolder holder = new NameHolder();
        // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.imgbtn_row_layout, null);
        // Now we can fill the layout with the right values
        TextView tv = (TextView) v.findViewById(R.id.name);
        TextView distView = (TextView) v.findViewById(R.id.dist);
        ImageButton btnFave = (ImageButton) v.findViewById(R.id.btnone);
        ImageButton btnBuy = (ImageButton) v.findViewById(R.id.btntwo);

        holder.nameView = tv;
        holder.distView = distView;
        holder.btnFave = btnFave;
        holder.btnBuy = btnBuy;

        btnFave.setTag(position);
        btnBuy.setTag(position);

        v.setTag(holder);
    } else {
        holder = (NameHolder) v.getTag();
    }
    final Name p = namesList.get(position);


    String grabbitall = p.getName();
    int cpos = grabbitall.indexOf(",");
    int lcpos = grabbitall.lastIndexOf(",");
    String beginName = grabbitall.substring(0, cpos);
    String endNames = grabbitall.substring(beginName.length(), lcpos-1);
    String eqiuvNames = grabbitall.substring(beginName.length(), grabbitall.length());

    Spannable namesToSpan = new SpannableString(beginName + eqiuvNames);
    namesToSpan.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), beginName.length(), ((beginName.length()+1) + endNames.length()+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    holder.nameView.setText(p.getName());
    holder.distView.setText("" + p.getDistance());

    holder.btnFave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (toggleListener != null) {
                toggleListener.onToggleClickListner(position, p.getName());
            }
        }

    });

    holder.btnBuy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (customListener != null) {
                customListener.onButtonClickListener(position, p.getName());
            }
        }
    });

    final EditText edit = (EditText) v.findViewById(R.id.txtName);
    StringBuilder sb = new StringBuilder();
    SharedPreferences shrdpref = context.getSharedPreferences("namesSpelled", 1);
    SharedPreferences.Editor editr = shrdpref.edit();
    String initialName="xxx";

    // Extract the default initial value stored in SharedPreferences
    String value = shrdpref.getString("namesSpelled", initialName);

    // Append to the extracted value
    String appendedValue =  value + sb.append(selectedName);
    String allvalues = shrdpref.getString("namesSpelled", appendedValue);

    if (allvalues.contains(p.getName())) {
        holder.btnFave.setAlpha(0.2f);
    } else {
        holder.btnFave.setAlpha(1.0f);
    }
       String[] temp;
    final Name s = namesList.get(position);

    String grabbitall = p.getName();
    int cpos = grabbitall.indexOf(",");
    int lcpos = grabbitall.lastIndexOf(",");
    String beginName = grabbitall.substring(0, cpos);
    String endNames = grabbitall.substring(beginName.length(), lcpos-1);
    String eqiuvNames = grabbitall.substring(beginName.length(), grabbitall.length());

    Spannable namesToSpan = new SpannableString(beginName + eqiuvNames);
    namesToSpan.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), beginName.length(), ((beginName.length()+1) + endNames.length()+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    notifyDataSetChanged();
    return v;
}

2 Answers2

0

You can get the index of the comma wiht indexOf:

int pos = s.indexOf(",");

And them do a substring between 0 and this pos - 1:

String name = s.substring(0, pos-1); 
Miguel Benitez
  • 2,322
  • 10
  • 22
0

I solved the prob by doing:

final Planet p = planetList.get(position);

String grabAll= p.getName();
int cpos = grabAll.indexOf(",");
int lcpos = grabAll.lastIndexOf(",");
String beginStr= grabAll.substring(0, cpos);
String endStr= grabAll.substring(beginStr.length(), lcpos-1);
String eqiuvNStr = grabAll.substring(beginStr.length(), grabAll.length());

Spannable namesToSpan = new SpannableString(beginStr+ eqiuvStr);
namesToSpan.setSpan(new ForegroundColorSpan(Color.BLUE), beginStr.length(), ((beginStr.length()+1) + endStr.length()+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

holder.nameView.setText(namesToSpan);

then I used a SPANNABLE to treat parts (the beginning and end) of each string differently. The color in ForegroundColorSpan overrides the color set in the textView.