我是android的初学者,我想学习使用ListView。 我搜索过它并尝试制作列表视图。
但不幸的是,它在开始时有力量。
这里是activity_main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/List"/>
这里是list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/groupTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp" />
</LinearLayout>
这里是CustomListView.java:
public class CustomListView extends BaseAdapter{
private ArrayList<ItemClass> arr = new ArrayList<ItemClass>();
public CustomListView( ArrayList<ItemClass> arr) {
this.arr = arr;
}
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int index) {
return getItem(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
if(view == null)
{
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
view = inflator.inflate(R.layout.list_row, parent, false);
}
ItemClass child = arr.get(index);
TextView Text = (TextView) view.findViewById(R.id.groupTitle);
Text.setText(child.getName());
return null;
}
}
这是主要课程:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ItemClass> arr = new ArrayList<ItemClass>();
arr.add(new ItemClass("Sh " , 1));
CustomListView adaptor;
adaptor = new CustomListView(arr);
ListView listView = (ListView) findViewById(R.id.List);
listView.setAdapter(adaptor);
}
}
最后这里是ItemClass.java:
public class ItemClass {
//private Context context;
String name;
private int rate;
public ItemClass(String name , int rate ) {
this.setName(name);
this.setRate(rate);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setRate(int rate) {
this.rate = rate;
}
public int getRate() {
return this.rate;
}
}
我评论listView.setAdapter(adaptor);
然后没有力量关闭。
提前感谢您的关注
答案 0 :(得分:3)
首先你纠正这个,你应该返回view
return view;
而不是
return null;
getView(....)
Adapter
方法
答案 1 :(得分:0)
public class ListViewAdapter extends BaseAdapter{
private ArrayList<String> listname;
private ArrayList<Integer> listimg;
private ArrayList<String> listtype;
private Activity activity;
public ListViewAdapter(Activity act,ArrayList<String> liststr,ArrayList<Integer> listint,ArrayList<String> list_type)
{
super();
this.listname=new ArrayList<String>();
this.listname=liststr;
this.listimg=listint;
this.listtype=list_type;
this.activity=act;
System.out.println("this is contry name "+this.listname);
System.out.println("this is img name "+this.listimg);
System.out.println("this is type "+this.listtype);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("len " + listname.size());
return listname.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listname.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.notes_listlayout, null);
view.txtViewTitle = (TextView) convertView
.findViewById(R.id.note_layout_name);
//view.txtViewTitle.setText("NO Notes Available.");
view.txtViewType= (TextView) convertView
.findViewById(R.id.note_layout_type);
view.txtViewType.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
// view.txtViewTitle.setTextColor(Color.BLACK);
//view.txtViewType.setTextColor(Color.GRAY);
/*TextView emptyView = (TextView)findViewById(R.id.emptyTv);
view.setEmptyView(yourEmptyView);*/
view.imgViewFlag = (ImageView) convertView
.findViewById(R.id.note_layout_img);
// view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
convertView.setTag(view);
}
else {
view = (ViewHolder) convertView.getTag();
}
System.gc();
try {
view.txtViewTitle.setText(listname.get(position));
view.txtViewType.setText(listtype.get(position));
view.imgViewFlag.setImageResource(listimg.get(position));
} catch (Exception e) {
//System.out.println("this is error " + e.getMessage());
}
return convertView;
}
}
实施它
ListViewAdapter mAdapter;
mAdapter = new ListViewAdapter(this, listCountry, listFlag,
listfoodtype);
lv_note = (ListView) findViewById(R.id.lv_note);
// gridView.setBackgroundColor(Color.WHITE);
lv_note.setAdapter(mAdapter);
答案 2 :(得分:0)
您需要更改适配器类中的getItem
方法,如下所示:
@Override
public Object getItem(int index) {
return arr.get(index);
}
同时更改getView
方法以返回除null之外的自定义视图,如下所示:
@Override
public View getView(int index, View view, ViewGroup parent) {
if(view == null)
{
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
view = inflator.inflate(R.layout.list_row, parent, false);
}
//.............................
return view;
}