public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < 30; i++)
{
HashMap<String, String> object = new HashMap<String, String>();
object.put("title","title" + i );
object.put("subtitle","subtitle" + i );
object.put("time","time" + i );
data.add(object);
}
String[] from = new String[]{"title","subtitle","time"};//作为hashmap的key使用
int[] to = new int[]{R.id.textView_title,R.id.textView_subtitle,R.id.textView_time};
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.relaout, from, to);
listView.setAdapter(simpleAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
注:SimpleAdapter支持的控件有限。
==================================================================
二、设置不同行布局
代码如下:
class WeiboListAdapter extends BaseAdapter
{
private final static int VIEW_TYPE1 = 0;
private final static int VIEW_TYPE2 = 1;
private final static int VIEW_TYPE_COUNT = VIEW_TYPE2 + 1;
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
CommentViewHolder commentHolder = null;
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null)
{
if (type == VIEW_TYPE1)
{
// 微博内容
convertView = getLayoutInflater().inflate(R.layout.weibo_item, null);
holder = new ViewHolder();
holder.headerImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_headerImg);
holder.userName = (TextView) convertView.findViewById(R.id.weibo_item_tv_userName);
holder.commentNum = (TextView) convertView.findViewById(R.id.weibo_item_tv_commentNum);
holder.forwardTimes = (TextView) convertView.findViewById(R.id.weibo_item_tv_forwardTimes);
holder.content = (TextView) convertView.findViewById(R.id.weibo_item_tv_content);
holder.contentImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_contentImg);
holder.forwardContent = (TextView) convertView.findViewById(R.id.weibo_item_tv_forwardContent);
holder.forwardContentImg = (ImageView) convertView.findViewById(R.id.weibo_item_iv_forwardContentImg);
holder.from = (TextView) convertView.findViewById(R.id.weibo_item_tv_from);
holder.time = (TextView) convertView.findViewById(R.id.weibo_item_tv_time);
holder.retweetedLayout = convertView.findViewById(R.id.weibo_item_retweetedLayout);
convertView.setTag(holder);
}
else
{
// 评论列表
commentHolder = new CommentViewHolder();
convertView = getLayoutInflater().inflate(R.layout.weibocomment_item, null);
commentHolder.headerImg = (ImageView) convertView.findViewById(R.id.weibocomment_item_iv_headerImg);
commentHolder.userName = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_userName);
commentHolder.time = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_time);
commentHolder.content = (TextView) convertView.findViewById(R.id.weibocomment_item_tv_content);
convertView.setTag(commentHolder);
}
}
else
{
if (type == VIEW_TYPE1)
{
holder = (ViewHolder) convertView.getTag();
}
else
{
commentHolder = (CommentViewHolder) convertView.getTag();
}
}
WeiboJson weiboJson = mDataList.get(position);
if (type == VIEW_TYPE1)
{
updataWeiboData(holder, weiboJson);
}
else
{
updateCommentData(commentHolder, weiboJson);
}
return convertView;
}
@Override
public int getCount()
{
return mDataList.size();
}
@Override
public int getItemViewType(int position)
{
if (position == 0)
{
return VIEW_TYPE1;
}
else
{
return VIEW_TYPE2;
}
}
@Override
public int getViewTypeCount()
{
return VIEW_TYPE_COUNT;
}
@Override
public Object getItem(int position)
{
return mDataList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
}
转载于:https://blog.51cto.com/glblong/1202086