【发布时间】:2014-02-06 11:20:33
【问题描述】:
我想在我的应用中创建一个收藏列表。 我正在使用数组适配器。
我有一个字符串和一个评级栏,只有一颗星! 我希望如果你按一次星星,它会点亮星星(并让它成为最爱) 当你再次按下它时,星星没有被填满......(现在它不再喜欢了)
我应该在这里写什么?如果问题甚至可以纠正这个问题吗?
public class ChooseFavorites extends Activity implements OnItemClickListener
{
String[] stations;
float[] rating;
boolean[] ifCheacked;
RatingAdapter ratingAdapter;
ArrayAdapter<String> arr;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_favorites);
stations = getResources().getStringArray(R.array.stations);
rating = new float[stations.length];
ifCheacked = new boolean[stations.length];
Arrays.fill(ifCheacked, false);
ratingAdapter = new RatingAdapter(this, R.layout.favorite_row, stations);
list = (ListView)findViewById(R.id.stationsList1);
list.setAdapter(ratingAdapter);
list.setOnItemClickListener(this);
}
class RatingAdapter extends ArrayAdapter<String>
{
Context ctx;
LayoutInflater inflater;
TextView stationName;
RatingBar star;
public RatingAdapter(Context context, int textViewResourceId, String[] objects)
{
super(context, textViewResourceId, objects);
ctx = context;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row==null)
{ // Object reuse
inflater = getLayoutInflater();
row = inflater.inflate(R.layout.favorite_row, parent, false);
}
stationName = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName.setText(stations[position]);
star= (RatingBar)row.findViewById(R.id.ratingBar1);
if (!ifCheacked[position]) //ifChecked is false
{
star.setRating(rating[position]);
star.setTag(position);
ifCheacked[position] = true;
}
else
{
}
star.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
{
public void onRatingChanged(RatingBar ratingBar, float rating2, boolean fromUser)
{
if(!fromUser) return;
int index = (Integer)(ratingBar.getTag());
rating[index] = rating2;
}
});
return row;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
【问题讨论】:
标签: java android eclipse listview