80,469
社区成员




public class MainActivity extends Activity {
private ListView lv;
private List<Map<String, Object>> data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lv);
//获取将要绑定的数据设置到data中
data = selectRoadsStaus();
MyAdapter adapter = new MyAdapter(this);
lv.setAdapter(adapter);
}
public List<Map<String, Object>> selectRoadsStaus(){
Map<String,Object> map = new HashMap<String,Object>();
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
for(int i = 1; i <= 5 ; i++){
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("num",String.valueOf(i));
if("1".equals(String.valueOf(i))){
map1.put("bak",0xff1A78FE);//map.get(String.valueOf(i))
}else if("2".equals(String.valueOf(i))){
map1.put("bak", Color.alpha(0xff9FC95D));//map.get(String.valueOf(i))
}else if("3".equals(String.valueOf(i))){
map1.put("bak", Color.alpha(0xffFFC95C));//map.get(String.valueOf(i))
}else if("4".equals(String.valueOf(i))){
map1.put("bak", 0xffE16567);//map.get(String.valueOf(i))
}else if("5".equals(String.valueOf(i))){
map1.put("bak", Color.alpha(0xffE60513));//map.get(String.valueOf(i))
}
list.add(map1);
}
System.out.print(list);
return list;
}
//ViewHolder静态类
static class ViewHolder
{
public TextView num;
public TextView bak;
}
public class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater = null;
private MyAdapter(Context context)
{
//根据context上下文加载布局,
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
//How many items are in the data set represented by this Adapter.
//在此适配器中所代表的数据集中的条目数
return data.size();
}
@Override
public Object getItem(int position) {
// Get the data item associated with the specified position in the data set.
//获取数据集中与指定索引对应的数据项
return position;
}
@Override
public long getItemId(int position) {
//Get the row id associated with the specified position in the list.
//获取在列表中与指定索引对应的行id
return position;
}
//Get a View that displays the data at the specified position in the data set.
//获取一个在数据集中指定索引的视图来显示数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//如果缓存convertView为空,则需要创建View
if(convertView == null)
{
holder = new ViewHolder();
//根据自定义的Item布局加载布局
convertView = mInflater.inflate(R.layout.list_item, null);
holder.num = (TextView)convertView.findViewById(R.id.tv);
holder.bak = (TextView)convertView.findViewById(R.id.info);
//将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
// holder.img.setBackgroundResource((Integer)data.get(position).get("img"));
holder.num.setText((String)data.get(position).get("title"));
holder.bak.setText((String)data.get(position).get("info"));
return convertView;
}
}
}
holder.bak.setText(String.valueOf(data.get(position).get("bak")));
holder.bak.setText((String)data.get(position).get("bak"));
public class MainActivity extends Activity {
ImageView iv;
RatingBar ratingbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.image);
ratingbar=(RatingBar)findViewById(R.id.rating);
RatingBar.OnRatingBarChangeListener ratin=new RatingBar.OnRatingBarChangeListener(){
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch)
{
iv.setAlpha((int)(rating*255/5));
}
};
}}
<!-- <android.support.constraint.ConstraintLayout -->
<LinearLayout
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"
tools:context="com.example.star.MainActivity"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:max="255"
android:progress="255"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>