【发布时间】:2023-03-06 00:21:01
【问题描述】:
我尝试使用 Array List 设置 Rating Bar 的值,我将 rating 的值设置为 Float,但我不知道如何设置该值并将其显示到 Rating Bar,我使用 listview 来显示数据。请帮我解决这个问题
这是我的 Artikel 活动,我将评分栏设置为浮动
包 com.example.latihanlist;
public class Artikel {
private int imageId;
private float ratingId;
private String title;
private String timestamp;
private String descriptions;
public Artikel(int imageId, String title, String timestamp, String
descriptions, float ratingId) {
this.imageId = imageId;
this.title = title;
this.timestamp = timestamp;
this.descriptions = descriptions;
this.ratingId = ratingId;
}
public int getImageId() {
return imageId;
}
public float getRatingId() {
return ratingId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getDescriptions() {
return descriptions;
}
public void setDescriptions(String descriptions) {
this.descriptions = descriptions;
}
}
这里是我的 MainActivity,我设置了 rating 2.0 的值
package com.example.latihanlist;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewStub stubList;
private ListView listview;
private ListViewArrayAdapter listViewArrayAdapter;
private List<Artikel> artikelList;
private List<Artikel> getArtikelList() {
artikelList= new ArrayList<>();
artikelList.add(new Artikel(R.drawable.mob,"Title 1","1 hours ago","Descriptions of text 1", "2.0"));
artikelList.add(new Artikel(R.drawable.mob,"Title 2","1 hours ago","Descriptions of text 2", "2.0"));
artikelList.add(new Artikel(R.drawable.mob,"Title 3","1 hours ago","Descriptions of text 3", "2.0"));
artikelList.add(new Artikel(R.drawable.mob,"Title 4","1 hours ago","Descriptions of text 4", "2.0"));
artikelList.add(new Artikel(R.drawable.ic_launcher_background,"Title 5","1 hours ago","Descriptions of text 5", "2.0"));
return artikelList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stubList=(ViewStub) findViewById(R.id.stublist);
stubList.inflate();
listview=(ListView) findViewById(R.id.list_view);
stubList.setVisibility(View.VISIBLE);
getArtikelList();
listViewArrayAdapter=new ListViewArrayAdapter(this,R.layout.list_item,artikelList);
listview.setAdapter(listViewArrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(getApplicationContext(),
// artikelList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("dataTitle", artikelList.get(position).getTitle().toString());
bundle.putInt("dataImg", artikelList.get(position).getImageId());
bundle.putString("dataDesc", artikelList.get(position).getDescriptions().toString());
Intent intent = new Intent(MainActivity.this, DetailBerita.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
Button mBtn_login = (Button)findViewById(R.id.button1);
mBtn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MenuApkAndroid.class);
startActivity(intent);
}
});
}
}
这是我的 list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/mob"/>
<TextView
android:id="@+id/textTitle"
android:text="Title of the Text"
android:layout_marginLeft="75dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView
android:id="@+id/textTime"
android:layout_below="@id/textTitle"
android:text="1 hours ago"
android:layout_marginLeft="75dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="9dp"/>
<TextView
android:id="@+id/textDesc"
android:layout_below="@+id/textTime"
android:text="This is descriptions of the title of text"
android:layout_marginLeft="75dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="10dp"/>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:numStars="5"
android:stepSize="1.0"
/>
</RelativeLayout>
【问题讨论】:
-
嗨。你能把你的customeArrayAdapter代码贴在这里吗?