在我的应用程序中,我创建了一个Dialog
。其中包含ListView。在我的ListView中,我正在设置评级栏。但我无法查看RatingBar。我使用两个xml
来创建完整的Dialog
。
popup_dialog = new Dialog(Home_Screen.this);
popup_dialog.setContentView(R.layout.rating_layout);
popup_dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup_dialog.setCancelable(true);
LayoutInflater inflater = Home_Screen.this.getLayoutInflater();
final View rating_view = inflater.inflate(R.layout.rating_list, null);
// LayoutInflater rating = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
// final View rating_view = rating.inflate(R.layout.rating_list, null);
TextView tv1 = (TextView)popup_dialog.findViewById(R.id.textView1);
tv1.setText("Give Rating");
lv = (ListView) popup_dialog.findViewById(R.id.listView1);
ImageView close = (ImageView)popup_dialog.findViewById(R.id.close);
Button give_rating = (Button)popup_dialog.findViewById(R.id.give_rating);
Custum_Adapter adapter = new Custum_Adapter(Home_Screen.this, applist);
lv.setAdapter(adapter);
ratingbar = (RatingBar)rating_view.findViewById(R.id.vendor_rating);
//setOnRatingBarChangeListener not responding .....may be because I am not able to get proper view.
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Rating-->"+rating+"Position-->"+lv.getPositionForView(ratingBar), Toast.LENGTH_LONG).show();
System.out.println("Rating------>"+rating+"position----->"+lv.getPositionForView(ratingBar));
}
});
这是rating_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="text"
android:textColor="#6eb5c4"
android:textStyle="bold" />
<RatingBar
android:id="@+id/vendor_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:numStars="5"
android:rating="2.0"
android:padding="10dp"
android:stepSize="0.1" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="@drawable/editbackground"
android:hint="Tip"
android:padding="10dp" />
</LinearLayout>
</LinearLayout>
这是rating_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="@+id/custom_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/action_bar_popup" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="290dp"
android:background="@drawable/new_background"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="0.38"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
<Button
android:id="@+id/give_rating"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Give Rating"
android:textColor="#000000"
android:layout_marginBottom="5dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/editbackground"/>
</LinearLayout>
</LinearLayout>
我正在通过CustomAdapter
在对话框中设置rating_list.xmlprivate class Custum_Adapter extends BaseAdapter {
ArrayList<HashMap<String, String>> list;
public Custum_Adapter(Context context, ArrayList<HashMap<String, String>> applist) {
this.list = applist;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.rating_list, parent, false);
}
System.out.println("list size*******"+list.size());
HashMap<String, String> a = list.get(position);
System.out.println("Position of hash map----->"+position);
// String id = a.get("id");
String name = a.get("provider_name");
System.out.println("name--------->"+name);
// String c_cost = a.get("cost");
// String c_id = a.get("cost_id");
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
// TextView tv1 = (TextView) convertView.findViewById(R.id.textView2);
// TextView tv2 = (TextView) convertView.findViewById(R.id.costing_id);
// TextView tv3 = (TextView) convertView.findViewById(R.id.cost);
tv.setText(name);
// tv1.setText(id);
// tv2.setText(c_id);
// tv3.setText(c_cost);
return convertView;
}
}
所以setOnRatingBarChangeListener
没有响应(不在控制台或Toast上打印评级)可能是因为我无法正确查看RatingBar
。那么有人可以告诉我这里有什么问题吗?
答案 0 :(得分:0)
当您将rating_view设置为对话框中的任何视图或布局时,您将无法以此方式获取视图。在对话框中创建relativeLayout或视图,并将rating_view添加到其中。干杯!
答案 1 :(得分:0)
您尝试实现的目标可以通过DialogFragment更简单地完成,您可以从中访问对话框的所有包含视图。
http://developer.android.com/reference/android/app/DialogFragment.html
如果您仍想使用对话框,我相信如果您在setContentView之前移动了膨胀,您的问题就会得到解决,因此您将在对话框中引用该视图。但不能确认100%它会起作用。
popup_dialog = new Dialog(Home_Screen.this);
LayoutInflater inflater = Home_Screen.this.getLayoutInflater();
final View rating_view = inflater.inflate(R.layout.rating_list, null);
popup_dialog.setContentView(rating_view);
popup_dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup_dialog.setCancelable(true);
TextView tv1 = (TextView)rating_view.findViewById(R.id.textView1);
tv1.setText("Give Rating");
lv = (ListView) rating_view.findViewById(R.id.listView1);
ImageView close = (ImageView)rating_view.findViewById(R.id.close);
Button give_rating = (Button)rating_view.findViewById(R.id.give_rating);
Custum_Adapter adapter = new Custum_Adapter(Home_Screen.this, applist);
lv.setAdapter(adapter);
ratingbar = (RatingBar)rating_view.findViewById(R.id.vendor_rating);
ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Rating-->"+rating+"Position-->"+lv.getPositionForView(ratingBar), Toast.LENGTH_LONG).show();
System.out.println("Rating------>"+rating+"position----->"+lv.getPositionForView(ratingBar));
}
});
答案 2 :(得分:0)
此问题的解决方法是我需要将ratingbar.setOnRatingBarChangeListener
放在getView();
的{{1}}方法中。通过这种方式,我可以在CustomAdapter
中获得由RatingBar
设置的CustomAdapter
视图。
Dialog