Java a「;收藏夹列表“;在android中-重新更改分级栏
我想在我的应用程序中列出收藏夹。 我正在使用arrayadapter 我有一个字符串和一个评级条,只有一颗星! 我想如果你按下星星一次,它就会点亮星星(并使它成为最受欢迎的) 当你再按一次,星星没有填满。。。(现在它不再是最受欢迎的了) 我应该在这里写什么?这个问题对吗Java a「;收藏夹列表“;在android中-重新更改分级栏,java,android,eclipse,listview,Java,Android,Eclipse,Listview,我想在我的应用程序中列出收藏夹。 我正在使用arrayadapter 我有一个字符串和一个评级条,只有一颗星! 我想如果你按下星星一次,它就会点亮星星(并使它成为最受欢迎的) 当你再按一次,星星没有填满。。。(现在它不再是最受欢迎的了) 我应该在这里写什么?这个问题对吗 public class ChooseFavorites extends Activity implements OnItemClickListener { String[] stations; float[]
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
}
}
public类ChooseFavorites扩展活动实现了McClickListener
{
串[]个站;
浮动[]评级;
布尔[]如果已确认;
比率适配器比率适配器;
ArrayAdapter-arr;
列表视图列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u选择\u收藏夹);
stations=getResources().getStringArray(R.array.stations);
额定值=新浮球[站长];
ifCheacked=新布尔值[stations.length];
数组。填充(如果已确认,则为false);
比率适配器=新比率适配器(此,R.layout.favorite_行,站);
列表=(ListView)findViewById(R.id.StationList1);
列表.设置适配器(比率适配器);
list.setOnItemClickListener(此);
}
类比率适配器扩展了ArrayAdapter
{
上下文ctx;
充气机;
TextView站点名称;
拉丁巴星;
公共比率适配器(上下文上下文、int textViewResourceId、字符串[]对象)
{
超级(上下文、textViewResourceId、对象);
ctx=上下文;
充气器=充气器。从(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图行=转换视图;
if(行==null)
{//对象重用
充气机=充气机();
行=充气机。充气(R.layout.favorite\u行,父项,false);
}
stationName=(TextView)row.findViewById(R.id.textFavoriteItemInRow);
stationName.setText(站[位置]);
星形=(评级条)行。findViewById(R.id.ratingBar1);
如果(!ifCheacked[position])//ifChecked为false
{
星型设定值(额定值[位置]);
star.setTag(位置);
ifCheacked[位置]=真;
}
其他的
{
}
star.setOnRatingBarChangeListener(新的RatingBar.OnRatingBarChangeListener()
{
更改了公共无效率(率条率条、浮动率条、布尔值fromUser)
{
如果(!fromUser)返回;
int index=(整数)(ratingBar.getTag());
评级[指数]=评级2;
}
});
返回行;
}
}
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
}
}
您可以通过复选框完成此操作。将两个图像设置为oncheck(设置图像(亮星))和uncheck(设置图像(撤消图像))
如果(!isCheck){
您可以通过复选框来完成此操作。将两个图像设置为oncheck(设置图像(亮星))和uncheck(设置图像(撤消图像)) 如果(!isCheck){
您可以通过复选框来完成此操作。将两个图像设置为oncheck(设置图像(亮星))和uncheck(设置图像(撤消图像)) 如果(!isCheck){
您可以通过复选框来完成此操作。将两个图像设置为oncheck(设置图像(亮星))和uncheck(设置图像(撤消图像)) 如果(!isCheck){
也许你不应该使用
分级栏
,而应该使用复选框
。我在我的一个应用程序中使用了复选框
,这正是你所需要的
基本上,您需要做的是:
在res\drawable\文件夹中,定义一个新的布局文件,假设您将其命名为checkbox.xml
在该文件中,您可以定义如下内容:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/checkbox_star_down" />
<item android:state_checked="false" android:drawable="@drawable/checkbox_star" />
</selector>
<CheckBox
android:id="@+id/soloR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:onClick="somethingChanged"
android:textSize="12sp"
android:textColor="#FFFFFF"
android:button="@drawable/checkboxes"
android:text="@string/soloR" />
很好,您可能看到了设置为刚才定义的复选框的android:button
属性。完成后,删除您的star.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()…
实现,并将其替换为:
onCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)
onCheckedChanged
的侦听器提供两个参数:CompoundButton-buttonView,boolean-isChecked
基本上,第二条告诉你你的明星是否闪亮。你的代码的其余部分看起来一般都是合理的,但是:
- 我不会做
row=convertView
赋值。这是我见过很多次的做法,直接在convertView
实例上工作是没有意义的
<> L> > P>我不知道你在<代码> ListVIEW < /C>中工作了多少行,但是如果它们太多,或者很难渲染,我会考虑使用<代码> ViWHOOLD 。如果你没有和它们一起工作,这里有一个很大的解释。
- 我发现您在每一行上都使用了
findViewById()
。这可能会导致巨大的性能问题,因为此调用非常昂贵。我会同时保存这两个绘图项(假设您的star可能只有2个)在您的类中,并在构造函数中分配它们,因此您只需处理它们一次——或者正如我前面所说的,为此使用viewHolder
s
- 请记住一件事:如果您不保存更改的状态,一旦您的行从屏幕上“消失”(例如,因为用户正在列表中滚动)如果行是新呈现的,您将看到以前的状态,即更改不会保存。为此,在
ArrayAdapter
扩展中处理数据非常重要,每次用户单击星号时,将其状态保存在类中。我指的是第三个构造函数参数:public RatingAdapter(Context Context,int textViewResourceId,String[]objects)
。现在您只需对其调用super
,但不保存它。请记住,这是您的工作数据,任何更改都应反映在该对象数组中,否则每次渲染行时,都不会保存更改。
<CheckBox
android:id="@+id/soloR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:onClick="somethingChanged"
android:textSize="12sp"
android:textColor="#FFFFFF"
android:button="@drawable/checkboxes"
android:text="@string/soloR" />
onCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)
class RatingAdapter extends ArrayAdapter<String>
{
Context ctx;
LayoutInflater inflater;
TextView stationName;
RatingBar star;
ArrayMap<String, Boolean> rowLayout = ArrayMap<String, Boolean>();
public RatingAdapter(Context context, int textViewResourceId, String[] objects)
{
super(context, textViewResourceId, objects);
ctx = context;
inflater = LayoutInflater.from(context);
for (String rowText : (String) objects)
rowLayout.put(rowText, false); // Supposing all your initial stars will be off
}
TextView tv = (TextView) convertView.findViewById(R.id.lvTextView);
final ListView lv = (ListView) findViewById(R.id.your_listview));
RatingAdapter adapter = (RatingAdapter) lv.getAdapter();
adapter.notifyDataSetChanged();
private ArrayList<String> elemlist;
elemlist.add("First row");
elemlist.add("Second one");
MultiLayoutArrayAdapter adapter = new MultiLayoutArrayAdapter(MyActivity.this, R.layout.my_custom_layout, elemlist);
elemlist.add("Yet another row");
adapter.notifyDataSetChanged();