我正在尝试使用 ListView 作为杂志阅读器应用程序中的主要导航功能。我希望列表中的每个视图都在与其关联的图像下方显示故事的标题。我已经创建了一个 XML 布局,它具有一个垂直方向的 LinearLayout,顶部是 ImageView,底部是 TextView,并且正在尝试创建一个自定义适配器,在 getView 方法中扩展此布局并将图像/文本资源放入其中。这是我的代码:
package com.example.caliber;
import java.util.List;
import com.example.caliber.R;
public class MainActivity extends Activity {
Spinner issues, sections;
Button blog;
ListView mainGal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] issueList = {"issues", "fall '12", "spring '12", "fall '11", "spring '12",
"fall '10", "spring '10"};
String[] sectionList = {"sections", "campus", "entertainment", "lifestyle", "love&sex", "tech&web", "Current" };
ArrayAdapter<String> aaIssues = new ArrayAdapter<String>(this, R.layout.spin_item, issueList);
ArrayAdapter<String> aaSections = new ArrayAdapter<String>(this,
R.layout.spin_item, sectionList);
blog = (Button)findViewById(R.id.blogBtn);
issues = (Spinner)findViewById(R.id.issueSpin);
sections = (Spinner)findViewById(R.id.sectionSpin);
issues.setAdapter(aaIssues);
sections.setAdapter(aaSections);
OnClickListener mbListener = new OnClickListener() {
@Override
public void onClick(View v){
issues.setAlpha((float) 0.5);
sections.setAlpha((float) 0.5);
blog.setAlpha((float) 0.5);
}
};
ImageButton mainbtn = (ImageButton)findViewById(R.id.cbutton);
mainbtn.setOnClickListener(mbListener);
mainGal = (ListView)findViewById(R.id.mainGal);
GalAdapter ga = new GalAdapter();
mainGal.setAdapter(ga);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class GalAdapter extends ArrayAdapter<String> {
Activity context;
String[] headlines = {"Alpha bravo charlie delta", "Echo foxtrot golf
hotel", "indigo juliette kilo lima"};
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
public GalAdapter() {
super (MainActivity.this, R.layout.galleryview);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View story = li.inflate(R.layout.galleryview, parent);
ImageView image = (ImageView)story.findViewById(R.id.image);
TextView headline = (TextView)story.findViewById(R.id.headline);
image.setImageResource(images[position]);
headline.setText(headlines[position]);
return story;
}
该应用程序启动正常,我可以看到列表视图的背景,但列表中没有项目。为什么是这样?可能很清楚,我对 android 开发非常陌生,但对 Java 的基础知识有很好的理解。我知道这是由于我在这里有一个基本的误解,可能是其中一个构造函数中的错误(我对某些参数的含义仍然有些模糊)。无论如何,如果有人能帮助我更深入地理解这些问题,我将不胜感激。
提前致谢
ps 在android和java下分类它是不是很糟糕?我以后应该只使用android吗?
- -编辑:
class GalAdapter extends ArrayAdapter<String> {
Activity context;
String[] hlines;
int[] imgs;
public GalAdapter(String[] hlines, int[] imgs) {
super (MainActivity.this, R.layout.galleryview , hlines);
this.hlines = hlines;
this.imgs = imgs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null){
LayoutInflater li = (LayoutInflater)context. getSystemService (Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.galleryview, parent);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
TextView headline = (TextView)convertView.findViewById(R.id.headline);
image.setImageResource(imgs[position]);
headline.setText(hlines[position]);}
return convertView;
}
}
现在我在调用 LayoutInflator 的行上遇到空指针异常。它指的是什么指针?我假设我没有将数组 (String[] hlines + int[] imgs) 正确传递给它......这是我打电话的地方:
String[] headlines ={ "Echo foxtrot golf hotel", "Echo foxtrot golf hotel", "indigo
juliette kilo lima"};
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
....
mainGal = (ListView)findViewById(R.id.mainGal);
GalAdapter ga = new GalAdapter(headlines, images);
mainGal.setAdapter(ga);