0

我想ListView用可点击的元素填充 a 。现在我有一个TextView,但我不能点击每个项目,因为它是一个附加项。我能怎么做?我想看看ListView带有可点击元素的。现在它将打开一个 Toast。这是代码:

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

        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();

            }
        });

        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); 
         if(position == 0){
            CharSequence text = "Workflow scelto!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(getApplicationContext(), text, duration);
            toast.show();
         c.requery(); 
        }}
               });

        //adapter.notifyDataSetChanged();

        /*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);
    }

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scegli il Workflow da testare:"
        android:gravity="center" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Workflow di default" />

    <TextView
        android:id="@+id/masterTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Master" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nuovi Workflow" />

    <TextView
        android:id="@+id/wfsTv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textStyle="bold"
        android:onClick=""
        android:padding="20dp"
        android:paddingBottom="10dp"/>

    <ListView
        android:id="@+id/wfsLv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="0dp"   
    ></ListView>
</LinearLayout>

我有一个 Textview 和一个 ListView .. 但我只想要一个 ListView 并且在每一行中都有一个修改过的 TextView .. 不是 2 东西分开.. 像这段代码..

4

1 回答 1

0

试试这个,而不是你的SimpleCursorAdapter. 所以,替换SimpleCursorAdapter adapter = ...MyAdapter adapter = new MyAdapter(this);

这是适配器:

public class MyAdapter extends BaseAdapter {

    private Activity thisContext = null;

    private ArrayList<String> myList = null;

    /**
     * MyAdapter Constructor.
     * @param c Context
     */
    public MyAdapter(Activity c) {

        this.myList = new ArrayList<String>();
        this.thisContext = c;

            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));

            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);

            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 {
                            myList.add("Wf Name:"+c.getString(nameCol)+", Class:"+c.getString(classCol)); //estrazione dei dati dalla entry del cursor
                        } while (c.moveToNext());//iteriamo al prossimo elemento
            }

        db.close();

    } //End MyAdapter()

    public int getCount() {
        return myList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    /**
     * Create a new View for each item referenced by the Adapter. 
     * @return The View created.
     */
    public View getView(final int position, View convertView, ViewGroup parent) {

        //Layout inflated from XML
        LayoutInflater inflater = context.getLayoutInflater();
        View MyView = inflater.inflate(R.layout.wfsTv_item, null, true);

        //Text to be shown in list
        TextView wfsTv = (TextView) MyView.findViewById(R.id.wfsTv);
        wfsTv.setText(myList.get(position));

        MyView.setClickable(true);

        //Set the click action for the list
        MyView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                        CharSequence text = "Workflow scelto!";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(thisContext, text, duration);
                        toast.show();

            }
        }); //End setOnClickListener()

        return MyView;
    } //End getView()

}

和布局 XML 文件wfsTv_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/wfsTv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textStyle="bold"
        android:onClick=""
        android:padding="20dp"
        android:paddingBottom="10dp"/>

</LinearLayout>

我没有测试过这个,所以它可能有一些问题。此外,您可以更改布局并添加任意数量的内容。请记住,OnClickListener仅应用于项目,而不是项目的单个子项(如果有)。

于 2012-05-15T16:23:28.917 回答