2016-10-13 4 views
-1

私はこの問題に混乱しています!アダプターにnullテキストが設定されているのはなぜですか?

アダプタでテキストをTextViewに設定しましたが、リストビューが生成されたときにTextViewがまだnullです。

私の問題は約MyAdapterです。私がテキストを手動で入力すると、それは完全に機能するからです。

私を助けてください。

これは私の "アダプタ" です:

package ir.sarashpazp.peymanehsarashpaz; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.List; 

public class MainAdapter extends BaseAdapter { 
private Activity activity; 
private LayoutInflater inflater; 
private List<items> feedItems; 
private items item; 


public MainAdapter(Activity activity, List<items> feedItems) { 
    this.activity = activity; 
    this.feedItems = feedItems; 
} 

@Override 
public int getCount() { 
    return feedItems.size(); 
} 

@Override 
public Object getItem(int location) { 
    return feedItems.get(location); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    item = new items(); 
    if (inflater == null) 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) 
     convertView = inflater.inflate(R.layout.eachdastoor, null); 


    TextView name = (TextView) convertView.findViewById(R.id.name); 

    name.setText(item.getName()); 

    return convertView; 
} 

} 

これは私の "項目" クラスです:

public class items { 
private int id; 
private String name, status, image, profilePic, timeStamp, url; 

public items() { 
} 

public items(int id, String name, String image, String status, 
       String profilePic, String timeStamp, String url) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.image = image; 
    this.status = status; 
    this.profilePic = profilePic; 
    this.timeStamp = timeStamp; 
    this.url = url; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 


} 
+0

item = new items(); 

を交換してください();'ラインを。これは、アダプタ内のオブジェクトごとに空のオブジェクトを作成します。それを 'item = feedItems.get(position);に変更してください。 –

+0

あなたの答えに感謝します。何がfeedPositionsですか? –

+0

この 'item = new items();はどういうのですか? – njzk2

答えて

0

あなたはいつもの代わりに、項目の新しいオブジェクトのインスタンスにアクセスしていますあなたは配列します。 `項目=新しいアイテムを削除してください

item = feedItems.get(position); 
+0

あなたの答えをありがとう、feedPositionsとは何ですか? –

+0

@GamesroomIran私は答えを編集しました。それは 'feedItems'でなければなりません。すべてのアイテムオブジェクトをアダプタに格納する配列 –

+0

aha、それは動作しますが、各アイテムのすべてのテキストは" Item 5 "です! –

関連する問題