5
我有一個ListView包含的RatingBar指示的東西,如果你點擊它彈出一個對話框(AlertDialog)的LisView的項目包括的RatingBar,現在我不知道:嵌入的RatingBar在AlertDialog
- 如何風俗在對話框中顯示的RatingBar(下面的代碼不起作用,我認爲它是因爲最後的,這使得它不能被定製)。
- 如何將評分從對話框RatingBar傳遞給ListView RatingBar。
我當前的代碼是:
final RatingBar ratingBar = new RatingBar(getApplicationContext());
ratingBar.setRating(0);
ratingBar.setStepSize(1);
ratingBar.setNumStars(2);
builder.setTitle(R.string.alertdialog_title)
.setIcon(android.R.drawable.ic_dialog_info)
.setView(ratingBar)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do something.
}
})
.setNegativeButton("Cancel", null);
EDIT1
我已經表現出對列表視圖與fileListAdapter(FileListAdapater的實例作爲代碼1)項的評級。現在的問題是:在對話框中點擊
- 該評級變化之後,但它不是,我點擊,如何地圖與評級(位於對話框),更改的項目的項目。
- 如何存儲評分,每次ListView刷新時,評分都會消失。
我已經把onItemClickListener的對話框代碼設置爲code2。
代碼1:
public class FileListAdapter extends BaseAdapter {
private Context context;
private ArrayList<File> files; // The ArrayList to store the files
private boolean isSDcard; // To indicate whether the file is the SDcard
private LayoutInflater mInflater; // The LayoutInflater
public FileListAdapter (Context context, ArrayList<File> files, boolean isSDcard) {
this.context = context;
this.files = files;
this.isSDcard = isSDcard;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return files.size();
}
@Override
public Object getItem (int position) {
return files.get(position);
}
@Override
public long getItemId (int position) {
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
/**
* The view is not initialized,initialize the view according to the layout file,
* and bind it to tag.
*/
if(convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.file_list_item, null); // Inflate from the layout file
convertView.setTag(viewHolder);
viewHolder.file_title = (TextView) convertView.findViewById(R.id.file_title); // Find the file_title textview
viewHolder.file_icon = (ImageView) convertView.findViewById(R.id.file_icon); // Find the file_icon imageview
viewHolder.file_path = (TextView) convertView.findViewById(R.id.file_path); // Find the file_path textview
viewHolder.encrypt_level = (RatingBar) convertView.findViewById(R.id.encrypt_level); // Find the rating_bar view
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
File file = (File) getItem(position);
if(position == 0 && !isSDcard) { // Add the back to SDcard item
viewHolder.file_title.setText(R.string.back_to_sdcard);
viewHolder.file_icon.setImageResource(R.drawable.sdcard_icon);
viewHolder.file_path.setText(Environment.getExternalStorageDirectory().toString());
viewHolder.encrypt_level.setRating(0);
}
if(position == 1 && !isSDcard) { // Add the back to parent item
viewHolder.file_title.setText(R.string.back_to_parent);
viewHolder.file_icon.setImageResource(R.drawable.folder_up_icon);
viewHolder.file_path.setText(file.getPath());
viewHolder.encrypt_level.setRating(0);
} else { // The current filepath is the SDcard or the position is neither 0 nor 1
String fileName = file.getName();
viewHolder.file_title.setText(fileName);
viewHolder.file_path.setText(file.getPath());
viewHolder.encrypt_level.setRating(0);
if(file.isDirectory()) { // The variable file is a directory
viewHolder.file_icon.setImageResource(R.drawable.folder_icon);
} else { // The variable file is a file
viewHolder.file_icon.setImageResource(R.drawable.file_icon);
}
}
return convertView;
}
private class ViewHolder {
private ImageView file_icon; // The icon of the file
private TextView file_title; // The title of the file
private TextView file_path; // The path of the file
private RatingBar encrypt_level; // The encrypt level of the file
}
}
碼2:
final Dialog rankDialog = new Dialog(MainActivity.this, R.style.FullHeightDialog);
rankDialog.setContentView(R.layout.rank_dialog);
rankDialog.setCancelable(true);
final RatingBar ratingBar = (RatingBar)rankDialog.findViewById(R.id.dialog_ratingbar);
TextView text = (TextView) rankDialog.findViewById(R.id.rank_dialog_text1);
text.setText("Set Rating!");
Button updateButton = (Button) rankDialog.findViewById(R.id.rank_dialog_button);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(Tag, ratingBar.getRating() + "");
RatingBar ratebar = (RatingBar) findViewById(R.id.encrypt_level); // The RatingBar in the ListView.
ratebar.setRating(ratingBar.getRating()); // Change the rating.
rankDialog.dismiss();
}
});
rankDialog.show();
定製對話框工作,但t這仍然是另一個需要解決的問題。我已經將它添加到了問題中。 – twlkyao
你想存儲收視率? –
是的,作爲一個指標,現在問題是每次刷新ListView時評分都會消失,並且它不在我單擊的項目上。 – twlkyao