这是我的活动:
public class HomeTimelineActivity extends AppCompatActivity {
private TwitterClient client;
Context mContext;
@BindView(R.id.rvTweets)
RecyclerView rvTweets;
List<Tweet> mTweets;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_timeline);
Butterknife.bind(this);
client = TwitterApplication.getRestClient();
populateTimeline();
}
private void populateTimeline() {
client.getHomeTimeline(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray jsonArray) {
Log.d("DEBUG", jsonArray.toString());
mTweets = new ArrayList<Tweet>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonTweet = jsonArray.getJSONObject(i);
Tweet tweet = new Tweet();
tweet.setBody(jsonTweet.getString("text"));
tweet.setUid(jsonTweet.getLong("id"));
tweet.setCreateAt(jsonTweet.getString("created_at"));
JSONObject jsonUser = jsonTweet.getJSONObject("user");
User user = new User();
user.setName(jsonUser.getString("name"));
user.setUid(jsonUser.getLong("id"));
user.setScreenname(jsonUser.getString("screen_name"));
user.setProfileImageurl(jsonUser.getString("profile_image_url"));
tweet.setUser(user);
mTweets.add(tweet);
runOnUiThread(new Runnable() {
@Override
public void run() {
TweetAdapter adapter = new TweetAdapter(mContext, mTweets);
rvTweets.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
rvTweets.setLayoutManager(layoutManager);
}
});
} catch (JSONException e) {
e.printStackTrace();
continue;
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
Log.d("DEBUT", errorResponse.toString());
}
});
}
}
这是我的适配器:
public class TweetAdapter extends RecyclerView.Adapter<TweetAdapter.TweetViewHolder>{
private Context mContext;
private List<Tweet> mTweets;
// Automatically add the Constructor here
public TweetAdapter(Context context, List<Tweet> tweets) {
mContext = context;
mTweets = tweets;
}
// Implements compulsory methods
@Override
public TweetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tweet_list_items,
parent, false);
TweetViewHolder viewHolder = new TweetViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(TweetViewHolder holder, int position) {
holder.bindModel(mTweets.get(position));
}
@Override
public int getItemCount() {
return mTweets.size();
}
public class TweetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
// Hook the ID to new created Widget using ButterKnife
@BindView(R.id.imgUserProfile)
ImageView userProfile;
@BindView(R.id.tvUserName)
TextView userName;
@BindView(R.id.tvTweet)
TextView tweetText;
TweetViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
// Set the OnclickListener of an item in list
itemView.setOnClickListener(this);
}
void bindModel(Tweet tweet) {
// Use the Widget mVar and the model name to set_get info
tweetText.setText(tweet.getBody());
}
@Override
public void onClick(View view) {
// Add What happen with OnClick
}
}
}
我尝试使用mTweets List设置适配器。我使用调试器来确保getTweet方法返回了足够的Object tweets = {ArrayList @ 5524} size = 25
0 = {Tweet@5530}
1 = {Tweet@5531}
2 = {Tweet@5532}
3 = {Tweet@5533}
4 = {Tweet@5534}
5 = {Tweet@5535}
6 = {Tweet@5536}
7 = {Tweet@5537}
8 = {Tweet@5538}
9 = {Tweet@5539}
10 = {Tweet@5540}
11 = {Tweet@5541}
但是,当我尝试设置适配器时,没有显示数据,应用程序立即停止。奇怪的是,Logcat是空白的,没有任何信息。它只是黑色。 Twitter的身份验证工作正常。 有人请帮帮我。
答案 0 :(得分:0)
您需要遵循一些步骤来提高代码性能并使其正常运行。
您缺少的主要是您在循环中的每次迭代后设置mTweets。只有在填充整个列表后,才应在适配器中设置mTweets。接下来,您应该调用适配器 notifyDataSetChanged()。
请遵循以下:
第1步
// Class variable adapter...
private TweetAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_timeline);
client = TwitterApplication.getRestClient();
// Initially pass null as the mTweets since you don't have any tweets right now...
adapter = new TweetAdapter(mContext, null);
rvTweets.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
rvTweets.setLayoutManager(layoutManager);
populateTimeline();
}
第2步 在TweetAdapter中,修改getItemCount方法以处理mTweets上的空检查。将新方法addNewTweets添加到适配器。
@Override
public int getItemCount() {
return (mTweets !=null) ? mTweets.size() : 0;
}
public void setNewTweets(List<Tweet> newTweetList) {
this.mTweets = newTweetList;
}
第3步 修改此方法populateTimeline
private void populateTimeline() {
client.getHomeTimeline(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray jsonArray) {
Log.d("DEBUG", jsonArray.toString());
mTweets = new ArrayList<Tweet>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject jsonTweet = jsonArray.getJSONObject(i);
Tweet tweet = new Tweet();
tweet.setBody(jsonTweet.getString("text"));
tweet.setUid(jsonTweet.getLong("id"));
tweet.setCreateAt(jsonTweet.getString("created_at"));
JSONObject jsonUser = jsonTweet.getJSONObject("user");
User user = new User();
user.setName(jsonUser.getString("name"));
user.setUid(jsonUser.getLong("id"));
user.setScreenname(jsonUser.getString("screen_name"));
user.setProfileImageurl(jsonUser.getString("profile_image_url"));
tweet.setUser(user);
mTweets.add(tweet);
/* REMOVE THIS CODE!
runOnUiThread(new Runnable() {
@Override
public void run() {
TweetAdapter adapter = new TweetAdapter(mContext, mTweets);
rvTweets.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
rvTweets.setLayoutManager(layoutManager);
}
});
*/
} catch (JSONException e) {
e.printStackTrace();
continue;
}
// NOW SET MTWEETS IN YOUR ADAPTER.
// NOTIFY the adapter that the data is changed.
adapter.setNewTweets(mTweets);
adapter.notifyDataSetChanged();
}
}