从ListView项检索数据

时间:2017-09-08 09:18:18

标签: java android listview adapter

我用适配器创建了一个listview,每个listview项包含四个RadioButtons。

我想要做的是在listview上有一个包含活动的按钮。当我点击该按钮时,它将检索每个项目的所有关键按钮的检查状态,并将它们发送到其他活动。

但我不知道如何检索这些数据,尽管我经常搜索。这是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>

1 个答案:

答案 0 :(得分:0)

使用类似

的模型
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;
    }
}

现在,内部活动的行为如下:

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);

现在,在适配器类内部,处理单选按钮的事件。要将数据传递回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的接口以更新List。

public class TempActivity extends AppCompatActivity implements RateAdapter.RadioClickListener {
...

@Override
public void radioClicked(int position, int rate){
    rates.get(position).setRate(rate);
}

现在,您已获得更新的费率列表,因此要将其传递给另一个活动包裹并具有意图并发送。