我从android开始。在一个活动中,我创建了一个带有自定义视图的listview,当我单击时,我打开了一个带有多个微调器和文本视图的对话框。但是,我的旋转器不工作。将显示微调器列表,但单击时不会发生任何情况,也不会显示默认值。
提前感谢您的帮助!
下面是一个驱动链接,可以查看发生了什么:https://drive.google.com/file/d/1t2omoa7gjqjnttvv6wig5eshyo5cwj0v/view?usp=sharing
下面是我的代码:teamactivity.java
public class TeamActivity extends AppCompatActivity{
ArrayList<Team> teams;
static String[] selected;
static int id_selected;
Spinner s_p1;
ArrayAdapter ad_sp1;
Spinner s_p2;
Spinner s_p3;
//Constructeur
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team);
/*Team creation*/
teams = new ArrayList<Team>();
int id = 1;
for (int i = 0; i < participants.size(); i = i + 3) {
teams.add(new Team(id, participants.get(i), participants.get(i + 1), participants.get(i + 2)));
id++;
}
/*list view*/
final ListView listView = (ListView) findViewById(R.id.listView_team);
// Create the adapter to convert the array to views
final TeamAdapter adapter = new TeamAdapter(this, teams);
// Attach the adapter to a ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, final View view, int i, long l) {
id_selected = i;
AlertDialog.Builder d_builder = new AlertDialog.Builder(listView.getContext());
View d_order = getLayoutInflater().inflate(R.layout.dialog_order,null);
s_p1 = (Spinner) d_order.findViewById(R.id.spinner_p1);
selected = new String[3];
ad_sp1 = new ArrayAdapter<String>(listView.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.value));
ad_sp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_p1.setAdapter(ad_sp1);
d_builder.setPositiveButton("Validez", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
d_builder.setView(d_order);
AlertDialog dialog = d_builder.create();
dialog.show();
}
});
}
}
布局/对话框\u order.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<Spinner
android:id="@+id/spinner_p3"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="6dp"
android:layout_marginTop="116dp"
android:layout_toEndOf="@+id/textView_fname_p3"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_name_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="25dp" />
<TextView
android:id="@+id/textView_fname_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="276dp" />
<TextView
android:id="@+id/textView_name_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="63dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="66dp" />
<TextView
android:id="@+id/textView_fname_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="65dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="276dp"
tools:layout_editor_absoluteY="66dp" />
<TextView
android:id="@+id/textView_name_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="112dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="116dp" />
<TextView
android:id="@+id/textView_fname_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="113dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="276dp"
tools:layout_editor_absoluteY="116dp" />
<Spinner
android:id="@+id/spinner_p1"
android:spinnerMode="dialog"
android:entries="@array/value"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="7dp"
android:layout_marginTop="16dp"
android:layout_toEndOf="@+id/textView_fname_p1"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner_p2"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="7dp"
android:layout_marginTop="64dp"
android:layout_toEndOf="@+id/textView_fname_p2"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
```
**values/order_value.xml**
```<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="value">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
团队适配器.java
public class TeamAdapter extends ArrayAdapter<Team> {
// View lookup cache
private static class ViewHolder {
TextView name_p1;
TextView name_p2;
TextView name_p3;
TextView fname_p1;
TextView fname_p2;
TextView fname_p3;
TextView level;
TextView id_team;
}
public TeamAdapter(Context context, ArrayList<Team> t) {
super(context, R.layout.item_team, t);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Team t = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_team, parent, false);
viewHolder.name_p1 = (TextView) convertView.findViewById(R.id.textView_name_p1);
viewHolder.name_p2 = (TextView) convertView.findViewById(R.id.textView_name_p2);
viewHolder.name_p3 = (TextView) convertView.findViewById(R.id.textView_name_p3);
viewHolder.fname_p1 = (TextView) convertView.findViewById(R.id.textView_fname_p1);
viewHolder.fname_p2 = (TextView) convertView.findViewById(R.id.textView_fname_p2);
viewHolder.fname_p3 = (TextView) convertView.findViewById(R.id.textView_fname_p3);
viewHolder.level = (TextView) convertView.findViewById(R.id.textView_nb_lvl);
viewHolder.id_team = (TextView) convertView.findViewById(R.id.textView_id_team);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data from the data object via the viewHolder object
// into the template view.
viewHolder.name_p1.setText(t.getParticipants().get(0).getName());
viewHolder.name_p2.setText(t.getParticipants().get(1).getName());
viewHolder.name_p3.setText(t.getParticipants().get(2).getName());
viewHolder.fname_p1.setText(t.getParticipants().get(0).getFirstName());
viewHolder.fname_p2.setText(t.getParticipants().get(1).getFirstName());
viewHolder.fname_p3.setText(t.getParticipants().get(2).getFirstName());
viewHolder.level.setText(String.valueOf(t.getLevel()));
viewHolder.id_team.setText(String.valueOf(t.getId()));
// Return the completed view to render on screen
return convertView;
}
}
项目\u team.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">
<TextView
android:id="@+id/textView_name_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="7dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_name_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="52dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_name_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="97dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_fname_p1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="461dp"
android:layout_marginTop="7dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_fname_p2"
android:layout_width="251dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="461dp"
android:layout_marginTop="52dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_fname_p3"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="461dp"
android:layout_marginTop="97dp"
android:text="TextView"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageViewP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="728dp"
android:layout_marginTop="11dp"
android:src="@drawable/pf" />
<ImageView
android:id="@+id/imageViewP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="730dp"
android:layout_marginTop="55dp"
android:src="@drawable/ps" />
<ImageView
android:id="@+id/imageViewP3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="728dp"
android:layout_marginTop="101dp"
android:src="@drawable/pt" />
<TextView
android:id="@+id/textView_lvl_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="36dp"
android:layout_marginTop="6dp"
android:text="Niveau :"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_nb_lvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="153dp"
android:layout_marginTop="7dp"
android:textSize="30sp" />
<TextView
android:id="@+id/textView_id_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="4dp"
android:layout_marginTop="12dp"
android:textSize="20sp" />
</RelativeLayout>
团队.java
public class Team {
private ArrayList<Participant> _team;
private int _level;
private int _id;
/*Builder*/
public Team(int id,ArrayList<Participant>team, int lvl){
this._team = team;
this._level=lvl;
this._id=id;
}
public Team(int id,Participant p1, Participant p2,Participant p3,int lvl){
ArrayList<Participant> team = new ArrayList<Participant>();
team .add(p1);
team.add(p2);
team.add(p3);
this._team = team;
this._level=lvl;
this._id=id;
}
public Team(int id,Participant p1, Participant p2,Participant p3){
ArrayList<Participant> team = new ArrayList<Participant>();
team.add(p1);
team.add(p2);
team.add(p3);
this._team = team;
this._level=p1.getLevel()+p2.getLevel()+p3.getLevel();
this._id=id;
}
/*Getter & Setter*/
public void setParticipants(ArrayList<Participant> team){
this._team=team;
}
public void setParticipants(Participant p1, Participant p2,Participant p3){
ArrayList<Participant> team = new ArrayList<Participant>();
team .add(p1);
team.add(p2);
team.add(p3);
this._team = team;
}
public ArrayList<Participant> getParticipants(){
return _team;
}
public void setLevel(int lvl){
this._level=lvl;
}
public int getLevel(){
return _level;
}
public void setId(int id){
this._id=id;
}
public int getId(){
return _id;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!