从数据库中提取值进行操作并将它们保存在另一个表格中,安卓。

huangapple 未分类评论42阅读模式
英文:

Extracting value from database to do operations and save them in another table android

问题

以下是翻译好的代码部分:

public class LastActivity extends AppCompatActivity {

    TextView txt;
    Button btn;
    static MyDB myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_last);

        myDB = new MyDB(this);

        getSupportActionBar().setTitle("Final Data");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final LinearLayout lm = (LinearLayout) findViewById(R.id.lastactivity);

        btn = (Button) findViewById(R.id.subfielddata);
        txt = (TextView) findViewById(R.id.textView7);
        Bundle bundle = getIntent().getExtras();
        String date2 = bundle.getString("date1");
        txt.setText(date2);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor c = Secondlast_Activity.myDB.getAllTime();
                c.moveToFirst();

                if (c == null) {
                    Toast.makeText(getApplicationContext(), "Error!! Cursor is empty", Toast.LENGTH_SHORT).show();
                } else {

                    while (c.moveToNext()) {

                        String ename = c.getString(0);
                        int h1 = c.getInt(1);
                        int h2 = c.getInt(5);
                        int m1 = c.getInt(2);
                        int m2 = c.getInt(6);
                        int ad = c.getInt(7);
                        int wages = c.getInt(3);

                        int a, b, d;
                        int min = 0, res, hr;

                        hr = h1 - h2;

                        if (m1 > m2) {
                            a = 60 - m1;
                            m2 = a + m2;
                            min = m2;
                            hr--;
                        } else {
                            if (m1 < m2) {
                                b = m2 - m1;
                                min = b;
                            }
                        }
                        hr = hr * (-1);
                        wages = wages * hr;

                        hr = h1 - h2;

                        if (m1 > m2) {
                            a = 60 - m1;
                            m2 = a + m2;
                            min = m2;
                            hr--;
                        } else {
                            if (m1 < m2) {
                                b = m2 - m1;
                                min = b;
                            }
                        }
                        hr = hr * (-1);
                        wages = wages * hr;

                        boolean result = Secondlast_Activity.myDB.insertData(ename, hr, wages);
                        if (result) {
                            Toast.makeText(LastActivity.this, hr + " Insertion Complete", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(LastActivity.this, "Error!!", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        });

        Cursor c = LastActivity.myDB.getLastData();
        c.moveToFirst();
        if (c == null) {
            Toast.makeText(getApplicationContext(), "Cursor d is empty!!", Toast.LENGTH_SHORT).show();
        } else {
            while (c.moveToNext()) {
                final String emname = c.getString(0);
                final int totalwork = c.getInt(2);
                final int Advance = c.getInt(3);
                final int totalearn = c.getInt(4);

                LinearLayout ll = new LinearLayout(this);
                ll.setOrientation(LinearLayout.HORIZONTAL);

                TextView Name = new TextView(this);
                Name.setText(emname + "");
                Name.setWidth(250);
                ll.addView(Name);

                TextView work = new TextView(this);
                work.setText(totalwork + "");
                work.setWidth(290);
                ll.addView(work);

                TextView Ad = new TextView(this);
                Ad.setText(Advance + "");
                Ad.setWidth(290);
                ll.addView(Ad);

                TextView earn = new TextView(this);
                earn.setText(totalearn + "");
                earn.setWidth(250);
                ll.addView(earn);

                Name.setLayoutParams(params);
                lm.addView(ll);
            }
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return super.onSupportNavigateUp();
    }
}

请注意,以上翻译仅包含代码部分,未包含任何附加信息。

英文:

This is my view activity and in which I want to extract data from another table and save operated data to another table. The problem is the data is adding again and again, and in the view activity, the same data is repeating and filling up the whole space.

public class LastActivity extends AppCompatActivity {

    TextView txt;
    Button btn;
    static MyDB myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_last);

        myDB = new MyDB(this);

        getSupportActionBar().setTitle(&quot;Final Data&quot;);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final LinearLayout lm = (LinearLayout) findViewById(R.id.lastactivity);

        btn = (Button) findViewById(R.id.subfielddata);
        txt = (TextView) findViewById(R.id.textView7);
        Bundle bundle = getIntent().getExtras();
        String date2 = bundle.getString(&quot;date1&quot;);
        txt.setText(date2);


        //create the layout parameters
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor c = Secondlast_Activity.myDB.getAllTime();
                c.moveToFirst();

                if (c == null) {
                    Toast.makeText(getApplicationContext(), &quot;Error!! Cursor is empty&quot;, Toast.LENGTH_SHORT).show();
                } else {

                    //create all variants to fetch data from database
                    while (c.moveToNext()) {

                        String ename = c.getString(0);
                        int h1 = c.getInt(1);
                        int h2 = c.getInt(5);
                        int m1 = c.getInt(2);
                        int m2 = c.getInt(6);
                        int ad = c.getInt(7);
                        int wages = c.getInt(3);

                        int a, b, d;
                        int min = 0, res, hr;

                        //oode for getting hours from edit text
                        hr = h1 - h2;

                        //code for converting values to edit text value to minutes
                        if (m1 &gt; m2) {
                            a = 60 - m1;
                            m2 = a + m2;
                            min = m2;
                            hr--;
                        } else {
                            if (m1 &lt; m2) {
                                b = m2 - m1;
                                min = b;
                            }
                        }
                        hr = hr * (-1);
                        wages = wages * hr;

                        //oode for getting hours from edit text
                        hr = h1 - h2;

                        //code for converting values to edit text value to minutes
                        if (m1 &gt; m2) {
                            a = 60 - m1;
                            m2 = a + m2;
                            min = m2;
                            hr--;
                        } else {
                            if (m1 &lt; m2) {
                                b = m2 - m1;
                                min = b;
                            }
                        }
                        hr = hr * (-1);
                        wages = wages * hr;

                        boolean result = Secondlast_Activity.myDB.insertData(ename,hr, wages);
                        if (result) {
                            Toast.makeText(LastActivity.this, hr + &quot; Insertion Complete&quot;, Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(LastActivity.this, &quot;Error!!&quot;, Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        });

        //creating cursor for database to let data in count
        Cursor c = LastActivity.myDB.getLastData();
            c.moveToFirst();
            if (c == null) {
                Toast.makeText(getApplicationContext(), &quot;Cursor d is empty!!&quot;, Toast.LENGTH_SHORT).show();
            } else {
                //create all variants to fetch data from database
                while (c.moveToNext()) {
                    final String emname = c.getString(0);
                    final int totalwork = c.getInt(2);
                    final int Advance = c.getInt(3);
                    final int totalearn = c.getInt(4);

                    //create linear layout
                    LinearLayout ll = new LinearLayout(this);
                    ll.setOrientation(LinearLayout.HORIZONTAL);

                    //create textView to show data
                    TextView Name = new TextView(this);
                    Name.setText(emname + &quot;&quot;);
                    Name.setWidth(250);
                    ll.addView(Name);

                    TextView work = new TextView(this);
                    work.setText(totalwork + &quot;&quot;);
                    work.setWidth(290);
                    ll.addView(work);

                    TextView Ad = new TextView(this);
                    Ad.setText(Advance + &quot;&quot;);
                    Ad.setWidth(290);
                    ll.addView(Ad);

                    TextView earn = new TextView(this);
                    earn.setText(totalearn + &quot;&quot;);
                    earn.setWidth(250);
                    ll.addView(earn);

                   Name.setLayoutParams(params);
                   lm.addView(ll);
                }
            }
        }
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return super.onSupportNavigateUp();
    }
}

huangapple
  • 本文由 发表于 2020年5月4日 05:58:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61582184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定