我是Android的新手,正尝试学习各种技术,包括使用“编辑文本”选项的搜索栏。
我目前正在使用JSON解析Twitter,并将其存储在列表活动中。
同样,使用EditText选项实现搜索栏,以便当用户输入任何内容时都会收到一个新的数组列表,但是需要以某种方式将其与TextWatcher和ListActivity集成在一起。
收到的实际错误:创建TextWatcher的构造方法。
TwitterFeedAdapter具有作为公共TwitterFeedAdapter(活动a,int textViewResourceId,ArrayList tweets)的构造函数,但是当我返回搜索值时,我将TwitterFeedAdapter用作ArrayList,但它需要实现TextWatcher。
任何有关如何恢复工作方式的想法或任何更好的帮助。
/ ** TwitterFeedActivity ** /
public class TwitterFeedActivity extends Activity implements TextWatcher{
public Bitmap placeholder;
private EditText et;
private ListView listView;
private ArrayList<Tweet> tweets;
private ArrayList<Tweet> array_sort= new ArrayList<Tweet>();
int textLength = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tweets = getTweets("android", 1);
listView = (ListView) findViewById(R.id.ListViewId);
et = (EditText)findViewById(R.id.EditText01);
listView.setAdapter(new TwitterFeedAdapter(this, R.layout.list_item, tweets));
et.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
textLength = et.getText().length();
array_sort.clear();
for (int i = 0; i < tweets.size(); i++)
{
if (textLength <= tweets.size())
{
if(et.getText().toString().equalsIgnoreCase((String)((CharSequence) tweets.get(i)).subSequence(0,textLength)))
{
array_sort.add(tweets.get(i));
}
}
}
/***** Error is here since TwitterFeedAdapter has Activity implemented for it *******/
listView.setAdapter(new TwitterFeedAdapter(this, R.layout.list_item array_sort));
}
});
}
public ArrayList<Tweet> getTweets(String searchTerm, int page) {
String searchUrl = "http://search.twitter.com/search.json?q=@"
+ searchTerm + "&rpp=100&page=" + page;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
}catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
JSONArray arr = null;
try {
Object j = jsonObject.get("results");
arr = (JSONArray)j;
System.out.println(arr + "Array");
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
for(Object t : arr) {
Tweet tweet = new Tweet(
((JSONObject)t).get("from_user").toString(),
((JSONObject)t).get("text").toString(),
((JSONObject)t).get("profile_image_url").toString()
);
tweets.add(tweet);
}
return tweets;
}
/** Classes **/
public class Tweet {
public String username;
public String message;
public String image_url;
public Boolean usernameSet = false;
public Boolean messageSet = false;
public Boolean imageSet = false;
public Bitmap avatar;
public Tweet(String username, String message, String url) {
this.username = username;
this.message = message;
this.image_url = url;
}
}
}
/**TwitterFeedAdapter.java****/
public class TwitterFeedAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
private Activity activity;
public ImageManager imageManager;
public TwitterFeedAdapter(Activity a, int textViewResourceId, ArrayList<Tweet> tweets) {
super(a, textViewResourceId, tweets);
this.tweets = tweets;
activity = a;
imageManager = new ImageManager(activity.getApplicationContext());
}
public static class ViewHolder{
public TextView username;
public TextView message;
public ImageView image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.message = (TextView) v.findViewById(R.id.message);
holder.image = (ImageView) v.findViewById(R.id.avatar);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Tweet tweet = tweets.get(position);
if (tweet != null) {
holder.username.setText(tweet.username);
holder.message.setText(tweet.message);
holder.image.setTag(tweet.image_url);
imageManager.displayImage(tweet.image_url, activity, holder.image);
}
return v;
}
}
最佳答案
在设置适配器的地方,尝试按以下方式将this
更改为TwitterFeedActivity.this
...
listView.setAdapter(new TwitterFeedAdapter(TwitterFeedActivity.this, R.layout.list_item, array_sort));
编辑:此外,您的代码缺少
R.layout.list_item
和array_sort
之间的逗号。我已将其添加到上面的示例中。关于android - 如何将TextWatcher添加到Android中的Activity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8782701/