簡體   English   中英

我想通過隨機單擊每個文本框以另一種形式顯示數據

[英]i want to display data on another form by clicking each textbox randomly

我已經動態創建了文本框...現在我想知道如何獲取每個文本框的ID ..通過它們的ID,我想獲取數據並將其顯示為另一種形式...如果可能的話,請幫我..

public class remaind_report extends Activity {

    SQLiteDatabase db;
    LinearLayout l2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remaind_report);

        db = openOrCreateDatabase("tracker.db",
                SQLiteDatabase.CREATE_IF_NECESSARY, null);
        Toast.makeText(getApplicationContext(), "Database Is Created
                Successfully...", Toast.LENGTH_LONG).show();

                l2 = (LinearLayout)findViewById(R.id.layout_remind);

        db= SQLiteDatabase.openDatabase("sdcard/tracker.db",null,SQLiteDatabase.CREATE_IF_NECESSARY);

        try
        {
            db.execSQL("CREATE TABLE remind(renotes VARCHAR(200),date
                    TEXT,time TEXT);");
            Toast.makeText(getApplicationContext(), "Table Is Created
                    Successfully....", Toast.LENGTH_LONG).show();
        }
        catch(Exception ex)
        {
            Toast.makeText(getApplicationContext(), "Table Is Already
                    Exists.....", Toast.LENGTH_LONG).show();
        }


        try {

            Cursor c1 = db.rawQuery("select * from remind ",null);
            int theTotal= c1.getCount();
            Toast.makeText(this, "Total: "+ theTotal, 1).show();
            String[] args= {"renotes","date","time"};
            int notesCol= c1.getColumnIndex("renotes");
            int dateCol= c1.getColumnIndex("date");
            int timeCol = c1.getColumnIndex("time");
            TextView t1=null;

            while(c1.moveToNext()) {


                t1=new TextView(getApplicationContext());
                t1.setText("textbox");
                t1.setId(notesCol);
                t1.setClickable(true);
                t1.setBackgroundColor(Color.parseColor("#ff009999"));
                l2.addView(t1);

                args[0] = c1.getString(notesCol);
                args[1] = c1.getString(dateCol);
                args[2] = c1.getString(timeCol);

                t1.append("\nNotes: "+ args[0]+"\nDate: "+args[1]+"\nTime: "+args[2]+"\n");

                t1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        {

                            Toast.makeText(getApplicationContext(),"hiiii...",
                                    Toast.LENGTH_LONG).show();
                            // do stuf here
                        }

                    }
                });
            }
        }
        catch (Exception e){
            Toast.makeText(getApplicationContext(),e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }
}

動態創建的視圖不需要任何ID。

t1=new TextView(getApplicationContext());

在這里,您可以使用t1來獲取數據,例如

t1.getText("Some text").toString();
we can not get position by dynamically generated textview's...so better to use list view instead of textviews to display different records from db...

here is my edited code.

    enter code here

package com.account;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class remaind_report extends ListActivity {

    Button logout;
    SQLiteDatabase db;
    LinearLayout l2;
    ListView lv;
    TextView ans;
    String[] name;
    ImageButton home;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remaind_report);

        db = openOrCreateDatabase("tracker.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        Toast.makeText(getApplicationContext(), "Database Is Created Successfully...", Toast.LENGTH_LONG).show();

        l2 = (LinearLayout)findViewById(R.id.layout_remind);
        home = (ImageButton)findViewById(R.id.imageButton1);
        logout = (Button)findViewById(R.id.button1);

        db= SQLiteDatabase.openDatabase("sdcard/tracker.db",null,SQLiteDatabase.CREATE_IF_NECESSARY);

        try
        {
            db.execSQL("CREATE TABLE remind(renotes VARCHAR(200),date TEXT,time TEXT);");
            Toast.makeText(getApplicationContext(), "Table Is Created Successfully....", Toast.LENGTH_LONG).show();
        }
        catch(Exception ex)
        {
            Toast.makeText(getApplicationContext(), "Table Is Already Exists.....", Toast.LENGTH_LONG).show();
        }

        db= SQLiteDatabase.openDatabase("sdcard/tracker.db",null,SQLiteDatabase.CREATE_IF_NECESSARY);

        try
        {   

            //TextView nTv = new TextView(view.getContext()); 
        lv = getListView();

        //lv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
        lv.getBaseline();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        lv.setBackgroundColor(Color.TRANSPARENT);
        lv.setDividerHeight(2);
        //lv.setMinimumHeight(20);


        String col[] = new String[] {"renotes","date","time"};
        Cursor c = db.query("remind", col, null, null, null, null, null);

        name = new String[c.getCount()];
        c.moveToFirst();

        for(int i=0;i<c.getCount();i++)
        {
            name[i] = new String (c.getInt(0)+"-"+c.getString(1)+"-"+c.getString(2));
            c.moveToNext();
        }
        c.close();
        db.close();
        lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_checked,name));

         ans = (TextView)findViewById(R.id.txtans);

        }           


    catch (Exception e) 
    {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();                
    }       

    home.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntentAcc_TrackerActivity = new Intent(remaind_report.this,AccountTrackerActivity.class);
            startActivityForResult(myIntentAcc_TrackerActivity,101);
        }
    });
    logout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntentlogout = new Intent(remaind_report.this,AccountTrackerActivity.class);
            myIntentlogout.putExtra("FINISH", "ACTION.FINISH.LOGOUT");
            startActivityForResult(myIntentlogout,101);
            Toast.makeText(getApplicationContext(), "You Have Successfully Logout", 1).show();
        }
    });

 }

    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);


        Intent showreport=new    Intent(getApplicationContext(),show_trans_report.class);
        startActivity(showreport);
        Toast.makeText(getApplicationContext(), "You have clicked", 1).show();



        }
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM