0

我已经动态创建了文本框...现在我想知道如何获取每个文本框的 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();
        }
    }
}
4

2 回答 2

0

动态创建的视图不需要任何 id。

t1=new TextView(getApplicationContext());

在这里使用 t1 您可以获取数据,例如

t1.getText("Some text").toString();
于 2013-04-12T17:04:09.333 回答
0
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();



        }
}   
于 2013-04-18T07:35:04.033 回答