-1
這是我的活動:ArrayList中是零的時候setAdapter
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列表中選擇適配器。我使用的調試,以確保該getTweet方法返回足夠的對象 鳴叫= {ArrayList的@ 5524}大小= 25
0 = {[email protected]}
1 = {[email protected]}
2 = {[email protected]}
3 = {[email protected]}
4 = {[email protected]}
5 = {[email protected]}
6 = {[email protected]}
7 = {[email protected]}
8 = {[email protected]}
9 = {[email protected]}
10 = {[email protected]}
11 = {[email protected]}
然而,當我嘗試設置適配器,沒有顯示的數據和應用程序立即停止。奇怪的是,Logcat是空白的,沒有任何信息。它只是黑色。與Twitter的身份驗證工作。 任何人都可以幫助我。