2015-09-28 21 views
-6

我在TweetAdapter.java中創建了一個自定義適配器。我寫的代碼爲如何使用列表<data>並將它傳遞給另一個類

公共類TweetAdapter擴展ArrayAdapter {

private List<Tweet> tweets; 
private Context context; 
public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) { 
    super(ctx, R.layout.row_tweet, tweets); 
} 

@Override 
public Tweet getItem(int arg0) { 
    return tweets.get(arg0); 
} 

@Override 
public long getItemId(int arg0) { 
    return arg0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View view = inflater.inflate(R.layout.row_tweet, parent); 
    } 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
    TextView tweetTitle = (TextView) v.findViewById(R.id.tweetTitle); 
    TextView tweetBody = (TextView) v.findViewById(R.id.tweetBody); 
    final Tweet currentTweet = tweets.get(position); 
    String a = currentTweet.getTitle(); 
    String b = currentTweet.getBody(); 
    tweetTitle.setText(a); 
    tweetBody.setText(b); 
    return v; 
} 

}

,但得到的錯誤

顯示java.lang.NullPointerException:試圖調用虛擬方法的java.lang .Object android.content.Context.getSystemService(java.lang.String)'null對象引用

at android.view.LayoutInflater.fr om(LayoutInflater.java:219)

+1

我需要錢,很多很多錢..! –

+1

你能詳細闡述一下你的問題和你的問題嗎? –

+0

這不是解決方案服務。即使您試圖自己編寫代碼,我們也不會爲您編寫代碼。另一方面,如果您有關於「您的」代碼的問題,我們將嘗試回答。但你必須先問它。 –

回答

0

解決

Implemented

import java.io.Serializable;

in Tweet.java

0

看起來像你的context變量爲空,因爲它從未被設置。

嘗試修改您的構造函數:

public TweetAdapter(TweetListActivity ctx, List<Tweet> tweets) { 
    super(ctx, R.layout.row_tweet, tweets); 
    context=ctx; 
} 
+0

我改變了代碼,按您的說法 但現在是給這個錯誤 java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)沒有在適配器視圖 在 支持,如果(V = = null){ LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.row_tweet,parent); } –

+0

我回答了您的原始問題。您在最新評論中添加的代碼錯誤是指不在原始問題中的代碼(上面的代碼中沒有addView命令),並且可能不相關。如果您有新問題,請打開一個新問題並在其中包含相關代碼。 – Kuffs

-1

檢查這個鏈接可以幫助你:

https://github.com/codelearn-org/CodelearnTwitterApp/blob/master/src/org/codelearn/twitter/TweetAdapter.java

這裏是relelvant部分:

public class TweetAdapter extends ArrayAdapter<Tweet> { 

    private LayoutInflater inflater; 
    private List<Tweet> tweetsLocal; 

    public TweetAdapter(Activity activity, List<Tweet> tweets){ 
     super(activity, R.layout.row_tweet, tweets); 
     inflater = activity.getWindow().getLayoutInflater(); 
     tweetsLocal = tweets; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 
     View row = inflater.inflate(R.layout.row_tweet, parent, false); 
     TextView title = (TextView) row.findViewById(R.id.tweetTitle); 
     Tweet tweet = tweetsLocal.get(position); 
     title.setText(tweet.getTitle()); 
     TextView body = (TextView) row.findViewById(R.id.textView2); 
     body.setText(tweet.getBody()); 
     return row; 
    } 

} 
相關問題