Array アダプターを拡張するカスタム アダプターを使用しようとしましたが、ヌル ポインター例外が発生しました。
注: XML は使用しないでください
私のmainActivityは
package com.example.newslist;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
public class MainActivity extends Activity {
DisplayMetrics dm = new DisplayMetrics();
public static int width,height;
public String text="1";
int image = R.drawable.ic_launcher;
public static ArrayList<ItemClass> listWithImage ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindowManager().getDefaultDisplay().getMetrics(dm);
width=dm.widthPixels;
height = dm.widthPixels;
listWithImage=new ArrayList<ItemClass>();
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
mainLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(mainLayout);
ListView list1 = new ListView(this);
ItemClass item = new ItemClass(text, image);
listWithImage.add(item);
list1.setAdapter(new CustomAdapter(MainActivity.this,listWithImage));
list1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, (int) (height*0.4)));
mainLayout.addView(list1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
私のカスタムアダプターは
package com.example.newslist;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<ItemClass>{
Context ctx;
static LinearLayout CustomLayout;
static TextView Name;
ImageView Image;
public CustomAdapter(Context context, List<ItemClass> objects) {
super(context, CustomLayout.getId(),Name.getId(), objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ItemClass list = this.getItem(position);
CustomLayout = new LinearLayout(ctx);
CustomLayout.setId(2);
CustomLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
CustomLayout.setOrientation(LinearLayout.VERTICAL);
Image = new ImageView(ctx);
Image.setLayoutParams(new LayoutParams((int) (MainActivity.width*0.006),(int )(MainActivity.height*0.006)));
Image.setImageResource(R.drawable.ic_launcher);
CustomLayout.addView(Image);
Name = new TextView(ctx);
Name.setId(1);
Name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Name.setPadding((int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005),(int) (MainActivity.width*0.0005),(int )(MainActivity.height*0.0005));
CustomLayout.addView(Name);
Image.setImageResource(list.getImage());
Name.setText(""+list.getName());
return CustomLayout;
}
}
そして私のアイテムクラスは
package com.example.newslist;
public class ItemClass {
public int imageId;
private String name;
public ItemClass(String text, int image)
{
this.name = text;
this.imageId = image;
}
public String getName()
{
return name;
}
public int getImage()
{
return imageId;
}
}