使用自定义列表视图制作项目。当我运行项目时,我只是得到一个空白的活动。我编写了BaseAdapter类来将数据放入listview中。我将String ArrayList传递给baseadapter以填充listview。有帮助吗?感谢
public class MainActivity extends Activity {
ListView L;
ArrayList<String> data= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data.add("two");
data.add("Three");
data.add("Four");
L=(ListView) findViewById(R.id.listView1);
MyAdapter Ada = new MyAdapter(MainActivity.this,data);
L.setAdapter(Ada);
}
@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;
}
}
公共类MyAdapter扩展了BaseAdapter {
Context context;
private ArrayList<String> data= new ArrayList<String>();
public MyAdapter(Context context ,ArrayList<String> data )
{
this.context=context;
this.data=data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
convertView = null;
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.listlayout, parent, false);
holder = new ViewHolder();
holder.mainpic = (ImageView)convertView.findViewById(R.id.imageView1);
holder.text= (TextView)convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(this.data.get(position));
return convertView;
}
static class ViewHolder {
ImageView mainpic;
TextView text;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
答案 0 :(得分:3)
将getCount()
更改为:
@Override
public int getCount() {
return data.size();
}
目前,您始终返回0,这告诉ListView没有要显示的项目。
另外,您可能会考虑实际实施getItem()
:
@Override
public Object getItem(int position) {
return data.get(position);
}
并使用它将你的物品送到其他地方。
答案 1 :(得分:0)
不要显式设置convertView null它只会在第一次调用getView时出现null,否则会导致性能问题
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(R.layout.listlayout, parent, false);
holder = new ViewHolder();
holder.mainpic = (ImageView)convertView.findViewById(R.id.imageView1);
holder.text= (TextView)convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(this.data.get(position));
return convertView;
}
还要在代码中进行以下更改:
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}