问题描述
我很新Android和努力学习各种技术,包括使用编辑文本选项搜索栏。我目前解析使用JSON并将其存储在列表中的活动微博。另外,实施是使用的EditText选项搜索栏,使得当用户输入接收到新arraylist.But不知它需要与TextWatcher和ListActivity得到整合任何东西。
I am very much new to android and trying to learn various techniques including Search bar using Edit Text options.I am currently parsing Twitter using JSON and storing it in a list activity.Also, implementing is the search bar using EditText option such that when a user enters anything it receives a new arraylist.But somehow it needs to get integrated with TextWatcher and ListActivity.
实际收到的错误:创建TextWatcher的构造函数。TwitterFeedAdapter具有构造公共TwitterFeedAdapter(活动一,INT textViewResourceId,ArrayList的鸣叫),但是当我得到的搜索值回我用TwitterFeedAdapter为ArrayList的,但它需要实现TextWatcher。
The actual error received : Create Constructor of TextWatcher.TwitterFeedAdapter has constructor as public TwitterFeedAdapter(Activity a, int textViewResourceId, ArrayList tweets) but when I get the search values back I use TwitterFeedAdapter as ArrayList but it needs to implement TextWatcher.
任何想法我怎么能恢复我做的方式或任何更好的帮助。
Any ideas how can I revert the way I am doing or any better help.
/ **的 TwitterFeedActivity 的** /
/**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 的**** /
/**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;
}
}
推荐答案
如果您在设置该适配器尝试改变这
到 TwitterFeedActivity.this
如下...
Where you are setting the adapter try changing this
to TwitterFeedActivity.this
as follows...
listView.setAdapter(new TwitterFeedAdapter(TwitterFeedActivity.this, R.layout.list_item, array_sort));
编辑:同时您code缺失之间的 R.layout.list_item
一个逗号和 array_sort
。我已经添加到我上面的例子。
Also your code is missing a comma between R.layout.list_item
and array_sort
. I've added it to my example above.
这篇关于如何TextWatcher添加到活动中的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!