首先,您需要在布局xml中使用listview
<ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" />
将列表声明为实例变量
ArrayList<CommentsLibrary> list = new ArrayList<CommentsLibrary>();
然后
for (int i = 0; i < jsonArray.length(); i++) { JSonObject jsonObject = jsonArray.getJSonObject(i); String name = jsonObject.optString("name","defaultValue"); String content = jsonObject.optString("content","defaultValue"); String published = jsonObject.optString("published","defaultValue"); list.add(new CommentsLibrary(name, content, published)); }
然后初始化listview
ListView lv =(ListView)findViewById(R.id.list);CustomAdapter cus = new CustomAdapter(ActivityName.this,list);lv.setAdapter(cus);
用3个textview定义一个自定义布局。(list_item.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="31dp" android:text="TextView" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_centerHorizontal="true" android:text="TextView" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:layout_marginRight="24dp" android:text="TextView" /></RelativeLayout>
然后,通过扩展基本适配器来定义自定义适配器。覆盖会
getView膨胀自定义布局并在那里设置文本视图。
public class CustomAdapter extends baseAdapter{ LayoutInfalter mInflater; ArrayList<CommentsLibrary> list; public CustomAdapter(Context context,ArrayList<CommentsLibrary> list) { mInflater = LayoutInfalter.from(context); this.list = list; } @Overridepublic int getCount() { // TODO Auto-generated method stub return list.size();}@Overridepublic Object getItem(int position) { // TODO Auto-generated method stub return position;}@Overridepublic long getItemId(int position) { // TODO Auto-generated method stub return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if(convertView==null) { convertView =minflater.inflate(R.layout.list_item, parent,false); holder = new ViewHolder(); holder.tv1 = (TextView) convertView.findViewById(R.id.textView1); holder.tv2 = (TextView) convertView.findViewById(R.id.textView2); holder.tv3 = (TextView) convertView.findViewById(R.id.textView3); convertView.setTag(holder); } else { holder= (ViewHolder) convertView.getTag(); } holder.tv1.setText(list.get(position).getName()); holder.tv2.setText(list.get(position).getContent()); holder.tv3.setText(list.get(position).getPublished()); return convertView;}static class ViewHolder{ TextView tv1,tv2,tv3}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)