将回收卡视图中的数据导入我们的主要活动

时间:2017-07-06 15:49:49

标签: android android-recyclerview cardview

//从卡中获取数据,但使用viewholder获取randon数据 //如何获得正确和准确的数据。

//这是我用于回收站视图的所有代码     1.列出项目             package com.example.jains.attendencemanagementsystem;

/**
 *  Created by jains on 6/28/2017.
 */
        import android.content.Context;
        import android.support.design.widget.Snackbar;
        import android.support.v7.widget.RecyclerView;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;

        import java.util.List;

        import static com.example.jains.attendencemanagementsystem.ViewHolder.IdTextView;
        import static com.example.jains.attendencemanagementsystem.ViewHolder.NameTextView;

/**
 * Created by JUNED on 6/16/2016.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

    Context context;
    RadioGroup myradiogroup;
    RadioButton mySelectedRadioButton;

    private RadioButton lastCheckedRB = null;

    List<GetDataAdapter> getDataAdapter;


    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

        super();

        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview, parent, false);

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

        NameTextView.setText(getDataAdapter1.getName());

        IdTextView.setText(String.valueOf(getDataAdapter1.getId()));
        //  holder.status.setText(String.valueOf());


        /**  holder.setButtonClickListener(new ButtonClickListener() {
        @Override
        public void onButtonClick(View v, int pos) {
        Snackbar.make(v, "Student " + getDataAdapter.get(pos).getName() + getDataAdapter.get(pos).getId() + " is selected", Snackbar.LENGTH_SHORT).show();
        }
        });
         **/
        View.OnClickListener rbClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton checked_rb = (RadioButton) v;
                if (lastCheckedRB != null) {
                    lastCheckedRB.setChecked(false);
                }
                lastCheckedRB = checked_rb;
            }
        };

        /**   View.OnClickListener save =new View.OnClickListener() {

        @Override public void onClick(View v) {
        String date = dateView.getText().toString();

        String mySelectedText = mySelectedRadioButton.getText().toString();

        String a=getDataAdapter1.getName().toString();
        String b=String.valueOf(getDataAdapter1.getId());


        String type = "take";
        BackgroundWorker_attendance_submit backgroundWorker = new BackgroundWorker_attendance_submit(context);
        backgroundWorker.execute(type, a, b, mySelectedText, date);


        }
         **/

        holder.P.setOnClickListener(rbClick);
        holder.A.setOnClickListener(rbClick);
        holder.L.setOnClickListener(rbClick);
        holder.S.setOnClickListener(rbClick);
        // holder.button0.setOnClickListener(save);

        //CLICKED
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Snackbar.make(v, "Student " + getDataAdapter.get(pos).getName() + getDataAdapter.get(pos).getId() + " is selected", Snackbar.LENGTH_SHORT).show();
            }
        });

    }
    /**

     cardView.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View view) {
    Intent i = new Intent (view.getContext(), take_attendance.class);
    i.putExtra("nn",NameTextView.getText().toString());
    i.putExtra("ii",IdTextView.getText());
    view.getContext().startActivity(i);
    }
    });



     **/
    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

}


package com.example.jains.attendencemanagementsystem;

/**
 * Created by jains on 6/24/2017.
 */


        import android.app.AlertDialog;
        import android.content.Context;
        import android.os.AsyncTask;

        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.io.OutputStream;
        import java.io.OutputStreamWriter;
        import java.net.HttpURLConnection;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.net.URLEncoder;

/**
 * Created by jains on 26//2017.
 */
public class BackgroundWorker_attendance_submit extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;

    BackgroundWorker_attendance_submit(Context ctx) {
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.43.216/a_record_submit.php";
        if(type.equals("take")) {
            try {
                int University_roll = Integer.parseInt(params[1]);

                String student_name= params[2];
                String  status=params[3];
                String date =params[4];

                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("University_roll","UTF-8")+"="+URLEncoder.encode(String.valueOf(University_roll),"UTF-8")+"&"
                        +URLEncoder.encode("student_name","UTF-8")+"="+URLEncoder.encode(student_name,"UTF-8")+"&"
                        +URLEncoder.encode("status","UTF-8")+"="+URLEncoder.encode(String.valueOf(status),"UTF-8")+"&"
                        +URLEncoder.encode("date","UTF-8")+"="+URLEncoder.encode(date,"UTF-8");

                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }



    @Override
    protected void onPreExecute() {

        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Registration Status...");

    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();


    }



    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);



    }
}`package com.example.jains.attendencemanagementsystem;

/**
 * Created by jains on 7/1/2017.
 */
        import android.view.View;
/**
 * Created by Hp on 3/18/2016.
 */
public interface ItemClickListener {
    void onItemClick(View v,int pos);
}`package com.example.jains.attendencemanagementsystem;

        import android.support.v7.widget.CardView;
        import android.support.v7.widget.RecyclerView;
        import android.view.View;
        import android.widget.Button;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;
        import android.widget.TextView;

/**
 * Created by jains on 7/2/2017.
 */

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ItemClickListener itemClickListener;
    ButtonClickListener buttonClickListener;


    static TextView IdTextView;

    static TextView NameTextView;


    static RadioButton P, A, L, S;
    static Button button11;
    RadioGroup myradiogroup;
    RadioButton mySelectedRadioButton;

    static CardView cardView;
    public ViewHolder(View itemView) {

        super(itemView);

        IdTextView = (TextView) itemView.findViewById(R.id.textView2);
        NameTextView = (TextView) itemView.findViewById(R.id.textView4);
        itemView.setOnClickListener(this);
        P = (RadioButton) itemView.findViewById(R.id.checkBoxP);
        A = (RadioButton) itemView.findViewById(R.id.checkBoxA);
        L = (RadioButton) itemView.findViewById(R.id.checkBoxL);
        S = (RadioButton) itemView.findViewById(R.id.checkBoxS);

        //this button is submit attendance button in card

        button11 = (Button) itemView.findViewById(R.id.button11);


        //these are the 4 radio button and radio group
        myradiogroup = (RadioGroup) itemView.findViewById(R.id.myradiogroup);
        int id = myradiogroup.getCheckedRadioButtonId();
        mySelectedRadioButton = (RadioButton) itemView.findViewById(id);

        cardView = (CardView)itemView.findViewById(R.id.cardview1);

    }

    @Override
    public void onClick(View v) {
        this.itemClickListener.onItemClick(v, getLayoutPosition());

    }


    public void setItemClickListener(ItemClickListener ic) {
        this.itemClickListener = ic;
    }

    /** @Override
    public void save(View view)
    {
    this.buttonClickListener.onButtonClick(view ,getLayoutPosition());
    }

    public void setButtonClickListener(ButtonClickListener bc)
    {
    this.buttonClickListener=bc;
    }
     **/
}   /**

 @Override
 public void onClick(View v) {

 **/



package com.example.jains.attendencemanagementsystem;

/**
 * Created by jains on 6/28/2017.
 */

        import android.content.Context;
        import android.support.design.widget.Snackbar;
        import android.support.v7.widget.RecyclerView;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;

        import java.util.List;

        import static com.example.jains.attendencemanagementsystem.ViewHolder.IdTextView;
        import static com.example.jains.attendencemanagementsystem.ViewHolder.NameTextView;

/**
 1. Created by JUNED on 6/16/2016.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

    Context context;
    RadioGroup myradiogroup;
    RadioButton mySelectedRadioButton;

    private RadioButton lastCheckedRB = null;

    List<GetDataAdapter> getDataAdapter;


    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

        super();

        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview, parent, false);

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

        NameTextView.setText(getDataAdapter1.getName());

        IdTextView.setText(String.valueOf(getDataAdapter1.getId()));
        //  holder.status.setText(String.valueOf());


        /**  holder.setButtonClickListener(new ButtonClickListener() {
        @Override
        public void onButtonClick(View v, int pos) {
        Snackbar.make(v, "Student " + getDataAdapter.get(pos).getName() + getDataAdapter.get(pos).getId() + " is selected", Snackbar.LENGTH_SHORT).show();
        }
        });
         **/
        View.OnClickListener rbClick = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton checked_rb = (RadioButton) v;
                if (lastCheckedRB != null) {
                    lastCheckedRB.setChecked(false);
                }
                lastCheckedRB = checked_rb;
            }
        };

        /**   View.OnClickListener save =new View.OnClickListener() {

        @Override public void onClick(View v) {
        String date = dateView.getText().toString();

        String mySelectedText = mySelectedRadioButton.getText().toString();

        String a=getDataAdapter1.getName().toString();
        String b=String.valueOf(getDataAdapter1.getId());


        String type = "take";
        BackgroundWorker_attendance_submit backgroundWorker = new BackgroundWorker_attendance_submit(context);
        backgroundWorker.execute(type, a, b, mySelectedText, date);


        }
         **/
        holder.P.setOnClickListener(rbClick);
        holder.A.setOnClickListener(rbClick);
        holder.L.setOnClickListener(rbClick);
        holder.S.setOnClickListener(rbClick);
        // holder.button0.setOnClickListener(save);

        //CLICKED
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Snackbar.make(v, "Student " + getDataAdapter.get(pos).getName() + getDataAdapter.get(pos).getId() + " is selected", Snackbar.LENGTH_SHORT).show();
            }
        });

    }
    /**

     cardView.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View view) {
    Intent i = new Intent (view.getContext(), take_attendance.class);
    i.putExtra("nn",NameTextView.getText().toString());
    i.putExtra("ii",IdTextView.getText());
    view.getContext().startActivity(i);
    }
    });



     **/

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

}

2. List item

        package com.example.jains.attendencemanagementsystem;

        import android.app.DatePickerDialog;
        import android.app.Dialog;
        import android.content.Context;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.support.design.widget.Snackbar;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.LinearLayoutManager;
        import android.support.v7.widget.RecyclerView;
        import android.view.View;
        import android.widget.Button;
        import android.widget.DatePicker;
        import android.widget.ProgressBar;
        import android.widget.RadioButton;
        import android.widget.RadioGroup;
        import android.widget.TextView;

        import com.android.volley.RequestQueue;
        import com.android.volley.Response;
        import com.android.volley.VolleyError;
        import com.android.volley.toolbox.JsonArrayRequest;
        import com.android.volley.toolbox.Volley;

        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import java.util.ArrayList;
        import java.util.Calendar;
        import java.util.List;

public class take_attendance extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;

    RecyclerView recyclerView;

    RecyclerView.LayoutManager recyclerViewlayoutManager;

    RecyclerView.Adapter recyclerViewadapter;

    ProgressBar progressBar;

    TextView uni,name;

    RadioButton radioButton_P,radioButton_A,radioButton_L,radioButton_S;

    private DatePicker datePicker;
    private Calendar calendar;
    private TextView dateView;
    private int year, month, day;


//    SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
    //  String t_name= sharedPreferences.getString(Config.NAME_SHARED_PREF,"Not Available");

    //TextView textView=(TextView)findViewById(R.id.showuser);
    //

    String GET_JSON_DATA_HTTP_URL = "http://192.168.43.216/fetch_stud.php?t_name=";

    String JSON_STUDENT_NAME = "student_name";
    String JSON_UNIVERSITY_ROLL = "University_roll";



    Button button0;

    JsonArrayRequest jsonArrayRequest ;

    RequestQueue requestQueue ;

    RadioButton mySelectedRadioButton;
    RadioGroup myradiogroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.take_attendance);

        //uni=(TextView)findViewById(R.id.textView2);
        //name=(TextView)findViewById(R.id.textView4);
        GetDataAdapter1 = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        button0 = (Button)findViewById(R.id.button0) ;

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(recyclerViewlayoutManager);

        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //button0.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.VISIBLE);


                JSON_DATA_WEB_CALL();

            }
        });


        dateView = (TextView) findViewById(R.id.textView3);
        calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);

        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        showDate(year, month+1, day);


    }


    public void save(View view)
    {
        saving();
    }


    private void saving()
    {

        // I want to get Student name and University Roll grom the card content which button is clicked by the user

        String a=  ViewHolder.IdTextView.getText().toString();
        String b= ViewHolder.NameTextView.getText().toString();


        String date = dateView.getText().toString();
        myradiogroup=(RadioGroup)findViewById(R.id.myradiogroup);

        int id = myradiogroup.getCheckedRadioButtonId();
        mySelectedRadioButton = (RadioButton) findViewById(id);
        String mySelectedText = mySelectedRadioButton.getText().toString();



        String type = "take";
        BackgroundWorker_attendance_submit backgroundWorker = new BackgroundWorker_attendance_submit(this);
        backgroundWorker.execute(type, a,b,mySelectedText,date);

        //Intent y= new Intent(register_main.this,teacher.class);
        //startActivity(y);
        // Toast.makeText(register_main.this, "Successfully Registered...!!!", Toast.LENGTH_LONG).show();

    }

    public void JSON_DATA_WEB_CALL(){

        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        final String aa= sharedPreferences.getString(Config.NAME_SHARED_PREF,"Not Available");


        final String a= GET_JSON_DATA_HTTP_URL.concat(aa);
        jsonArrayRequest = new JsonArrayRequest(a,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {


                        progressBar.setVisibility(View.GONE);
                        // button0.setVisibility(View.GONE);
                        Snackbar.make(recyclerView,"Note--> Take Attendance Of All Students ",1000000000).setDuration(1000000000).show();

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

        for(int i = 0; i<array.length(); i++) {

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setId(json.getInt(JSON_UNIVERSITY_ROLL));

                GetDataAdapter2.setName(json.getString(JSON_STUDENT_NAME));



            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }
//DATE PICKER FUNCTIONS


    @SuppressWarnings("deprecation")
    public void setDate(View view) {
        showDialog(999);

    }


    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == 999) {
            return new DatePickerDialog(this,
                    myDateListener, year, month, day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener myDateListener = new
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker arg0,
                                      int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                    // arg1 = year
                    // arg2 = month
                    // arg3 = day
                    showDate(arg1, arg2+1, arg3);
                }
            };

    private void showDate(int year, int month, int day) {
        dateView.setText(new StringBuilder().append(day).append("/")
                .append(month).append("/").append(year));
    }

}

0 个答案:

没有答案