当每行都是LongPressed时,我的ListView实现了ContextMenu。
我决定用逗号分隔更多的静态数据以及从ArrayList中取出的每个字符串来填充contextMenu - 它工作正常。
我的问题是如何在我的自定义适配器中正确使用子字符串,所以只有第一个值(在第一个逗号之前)出现在Listview中?
我尝试使用类似下面的内容在p.getName()上使用.split但收效甚微。
String[] temp;
for (String s: selectedItems) {
temp = s.split(" ");
这是我的getViews代码:
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;
}
答案 0 :(得分:0)
您可以使用indexOf获取逗号索引:
int pos = s.indexOf(",");
他们在0和这个位置之间做一个子串 - 1:
String name = s.substring(0, pos-1);
答案 1 :(得分:0)
我通过这样做解决了问题:
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);
然后我使用SPANNABLE来区别对待每个字符串的部分(开头和结尾)。 ForegroundColorSpan中的颜色会覆盖textView中的颜色集。