从 ListView 项目中检索数据
Retrieve data from ListView items
我用适配器制作了一个列表视图,每个列表视图项包含四个单选按钮。
我想要做的是在包含 activity.And 的列表视图上有一个按钮,当我单击该按钮时,它将检索每个项目的单选按钮的所有检查状态并将它们发送到其他活动。
虽然我搜索了很多,但我不知道如何检索该数据。这是 Activity.java 文件
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.width;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.util.TypedValue.applyDimension;
/**
* Created by jack on 30/08/17.
*/
public class TempActivity extends AppCompatActivity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s7_1);
ArrayList<String> titlesArray = new ArrayList<>();
titlesArray.add("إيه رأيك في البني آدم اللي شرح الدورة");
titlesArray.add("ايه رأيك في المكان اللي تمت فيه الدورة");
titlesArray.add("الجهة المنظمة اللي عملت الدورة");
titlesArray.add("محتوى الدورة");
titlesArray.add("تقييم عام");
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
listView = (ListView)findViewById(R.id.rate_listview);
listView.setAdapter(rateAdapter);
Toast.makeText(getBaseContext() , listView.getCount()+"" , Toast.LENGTH_SHORT).show();
}
}
这是我的适配器
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class RateAdapter extends ArrayAdapter {
View listItemView;
String currentTitle;
TextView titleTV ;
Context context;
public String a ="test";
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.n, parent, false);
}
//Declaration for the current title
currentTitle = (String) getItem(position);
//Initializations //Note if radiobuttons are declared as global variables the items are missed up :D
final RadioButton radioVa = (RadioButton) listItemView.findViewById(R.id.rateitems_va);
final RadioButton radioVb = (RadioButton) listItemView.findViewById(R.id.rateitems_vb);
final RadioButton radioVc = (RadioButton) listItemView.findViewById(R.id.rateitems_vc);
final RadioButton radioVd = (RadioButton) listItemView.findViewById(R.id.rateitems_vd);
titleTV = (TextView)listItemView.findViewById(R.id.rateitems_title);
//Setting the title to the TextView
titleTV.setText(currentTitle);
//Setting oncheck listener to permit only one check
//for Va
radioVa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVc.setChecked(false);
}
}
});
//
radioVb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVc.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVb.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVb.setChecked(false);
radioVc.setChecked(false);
radioVa.setChecked(false);
}
}
});
return listItemView;
}
}
这是listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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="160dp"
android:orientation="vertical"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:id="@+id/rateitems_title"
android:gravity="center_vertical"
android:paddingRight="15dp"
tools:text="ًWhat do you think about me "
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<LinearLayout
android:orientation="vertical"
android:padding="30dp"
android:layout_below="@id/rateitems_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:background="@drawable/course_items_border_trans"
android:id="@+id/rateitems_va"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ممتاز"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="@+id/rateitems_vb"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد جدا"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rateitems_vc"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="@+id/rateitems_vd"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ضعيف"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
</LinearLayout>
</android.widget.RelativeLayout>
这是 activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout 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:orientation="vertical"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:gravity="center_vertical"
android:paddingLeft="15dp"
tools:text="List test"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<ListView
android:id="@+id/rate_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/the_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.widget.LinearLayout>
使用类似于
的模型
Class Rate{
String title;
int rate;
Rates(String title){
this.title = title;
}
public void setRate(int rate){
this.rate = rate;
}
public String getTitle(){
return this.title;
}
}
现在,在 activity 中执行如下操作:
List<Rates> rates = new ArrayList<>();
rates.add(new Rates("إيه رأيك في البني آدم اللي شرح الدورة"));
rates.add(new Rates("ايه رأيك في المكان اللي تمت فيه الدورة");
rates.add(new Rates("الجهة المنظمة اللي عملت الدورة"));
rates.add(new Rates("محتوى الدورة"));
rates.add(new Rates("تقييم عام"));
List<String> strings = new ArrayList<>(rates.size());
for (Rate rate : rates) {
strings.add(rate != null ? rate.getTitle() : null);
}
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
现在,在适配器内部 class,处理单选按钮的事件。为了将数据传回 Activity,创建了一个接口来更新各自的索引。
interface RadioClickListener{
void radioClicked(int position, int rate);
}
...
RadioClickListener radioClickListener;
...
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
radioClicked = (RadioClickListener) context;
}
因此,当您单击 ratioButton 时,相应索引处的项目会通过调用
进行更新
//pass the position and rate for that item
radioClickListener.radioClicked(position, "1");
最后实现 Activity 的接口来更新列表。
public class TempActivity extends AppCompatActivity implements RateAdapter.RadioClickListener {
...
@Override
public void radioClicked(int position, int rate){
rates.get(position).setRate(rate);
}
现在,您有了更新的费率列表,因此将其传递给另一个 activity 包裹是有意发送的。
我用适配器制作了一个列表视图,每个列表视图项包含四个单选按钮。
我想要做的是在包含 activity.And 的列表视图上有一个按钮,当我单击该按钮时,它将检索每个项目的单选按钮的所有检查状态并将它们发送到其他活动。
虽然我搜索了很多,但我不知道如何检索该数据。这是 Activity.java 文件
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.width;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.util.TypedValue.applyDimension;
/**
* Created by jack on 30/08/17.
*/
public class TempActivity extends AppCompatActivity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s7_1);
ArrayList<String> titlesArray = new ArrayList<>();
titlesArray.add("إيه رأيك في البني آدم اللي شرح الدورة");
titlesArray.add("ايه رأيك في المكان اللي تمت فيه الدورة");
titlesArray.add("الجهة المنظمة اللي عملت الدورة");
titlesArray.add("محتوى الدورة");
titlesArray.add("تقييم عام");
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
listView = (ListView)findViewById(R.id.rate_listview);
listView.setAdapter(rateAdapter);
Toast.makeText(getBaseContext() , listView.getCount()+"" , Toast.LENGTH_SHORT).show();
}
}
这是我的适配器
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
public class RateAdapter extends ArrayAdapter {
View listItemView;
String currentTitle;
TextView titleTV ;
Context context;
public String a ="test";
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.n, parent, false);
}
//Declaration for the current title
currentTitle = (String) getItem(position);
//Initializations //Note if radiobuttons are declared as global variables the items are missed up :D
final RadioButton radioVa = (RadioButton) listItemView.findViewById(R.id.rateitems_va);
final RadioButton radioVb = (RadioButton) listItemView.findViewById(R.id.rateitems_vb);
final RadioButton radioVc = (RadioButton) listItemView.findViewById(R.id.rateitems_vc);
final RadioButton radioVd = (RadioButton) listItemView.findViewById(R.id.rateitems_vd);
titleTV = (TextView)listItemView.findViewById(R.id.rateitems_title);
//Setting the title to the TextView
titleTV.setText(currentTitle);
//Setting oncheck listener to permit only one check
//for Va
radioVa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVc.setChecked(false);
}
}
});
//
radioVb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVc.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVa.setChecked(false);
radioVb.setChecked(false);
radioVd.setChecked(false);
}
}
});
radioVd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true){
radioVb.setChecked(false);
radioVc.setChecked(false);
radioVa.setChecked(false);
}
}
});
return listItemView;
}
}
这是listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.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="160dp"
android:orientation="vertical"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:id="@+id/rateitems_title"
android:gravity="center_vertical"
android:paddingRight="15dp"
tools:text="ًWhat do you think about me "
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<LinearLayout
android:orientation="vertical"
android:padding="30dp"
android:layout_below="@id/rateitems_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:background="@drawable/course_items_border_trans"
android:id="@+id/rateitems_va"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ممتاز"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="@+id/rateitems_vb"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد جدا"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rateitems_vc"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="جيد"
android:textSize="15sp"
android:paddingRight="5dp"/>
<RadioButton
android:id="@+id/rateitems_vd"
android:background="@drawable/course_items_border_trans"
android:layout_width="150dp"
android:layout_height="30dp"
android:text="ضعيف"
android:textSize="15sp"
android:paddingRight="5dp"/>
</RadioGroup>
</LinearLayout>
</android.widget.RelativeLayout>
这是 activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout 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:orientation="vertical"
android:layoutDirection="rtl"
android:background="#fff"
android:paddingBottom="10dp">
<TextView
android:gravity="center_vertical"
android:paddingLeft="15dp"
tools:text="List test"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:background="#277db3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"/>
<ListView
android:id="@+id/rate_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/the_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.widget.LinearLayout>
使用类似于
的模型Class Rate{
String title;
int rate;
Rates(String title){
this.title = title;
}
public void setRate(int rate){
this.rate = rate;
}
public String getTitle(){
return this.title;
}
}
现在,在 activity 中执行如下操作:
List<Rates> rates = new ArrayList<>();
rates.add(new Rates("إيه رأيك في البني آدم اللي شرح الدورة"));
rates.add(new Rates("ايه رأيك في المكان اللي تمت فيه الدورة");
rates.add(new Rates("الجهة المنظمة اللي عملت الدورة"));
rates.add(new Rates("محتوى الدورة"));
rates.add(new Rates("تقييم عام"));
List<String> strings = new ArrayList<>(rates.size());
for (Rate rate : rates) {
strings.add(rate != null ? rate.getTitle() : null);
}
//Initialization of Adapter and ListView and hooking them together
RateAdapter rateAdapter = new RateAdapter(this , titlesArray);
现在,在适配器内部 class,处理单选按钮的事件。为了将数据传回 Activity,创建了一个接口来更新各自的索引。
interface RadioClickListener{
void radioClicked(int position, int rate);
}
...
RadioClickListener radioClickListener;
...
public RateAdapter(Context context, ArrayList<String> arrayList) {
super(context, 0, arrayList);
this.context = context;
radioClicked = (RadioClickListener) context;
}
因此,当您单击 ratioButton 时,相应索引处的项目会通过调用
进行更新//pass the position and rate for that item
radioClickListener.radioClicked(position, "1");
最后实现 Activity 的接口来更新列表。
public class TempActivity extends AppCompatActivity implements RateAdapter.RadioClickListener {
...
@Override
public void radioClicked(int position, int rate){
rates.get(position).setRate(rate);
}
现在,您有了更新的费率列表,因此将其传递给另一个 activity 包裹是有意发送的。