2016-09-24 5 views
0

私のコードはエラーなしでコンパイルされますが、getCount()が返すもの(6項目)のリスト項目は表示されません。カスタムアダプターとカスタムレイアウトを使用します。私を助けてください。ここで私のリストビューは画面に表示されませんが、すべてが問題ありません。

は私のコードです:

XMLリスト:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="bbeniful.com.listhandle.MainActivity"> 
<ListView 
    android:id="@+id/myListView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/>  
</LinearLayout> 

カスタムリストビューのxml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:weightSum="1"> 
<ImageView 
    android:id="@+id/myitemlistPicture" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:layout_weight="0.37" /> 

<TextView 
    android:id="@+id/myItemlisttext" 
    android:layout_width="150dp" 
    android:layout_height="153dp" 
    android:gravity="center" 
    android:textSize="25sp" 
    android:layout_weight="0.64" /> 

</LinearLayout> 

と、これは私のカスタムアダプタです:

public class MyAdapter extends BaseAdapter { 
Context context; 


String[] names; 
Integer[] imgId; 




public MyAdapter(Context context, String[] names,Integer[] imgId) { 
    this.context = context; 
    this.names = names; 
    this.imgId = imgId; 
} 

@Override 
public int getCount() { 
    return names.length; 
} 

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

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

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

    if (convertView == null) { 
     LayoutInflater inflater = 
    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.list_item, null); 

     Holder holder = new Holder(); 
     holder.txtviewfirstname = (TextView) 
     convertView.findViewById(R.id.myItemlisttext); 
     holder.image = (ImageView) 
     convertView.findViewById(R.id.myitemlistPicture); 
    } 

    return convertView; 
    } 

    } 

    final class Holder{ 
    TextView txtviewfirstname; 
    ImageView image; 
    } 

と最後の主な活動:

public class MainActivity extends Activity { 
ListView myListview; 
Activity context = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String[] name = { 

      "asd", 
      "asd", 
      "asd", 
      "asd", 
      "asd", 
      "asd", 
      "asd" 


    }; 
    Integer[] imgId = { 

      R.drawable.icon, 
      R.drawable.icon, 
      R.drawable.icon, 
      R.drawable.icon, 
      R.drawable.icon, 
      R.drawable.icon, 
      R.drawable.icon 
    }; 


    myListview = (ListView) findViewById(R.id.myListView);; 
    context = this; 
    myListview.setAdapter(new MyAdapter(this,name,imgId)); 

} 
} 
+1

を試してみてください、私はあなたのアダプタのコードを変更し、この2行

ListView

に値を設定します

holder.txtviewfirstname.setText(names[position]); holder.image.setImageResource(imgId[position]); 

を逃しました。ヒント:アダプタで 'names'または' imgId'変数を使用していません – Shaishav

+0

'getView()'メソッドで 'txtviewfirstname'と' image'の両方に何も設定しないので、 – Onik

+0

ええ、ありがとうそれはどうしたらいいですか? – Bbeni

答えて

0

は、基本的には、あなたは、あなたのアイテムを割り当てていないこの

public class MyAdapter extends BaseAdapter { 
    Context context; 
    String[] names; 
    Integer[] imgId; 

public MyAdapter(Context context, String[] names,Integer[] imgId) { 
this.context = context; 
this.names = names; 
this.imgId = imgId; 
    } 

    @Override 
    public int getCount() { 
     return names.length; 
    } 

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

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

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

    if (convertView == null) { 
     LayoutInflater inflater = 
    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.list_item, null); 

    Holder holder = new Holder(); 
    holder.txtviewfirstname = (TextView) 
    convertView.findViewById(R.id.myItemlisttext); 
    holder.image = (ImageView) 
    convertView.findViewById(R.id.myitemlistPicture); 
} 
    holder.txtviewfirstname.setText(names[position]); 
    holder.image.setImageResource(imgId[position]); 
return convertView; 
} 

} 

    final class Holder{ 
    TextView txtviewfirstname; 
    ImageView image; 
    } 
関連する問題