2016-09-23 13 views
1

私はcutomized listViewを作成しています。 listViewの各項目のモデルは、「model_view」レイアウトに示されているとおりです。 以下の「アダプタの設定」に示すように、データをアダプタに設定します。 :「CutomizedAdapter」に示すように cutomizedアダプタは、私がアプリケーションを実行するとリストビューの項目がリストビューのアイテムが表示されません

model_viewを示していない理由を、画面上

出現アイテムがない

です

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/tvName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight=".3" 
    android:text="Name"/> 

<TextView 
    android:id="@+id/tvAddress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight=".3" 
    android:text="Address: "/> 

<TextView 
    android:id="@+id/tvGender" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight=".3" 
    android:text="gender: "/> 
</LinearLayout> 

セットアダプタ

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    this.mListView = (ListView) findViewById(R.id.listview); 
    this.mModelList = new ArrayList<RowModel>(); 

    this.mModelList.add(new RowModel("xx", "Mümchen", "M")); 
    this.mModelList.add(new RowModel("xx", "Karlsruhe", "M")); 
    this.mModelList.add(new RowModel("xx", "Mümchen", "F")); 
    this.mModelList.add(new RowModel("xx", "Karlsruhe", "F")); 
    this.mModelList.add(new RowModel("xx", "Alexandria", "M")); 
    this.mModelList.add(new RowModel("xx", "Cairo", "F")); 

    CustomizedAdapter adapter = new CustomizedAdapter(this, R.layout.model_view, this.mModelList); 
    this.mListView.setAdapter(adapter); 

CutomizedAdapter:あなたは方法の下に充填されていないのはなぜ

public class CustomizedAdapter extends BaseAdapter { 

private Context mCxt = null; 
private int mRowModelLayout; 
private ArrayList<RowModel> mRowsList = null; 
private static LayoutInflater inflater=null; 

public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) { 
    this.mCxt = cxt; 
    this.mRowModelLayout = rowModelLayout; 
    this.mRowsList = rowsList; 
} 

@Override 
public int getCount() { 
    return 0; 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

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

public static class ViewHolder{ 
    public TextView tvName; 
    public TextView tvAddress; 
    public TextView tvGender; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    final int pos = position; 
    ViewHolder holder = new ViewHolder(); 
    View rowView; 

    convertView = inflater.inflate(R.layout.model_view, null); 
    holder.tvName=(TextView) convertView.findViewById(R.id.tvName); 
    holder.tvAddress=(TextView) convertView.findViewById(R.id.tvAddress); 
    holder.tvGender=(TextView) convertView.findViewById(R.id.tvGender); 

    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(mCxt, "You Clicked "+mRowsList.get(pos).getName(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
    return convertView; 
} 
} 

答えて

5

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

@Override 
public Object getItem(int position) { 
    return mRowsList.get(position); 
} 

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

はまた、あなたがこれらの方法で間違ったことを返すので

public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) { 
    this.mCxt = cxt; 
    this.mRowModelLayout = rowModelLayout; 
    this.mRowsList = rowsList; 
    inflater = (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
+0

「インフレータ」が初期化されていないため、今のAppがクラッシュした...私は次のようにそれを初期化しようとしています:インフレータ=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICEを)。しかし、 "getSystemService"は認識されません – user2121

+0

あなたのコンストラクタでは、これは 'inflater =(LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);' –

+0

のように利用できるはずです。 –

1

ようなあなたのコンストラクタでそれを初期化し、だからあなたのinflater

を初期化されていません。

@Override 
public int getCount() { 
    return 0; 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

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

これらの関数は何も返さず、次のように変更します。

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

@Override 
public Object getItem(int position) { 
    return mRowList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; //or mRowList.get(position).getID() 
} 
+0

'position.getID()'とは何ですか? –

+0

"インフレータ"が初期化されていないため、アプリケーションがクラッシュする...私は次のように初期化しようとしています:inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 「getSystemService」は認識できません – user2121

関連する問題