Java 在每个项目上创建一个带有onClick侦听器的列表

Java 在每个项目上创建一个带有onClick侦听器的列表,java,android,Java,Android,我有一个SQLitedb,我想在列表视图中查看所有元素。但我想让每一行都可以点击。如何做到这一点?代码如下: 然后。。另一个问题。。我有一个活动可以通过选项菜单启动,它可以从数据库中添加或删除数据。。我想在返回此活动时自动更新列表。。如何更改代码 public class WorkflowChoice extends Activity { private static final int INIT_JADE_PROFILE = 0; private static final in

我有一个
SQLite
db,我想在列表视图中查看所有元素。但我想让每一行都可以点击。如何做到这一点?代码如下: 然后。。另一个问题。。我有一个活动可以通过选项菜单启动,它可以从数据库中添加或删除数据。。我想在返回此活动时自动更新列表。。如何更改代码

public class WorkflowChoice extends Activity
{
    private static final int INIT_JADE_PROFILE = 0;
    private static final int MANAGE_DATABASE = 1;
    private static final int MODIFY_FILES = 2;
    private static final int CHANGE_THEME = 3;
    private static final int SHOW_OUTPUT_WORKFLOW = 4;
    private LinearLayout properties_container;
    private MyDatabase db;
    TextView wfsTv;
    ListView wfsLv;
    private Cursor c;
    public MyDatabase getDb()
    {
        return db;
    }
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choice);
        properties_container = (LinearLayout ) findViewById(R.id.properties_container);
        String host = (String) InitJadeProperties.retrieve(this, getString(R.string.main_container_host), getString(R.string.default_host));
        String port = (String) InitJadeProperties.retrieve(this, getString(R.string.main_container_port), getString(R.string.default_port));
        wfsTv=(TextView)findViewById(R.id.wfsTv);
        ListView wfsLv = (ListView)findViewById(R.id.wfsLv);
        db=new MyDatabase(getApplicationContext());
        db.open();
        //apriamo il db
        if(db.fetchWfs().getCount()==0)
        {
            //inserimento dati, solo se il db è vuoto
            db.insertWf("WF1", "class1");
            db.insertWf("WF2", "class2");
            db.insertWf("WF3", "class3");
            db.insertWf("WF4", "class4");
            db.insertWf("WF5", "class5");
        }
        c=db.fetchWfs();
        // query
        startManagingCursor(c);
        SimpleCursorAdapter adapter=new SimpleCursorAdapter( //semplice adapter per i cursor                 this,                  R.layout.wfs, 
        //il layout di ogni riga/prodotto c,new String[]
        {
            MyDatabase.WfMetaData.ID,MyDatabase.WfMetaData.WF_NAME_KEY,MyDatabase.WfMetaData.WF_CLASS_KEY
        }
        ,//questi colonne 
        new int[]{R.id.IDTv,R.id.nameTv,R.id.classTv }            );
        //in queste views 
        wfsLv.setAdapter(adapter);
        //la listview ha questo adapter //qui vediamo invece come reperire i dati e usarli, in questo caso li stampiamo in una textview
        int nameCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_NAME_KEY);
        //indici delle colonne
        int classCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_CLASS_KEY);
        if(c.moveToFirst())
        {
            //se va alla prima entry, il cursore non è vuoto         do
            {
                wfsTv.append("Wf Name:"+c.getString(nameCol)+", Class:"+c.getString(classCol)+"\n");
                //estrazione dei dati dalla entry del cursor
            }
            while (c.moveToNext());
            //iteriamo al prossimo elemento
        }
        db.close();
        getWindow().setFormat(PixelFormat.RGBA_8888);
        //visto che usiamo i gradient, usiamo questo trick (vedi snippet forum) getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
        //wfsLv.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]
        {
            Color.RED,Color.parseColor("#f2bf26")
        }
        ));
        //wfsTv.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]
        {
            Color.RED,Color.parseColor("#f2bf26")
        }
        ));
        //definizione ed uso di gradient in modo programmatico //animazioni in modo programmatico (vedi snippet forum) Animation a1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        a1.setDuration(1000);
        a1.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));
        wfsLv.startAnimation(a1);
        //entra da sotto Animation a2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        a2.setDuration(1000);
        a2.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));
        wfsTv.startAnimation(a2);
        //entra da sopra wfsLv.setClickable(true);
        //e affidiamo la gestione del tap/click ad un apposito listener, che ci permetterà di agire sull’elemento cliccato e ricaricare la nostra lista wfsLv.setOnItemClickListener        (new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<
            ?>
            parent,           View v, int position, long id)
            {
                TextView txtId = (TextView)           v.findViewById(R.id.wfsTv);
                c.requery();
            }
        }
        );
        /*wfsTv.setOnClickListener(new View.OnClickListener()
        {
            @Override         public void onClick(View v)
            {
                CharSequence text = "Workflow scelto!";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(getApplicationContext(), text, duration);
                toast.show();
            }
        }
        );
        */     TextView masterTv = (TextView)findViewById(R.id.masterTv);
        masterTv.setText("Master");
        masterTv.setOnClickListener(new View.OnClickListener()
        {
            @Override         public void onClick(View v)
            {
                startSubActivity();
            }
        }
        );
    }
    private void startSubActivity()
    {
        Intent intent = new Intent(this, ConfigChoice.class);
        startActivity(intent);
    }
}
编辑

创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.choice);
properties\u container=(LinearLayout)findViewById(R.id.properties\u container);
String host=(String)InitJadeProperties.retrieve(this,getString(R.String.main_container_host),getString(R.String.default_host));
String port=(String)InitJadeProperties.retrieve(this,getString(R.String.main_容器_port),getString(R.String.default_port));
wfsTv=(TextView)findViewById(R.id.wfsTv);
ListView wfsLv=(ListView)findViewById(R.id.wfsLv);
db=新的MyDatabase(getApplicationContext());
db.open();//Aprio il db
如果(db.fetchWfs()
db.insertWf(“WF1”、“class1”);
db.insertWf(“WF2”,“class2”);
db.insertWf(“WF3”,“class3”);
db.insertWf(“WF4”,“class4”);
db.insertWf(“WF5”、“class5”);
}
c=db.fetchWfs();//查询
开始管理光标(c);
SimpleCursorAdapter=新的SimpleCursorAdapter(//semplice adapter per i cursor
这
R.layout.wfs,//il layout di ogni riga/prodotto
C
新字符串[]{MyDatabase.WfMetaData.ID,MyDatabase.WfMetaData.WF_NAME_KEY,MyDatabase.WfMetaData.WF_CLASS_KEY},//questi colonne
新建int[]{R.id.IDTv,R.id.nameTv,R.id.classTv});//在queste视图中
wfsLv.setAdapter(适配器);//la listview ha questo适配器
adapter.notifyDataSetChanged();
//在《联合国文本视图》中的《我和你的故事》中,我听到了一段视频
int nameCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_NAME_KEY);//indici delle colonne
int classCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_CLASS_KEY);
如果(c.moveToFirst()){//se va alla prima entry,il cursore nonèvooto
做{
wfsTv.append(“Wf Name:+c.getString(nameCol)+”,Class:+c.getString(classCol)+“\n”);//estrazione dei dati dalla entry del cursor
}while(c.moveToNext());//iteriamo al-prossimo elemento
}
db.close();
getWindow().setFormat(PixelFormat.RGBA_8888);//visto che usiamo i gradient,usiamo questo trick(视频片段论坛)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_抖动);
//wfsLv.setBackgroundDrawable(新的GradientDrawable(GradientDrawable.Orientation.BOTTOM_-TOP,新的int[]{Color.RED,Color.parseColor(#f2bf26”)});
//wfsTv.setBackgroundDrawable(新的GradientDrawable(GradientDrawable.Orientation.TOP_-BOTTOM,新的int[]{Color.RED,Color.parseColor(“#f2bf26”)});
//在modo程序中定义uso di梯度
//modo programmatico中的animazioni(视频片段论坛)
Animation a1=新的TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_SELF,0.0f);
a1.设定持续时间(1000);
a1.setInterpolator(AnimationUtils.loadInterpolator(这个是android.R.anim.Decreate_interpolator));
wfsLv.startAnimation(a1);
//索托中心酒店
Animation a2=新的TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_PARENT,-1.0f,Animation.RELATIVE_TO_SELF,0.0f);
a2.设定持续时间(1000);
a2.setInterpolator(AnimationUtils.loadInterpolator(这个是android.R.anim.decreate_interpolator));
wfsTv.startAnimation(a2);
//索普拉中心酒店
wfsLv.setClickable(真);
//点击/点击按钮的位置与听众的位置一致,点击按钮的位置与听众的位置一致
wfsLv.setOnItemClickListener
(新AdapterView.OnItemClickListener(){
公共无效MClick(AdapterView父级、,
视图v,内部位置,长id){
//TextView txtId=(TextView)v.findViewById(R.id.wfsTv);
CharSequence text=“工作流scelto!”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(getApplicationContext(),text,duration);
toast.show();
c、 重新查询();
}
});
protected void onCreate(Bundle savedInstanceState) {

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

        properties_container = (LinearLayout ) findViewById(R.id.properties_container);

        String host = (String) InitJadeProperties.retrieve(this, getString(R.string.main_container_host), getString(R.string.default_host));
        String port = (String) InitJadeProperties.retrieve(this, getString(R.string.main_container_port), getString(R.string.default_port));

        wfsTv=(TextView)findViewById(R.id.wfsTv);
        ListView wfsLv = (ListView)findViewById(R.id.wfsLv);

        db=new MyDatabase(getApplicationContext());
        db.open();  //apriamo il db


        if(db.fetchWfs().getCount()==0){//inserimento dati, solo se il db è vuoto

                db.insertWf("WF1", "class1");
                db.insertWf("WF2", "class2");
                db.insertWf("WF3", "class3");
                db.insertWf("WF4", "class4");
                db.insertWf("WF5", "class5"); 

        }

        c=db.fetchWfs(); // query
        startManagingCursor(c);

        SimpleCursorAdapter adapter=new SimpleCursorAdapter( //semplice adapter per i cursor
                        this, 
                        R.layout.wfs, //il layout di ogni riga/prodotto 
                        c, 
                        new String[]{MyDatabase.WfMetaData.ID,MyDatabase.WfMetaData.WF_NAME_KEY,MyDatabase.WfMetaData.WF_CLASS_KEY},//questi colonne 
                        new int[]{R.id.IDTv,R.id.nameTv,R.id.classTv});//in queste views

        wfsLv.setAdapter(adapter); //la listview ha questo adapter

        adapter.notifyDataSetChanged();
        //qui vediamo invece come reperire i dati e usarli, in questo caso li stampiamo in una textview

        int nameCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_NAME_KEY);  //indici delle colonne
        int classCol=c.getColumnIndex(MyDatabase.WfMetaData.WF_CLASS_KEY);       

        if(c.moveToFirst()){  //se va alla prima entry, il cursore non è vuoto
                do {

                        wfsTv.append("Wf Name:"+c.getString(nameCol)+", Class:"+c.getString(classCol)+"\n"); //estrazione dei dati dalla entry del cursor

                        } while (c.moveToNext());//iteriamo al prossimo elemento
        }

        db.close();
        getWindow().setFormat(PixelFormat.RGBA_8888);   //visto che usiamo i gradient, usiamo questo trick (vedi snippet forum)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);  

        //wfsLv.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{Color.RED,Color.parseColor("#f2bf26")}));
        //wfsTv.setBackgroundDrawable(new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.RED,Color.parseColor("#f2bf26")}));
        //definizione ed uso di gradient in modo programmatico


        //animazioni in modo programmatico (vedi snippet forum)
        Animation a1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        a1.setDuration(1000);
        a1.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));
        wfsLv.startAnimation(a1);
        //entra da sotto


        Animation a2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        a2.setDuration(1000);
        a2.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator));
        wfsTv.startAnimation(a2);
        //entra da sopra


        wfsLv.setClickable(true);
        //e affidiamo la gestione del tap/click ad un apposito listener, che ci permetterà di agire sull’elemento cliccato e ricaricare la nostra lista

        wfsLv.setOnItemClickListener
               (new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,
                  View v, int position, long id) {
         //TextView txtId = (TextView)v.findViewById(R.id.wfsTv); 
         CharSequence text = "Workflow scelto!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(getApplicationContext(), text, duration);
            toast.show();
         c.requery(); 
        }
               });
listview.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       //---> arg2 <-- will give you row number
       // your code goes here
    }
});
adapter.notifyDataSetChanged();