是否可以将功能/参数添加到按钮?

时间:2017-05-03 09:59:39

标签: android button

您好我在android下开发了一个应用程序,在我的代码中我阻止了一个最后我想避免创建冗余页面,在我的代码上我有一个带有几个按钮的内核页面,然后显示一个列表视图,qaund on支持在一个按钮这个应用程序页面将处理信息,并在bdd上寻找它们,除了我会避免通过这么多按钮复制此页面,我想知道是否可以将参数传递给Identify按钮,或其他解决方案?

以下是我的代码,其中包含不同的按钮(我还没有放完所有按钮):

    package ddec.applicationddec;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;



/**
 * Created by Natsu on 22/04/2017.
 */

public class MaterielAssocie extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.materiel_associe);

        // creation de notre bouton pour la list view du matériel : les imprimantes
        Button btn_list_imp = (Button) findViewById(R.id.btn_list_imp);
        btn_list_imp.setOnClickListener(new Button.OnClickListener()
        {
            @Override

            public void onClick(View arg0)
            {
                Intent intent = new Intent(MaterielAssocie.this, Mat_asso_imp.class);
                startActivity(intent);
                Log.i("List imprimantes","bouton de recherche pour le matériel");
                Toast toast = Toast.makeText(getApplicationContext(),"search", Toast.LENGTH_SHORT);
                toast.show();
            }

        });

        // creation de notre bouton pour la list view du matériel : les ordinateur
        Button btn_list_ordi = (Button) findViewById(R.id.btn_list_ordi);
        btn_list_ordi.setOnClickListener(new Button.OnClickListener()
        {
            @Override

            public void onClick(View arg0)
            {
                Intent intent = new Intent(MaterielAssocie.this, Mat_asso_imp.class);
                startActivity(intent);
                Log.i("List des ordinateurs","bouton de recherche pour le matériel");
                Toast toast = Toast.makeText(getApplicationContext(),"search", Toast.LENGTH_SHORT);
                toast.show();
            }

        });



    }}

这是我的页面,它获取数据并将其放在列表视图中:

 package ddec.applicationddec;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Natsu on 22/04/2017.
 */

public class Mat_asso_imp extends Activity {
    private ListView lv;
    private ProgressDialog progressDialog;
    public static ArrayList<imprimante> detail = new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_materiel);
        lv = (ListView) findViewById(R.id.listViewMa);
        chargerJeuDessaie();
        rafraichirListView();
    }

    private void chargerJeuDessaie()
    {
        progressDialog = progressDialog.show(this,"Affichage du matériel : imprimante","Telechargement en cours");
        MonAsyncTaskHttp monAsyncTaskHttp = new MonAsyncTaskHttp() {

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                Mat_asso_imp.detail.clear();
                try {
                    JSONArray jsonArray = new JSONArray(o.toString());
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        imprimante imp = imprimante.factoryImp(jsonObject);
                        Mat_asso_imp.detail.add(imp);

                        rafraichirListView();


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                progressDialog.dismiss();
            }
        };
        monAsyncTaskHttp.execute("requete=getLesecoleetimp&etab="+ search_ville.etabSELECT.getId());
    }

    public void rafraichirListView() {
        ArrayAdapter<imprimante> imprimanteadapter = new ArrayAdapter<imprimante>(Mat_asso_imp.this, android.R.layout.simple_list_item_1, detail);
        lv.setAdapter(imprimanteadapter);
    }
}

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您可以将该类型作为额外的意图发送,这样就可以了,

Intent intent = new Intent(MaterielAssocie.this, Mat_asso_imp.class);
intent.putExtra("type","ordinateur"); // the type of material  
startActivity(intent);

并阅读其他活动的额外内容

Intent intent = getIntent();
String type = intent.getStringExtra("type");
//make your database request with type

答案 1 :(得分:0)

让您的班级实施View.onClick,然后只将您的Activity放在一边:

 @Override
 public void onClick(View arg0){
     Intent intent = new Intent(MaterielAssocie.this, Mat_asso_imp.class);
     startActivity(intent);
     Log.i("List des ordinateurs","bouton de recherche pour le matériel");
     Toast toast = Toast.makeText(getApplicationContext(),"search", Toast.LENGTH_SHORT);
         toast.show();
} 

并设置clickListeners,如下所示:

btn_list_ordi.setOnClickListener(this);
other_button.setOnClickListener(this);