我正在使用arrayadapter处理自定义列表视图,该应用程序崩溃并显示以下日志
04-14 02:11:20.231 1584-1584/com.urjapawar.project E/AndroidRuntime: at com.urjapawar.project.CustomAdapter.getView(CustomAdapter.java:46)
并且第46行我有
holder.imageView.setImageResource(rowItem.getImageId());
以下是 CustomAdapter.java
的代码import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
这是 RowItem.java
public class RowItem {
private int imageId;
private String title;
private String desc;
public RowItem(int imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}
这是 list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="@string/image"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/icon"
android:paddingBottom="10sp"
android:textColor="#CC0033"
android:textSize="16sp" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_toEndOf="@+id/icon"
android:paddingStart="10sp"
android:paddingEnd="10sp"
android:textColor="#3399FF"
android:textSize="14sp" />
</RelativeLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
public static final String[] titles = new String[] { "H C Verma",
"P Bahadur", "I E Irodov", "M L Khanna",
"O P Tondon", "Morrison & Boyd", "Halliday Resnick",
"R D Sharma", "S L Loney", "Hall and Knight", "Krotov", "I A Maron" };
public static final String[] descriptions = new String[] {
"Good For clearing concepts of physics",
"Variety of examples on particular concept",
"Read to become expert in physics",
"Read to become an expert in mathematics",
"A good reference for detailed stuff",
"Read to become an expert in chemistry",
"Interesting and fundamental book of physics",
"Mandatory book forJEE aspirants",
"Another good book for high rankers",
"Simple and clear concepts of chemistry",
"favourite one for physics lovers",
"Read this for practice purposes"
};
public static final Integer[] images = { R.drawable.hcv,
R.drawable.bahadur, R.drawable.irodov, R.drawable.mlkhanna,
R.drawable.tondon, R.drawable.boyd, R.drawable.resnick, R.drawable.sharma,
R.drawable.loney, R.drawable.knight, R.drawable.krotov, R.drawable.maron};
ListView listView;
List<RowItem> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rowItems = new ArrayList<>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
还有一件事,当我复制代码并在http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/的可绘制文件夹中添加必要的文件时 代码工作正常!我所做的只是更改了对象的名称并增加了它们的数量。
答案 0 :(得分:0)
您必须覆盖CustomAdapter上的getItem()
并根据position
参数返回请求的数据
答案 1 :(得分:0)
请勿使用ArrayAdapter而是使用BaseAdapter。
以下是示例代码
public class SampleAdapter extends BaseAdapter{
private List<Data> dataList;
private Context context;
public SampleAdapter(Context context, List<Data> dataList) {
this.context=context;
this.dataList=dataList;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
private TextView tvDate;
private TextView tvMobile;
private TextView tvWifi;
private TextView tvTotal;
private ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);
holder.tvMobile = (TextView) convertView.findViewById(R.id.tvMobile);
holder.tvWifi = (TextView) convertView.findViewById(R.id.tvWifi);
holder.tvTotal = (TextView) convertView.findViewById(R.id.tvTotal);
holder.imageView=(ImageView)convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvDate.setText(dataList.get(position).getDate());
holder.tvMobile.setText(dataList.get(position).getMobileData());
holder.tvWifi.setText(dataList.get(position).getWifiData());
holder.tvTotal.setText(dataList.get(position).getTotalData());
holder.imageView.setImageResource(R.mipmap.ic_launcher);
return convertView;
}
}
答案 2 :(得分:0)
我解决了!现在,我会工作!!!
public class CustomAdapter extends PagerAdapter {
private LayoutInflater mInflater;
private int[] mImage;
private Context mContext;
private ImageView mImageView;
private String[] mStringTitle;
private String[] mStringDesc;
private TextView mTextTitle;
private TextView mTextDesc;
public CustomAdapter(Context mContext, int[] image, String[] title, String[] desc, PageClickListener callback ) {
this. mImage = image;
this.mContext = mContext;
this.mStringTitle = title;
this.mStringDesc = desc;
mCallback = callback;
}
public interface PageClickListener {
void onPageClick(int id);
}
private PageClickListener mCallback;
@Override
public int getCount() {
return mImage.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.list_item, container, false);
assert view != null;
mImageView = (ImageView) view.findViewById(R.id.icon);
mTextTitle = (TextView) view.findViewById(R.id.title);
mTextDesc = (TextView) view.findViewById(R.id.desc);
mImageView.setImageResource(image[position]);
mTextTitle.setText(mStringTitle[position]);
mTextDesc.setText(mStringDesc[position]);
container.addView(view, 0);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCallback.onPageClick(position);
}
});
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
MainActivity中的
public class MainActivity extends AppCompatActivity implements GuideAdapter.PageClickListener{
public static final String[] titles = new String[] { "H C Verma",
"P Bahadur", "I E Irodov", "M L Khanna",
"O P Tondon", "Morrison & Boyd", "Halliday Resnick",
"R D Sharma", "S L Loney", "Hall and Knight", "Krotov", "I A Maron" };
public static final String[] descriptions = new String[] {
"Good For clearing concepts of physics",
"Variety of examples on particular concept",
"Read to become expert in physics",
"Read to become an expert in mathematics",
"A good reference for detailed stuff",
"Read to become an expert in chemistry",
"Interesting and fundamental book of physics",
"Mandatory book forJEE aspirants",
"Another good book for high rankers",
"Simple and clear concepts of chemistry",
"favourite one for physics lovers",
"Read this for practice purposes"
};
public static final int[] images = new int[] { R.drawable.hcv,
R.drawable.bahadur, R.drawable.irodov, R.drawable.mlkhanna,
R.drawable.tondon, R.drawable.boyd, R.drawable.resnick, R.drawable.sharma,
R.drawable.loney, R.drawable.knight, R.drawable.krotov, R.drawable.maron};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, images, titles, descriptions, this );
listView.setAdapter(adapter);
}
@Override
public void onPageClick(int id) {
// enter code when onClick Item Here
}
}
答案 3 :(得分:0)
唯一困扰我的是我使用的图像的大小,我们应该使用小尺寸的图像,否则它会说像
bitmap size exceeds VM budget
谢谢大家帮助我