我有一个带有lazyadapter的自定义列表视图。我正在尝试使列表视图上的文本视图的可点击部分打开一个新活动。但我被困住了。
这是在 LazyAdapter 中(我想说这不是一个 Activity。它只是 LazyAdapter 类):
private Activity activity;
private String[] urls;
private String[] tweets;
private String[] names;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] u, String[] n, String[] t) {
activity = a;
urls = u;
names = n;
tweets = t;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return urls.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_item, null);
TextView user = (TextView) vi.findViewById(R.id.textUser);
TextView tweet = (TextView) vi.findViewById(R.id.textTweet);
ImageView image = (ImageView) vi.findViewById(R.id.imageView);
user.setText(names[position]);
String[] checkHashtag= tweets[position].split(" ");
SpannableStringBuilder sb = new SpannableStringBuilder();
for(final String item: checkHashtag){
if(item.substring(0,1).matches("#")){
sb.append(item+" ");
sb.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
Intent intent = new Intent(null, TweetActivity.class);
intent.putExtra("item", item);
Intent.startActivity(intent);
}
}, sb.length()-item.length(), sb.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else sb.append(item+" ");
}
tweet.setText(sb);
if (tweets[position].toLowerCase().contains("http://")
|| tweets[position].toLowerCase().contains("https://")) {
String[] parts = tweets[position].split(" ");
for (String item : parts) {
URL url;
try {
url = new URL(item);
Pattern pattern = Pattern.compile(url.toString());
Linkify.addLinks(tweet, pattern, "http://");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
imageLoader.DisplayImage(urls[position], image);
return vi;
}
}
这就是我在 Tweet Activity 中调用 LazyAdapter 的方式:
LazyAdapter adapter = new LazyAdapter(this, pic_urls, user_names, tweets);
setListAdapter(adapter);
但它不起作用。startActivity(intent) 部分存在一些问题。