从列表中选择时显示用户的相应详细信息

时间:2018-03-20 06:00:08

标签: android

我开发了一个没有的屏幕。来自数据库的人员显示在列表视图中。我想显示所选人员的个人资料页面。所以我的问题是如何将所选人员的每个细节(如姓名,联系方式等)绑定到我创建的个人资料页面?我是否必须在onItemClickListener中调用getById API?

以下是已修改代码: -

public class Test extends AppCompatActivity {

    List<Genie> genieList;
    GenieAdapter genieAdapter;


    TextView responseView;
    ProgressBar progressBar;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);


        responseView = (TextView) findViewById(R.id.responseView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        button = (Button) findViewById(R.id.test);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Test.this, "Blahblah", Toast.LENGTH_LONG).show();

                new RetrieveFeedTask().execute();

            }
        });

    }


    class RetrieveFeedTask extends AsyncTask<Void, Void, List<Genie>> {

        private Exception exception;

        protected void onPreExecute() {
            progressBar.setVisibility(View.VISIBLE);
            responseView.setText("");
        }

        protected List<Genie> doInBackground(Void... urls) {


            GenieService genieService = new GenieService();
            return genieService.getAll();
        }

        protected void onPostExecute(List<Genie> genies) {
            if (genies == null) {
                new ArrayList<Genie>(); // "THERE WAS AN ERROR"
            } else {
                progressBar.setVisibility(View.GONE);

                Log.i("INFO", genies.get(0).name);

                List<String> rows = genies.stream().map(genie -> getRow(genie)).collect(Collectors.toList());

              genieAdapter=new GenieAdapter(getApplicationContext(),R.layout.genie_list, genies);

                ListView list=(ListView)findViewById(R.id.listViewMain);
                list.setAdapter(genieAdapter);

                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(Test.this, "" + position, Toast.LENGTH_SHORT).show();
//                        if (position == 1) {
//                            startActivity(new Intent(Test.this, viewGenie1.class));
//                    }
                }
            });

            list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(Test.this, viewGenie1.class);
                    intent.putExtra("name", "%s");
                    intent.putExtra("add", "%s");
                    intent.putExtra("phn", "%s");
                    intent.putExtra("sal", "%s");
                    intent.putExtra("lea", "%s");
                    startActivity(intent);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            }
        }

        private String getRow(Genie g) {
            return String.format("%s, %s, %s, %s, %s", g.name, g.salary, g.contact, g.paid_leaves, g.address);
        }
    }
}

这里是viewGenie1.class: -

public class viewGenie1 extends AppCompatActivity implements View.OnClickListener {

    TextView name;
    EditText address, contact, salary, leaves;
    Button attendance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_genie1);

        name = (TextView) findViewById(R.id.txName);
        address = (EditText) findViewById(R.id.txAddress);
        contact = (EditText) findViewById(R.id.txContact);
        salary = (EditText) findViewById(R.id.txSalary);
        leaves = (EditText) findViewById(R.id.txLeaves);


        Button update=(Button)findViewById(R.id.btUpdate);
        update.setOnClickListener(this);

        Button delete=(Button)findViewById(R.id.delete);
        delete.setOnClickListener(this);

        Button attendance = (Button) findViewById(R.id.attendance);
        attendance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAtt();
            }
        });
        String value = "";
        if (getIntent().hasExtra("name")) {
            String name = getIntent().getExtras().getString("name");
            String add = getIntent().getExtras().getString("add");
            String phn = getIntent().getExtras().getString("phn");
            String sal = getIntent().getExtras().getString("sal");
            String lea = getIntent().getExtras().getString("lea");
        }

        name.setText(value);
        address.setText(value);
        contact.setText(value);
        salary.setText(value);
        leaves.setText(value);
    }

    @Override
    public void onClick(View view) {
        final AlertDialog.Builder builder=new AlertDialog.Builder(viewGenie1.this);
        builder.setMessage("Are you sure you want to delete records?");
        builder.setCancelable(true);
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

                new deleteTask().execute();
                Toast.makeText(viewGenie1.this, "Genie deleted..!", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(viewGenie1.this, navDrawer.class));


//                GenieService genieService=new GenieService();
//                genieService.delete(2);
//                Log.d("Information", String.valueOf(genieService.delete(2)));
                // Log.i("INFO", genies.get(0).name);
                // startActivity(new Intent(viewGenie1.this,Test.class));
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        AlertDialog alert=builder.create();
        alert.show();
    }

    private class deleteTask extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] objects) {
            GenieService genieService = new GenieService();
            return genieService.delete(6);
        }
    }

    public void showAtt() {
        Intent intent = new Intent(this, viewAbsentee.class);
        startActivity(intent);
    }
}

这是我使用硬编码值创建的配置文件页面的xml文件,但希望使用以下方法显示本地mysql数据库中的实际值 API调用: -

<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:background="@drawable/bcak"
    tools:context="com.codionics.geniem.AddGenie"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="313dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/gradientbackground"
            android:orientation="vertical">

            <ImageView
                android:layout_width="117dp"
                android:layout_height="117dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="15dp"
                android:src="@drawable/genie" />

            <TextView
                android:id="@+id/txName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="Abc"
                android:textColor="#ffffff"
                android:textSize="21sp"
                android:textStyle="bold" />
        </LinearLayout>

        <android.support.v7.widget.CardView
            android:layout_width="300dp"
            android:layout_height="115dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="175dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="2">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Contact"
                        android:textColor="#f000"
                        android:textStyle="bold"
                        android:textSize="20sp" />

                    <EditText
                        android:id="@+id/txContact"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="10dp"
                        android:text="123456789"
                        android:textColor="#3F51B5"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Address"
                        android:textColor="#f000"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <EditText
                        android:id="@+id/txAddress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="10dp"
                        android:text="Pune"
                        android:textColor="#3F51B5"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">

                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:paddingLeft="25dp">

            <ImageView
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:src="@drawable/ic_attach_money_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingLeft="20dp"
                android:text="Paid leaves : "
                android:textColor="#303F9F"
                android:textSize="27dp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/txLeaves"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_weight="1"
                android:text="  5" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:paddingLeft="25dp">

            <ImageView
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:src="@drawable/ic_money" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingLeft="20dp"
                android:text="Salary : "
                android:textColor="#303F9F"
                android:textSize="27dp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/txSalary"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:textSize="20dp"
                android:textStyle="bold"
                android:text=" 5000"
                android:textColor="#123" />
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/btUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/buttonstylegradient"
        android:text="Update Genie"
        android:textColor="#fff" />

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="80dp"
        android:layout_marginTop="-50dp"
        android:background="@drawable/buttonstylegradient"
        android:text="Delete Genie"
        android:textColor="#fff" />

    <Button
        android:id="@+id/attendance"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/buttonstylegradient"
        android:textColor="#fff"
        android:text="Attendance" />
</LinearLayout>

我想在个人资料页面中显示详细信息,如下所示: -

profile page

1 个答案:

答案 0 :(得分:0)

请使用select ID from table_1 t1 where (UNIX_TIMESTAMP(t1.Date2)-UNIX_TIMESTAMP(t1.Date1)>864000)

传递Intent中的值
putExtra()

在viewGenie1类中,你可以得到像这样的值

Intent intent = new Intent(Test.this, viewGenie1.class);
intent.putExtra("key","Value"); //Key must be unique and value should be the value which you want to pass to viewGenie1 class.
startActivity(intent);

请替换

String value="";
    if(getIntent().hasExtra("key")) {
        value = getIntent().getExtras().getString("key");
    }

String value = "";
    if (getIntent().hasExtra("name")) {
        String name = getIntent().getExtras().getString("name");
        String add = getIntent().getExtras().getString("add");
        String phn = getIntent().getExtras().getString("phn");
        String sal = getIntent().getExtras().getString("sal");
        String lea = getIntent().getExtras().getString("lea");
    }

    name.setText(value);
    address.setText(value);
    contact.setText(value);
    salary.setText(value);
    leaves.setText(value);