如何通过 onItemClickListener 在不同的类( ConsumerDescAndEdit.java )中使用某个类的变量?

Posted

技术标签:

【中文标题】如何通过 onItemClickListener 在不同的类( ConsumerDescAndEdit.java )中使用某个类的变量?【英文标题】:How do I use variables of some class in a different class ( ConsumerDescAndEdit.java ) through onItemClickListener? 【发布时间】:2019-08-22 11:50:01 【问题描述】:

我需要在EditTextConsumerdescAndEdit 中显示namephoneNumberaccountNumber,那么在执行OnItemClickListener 时如何传递这些值?

ShowAll.java

我有从CustomAdapter 膨胀的ListView

Intent intent = new Intent(ShowAll.this,ConsumerDescAndEdit.class);
startActivity(intent);     

CustomAdapter.java:


public class CustomAdapter extends BaseAdapter 

    Context mContext;

    TextView nameView;
    TextView phoneNumberView;
    TextView accountView;

    String name;
    String phoneNumber;
    String accountNumber;

    ArrayList<Consumer> objects;


    public CustomAdapter(Context context, int resource, ArrayList<Consumer> objects)

        this.objects = objects;
        this.mContext = context;

    
    public CustomAdapter()

    
    @Override
    public int getCount() 
        return objects.size();
    

    @Override
    public Object getItem(int position) 
        return objects.get(position);
    

    @Override
    public long getItemId(int position) 
        return 0;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        Consumer consumer = (Consumer) getItem(position);

        // Values to be displayed.

        name = consumer.getName();
        phoneNumber = consumer.getPhoneNumber();
        accountNumber = consumer.getAccountNumber();


        LayoutInflater inflater = LayoutInflater.from(mContext); // generate an inflater using context.
        convertView = inflater.inflate(R.layout.details_layout,null); // inflate details_layout and store it in convertView.

        nameView = convertView.findViewById(R.id.nameView);
        phoneNumberView = convertView.findViewById(R.id.phoneNumberView);
        accountView = convertView.findViewById(R.id.accountView);

        nameView.setText(name);
        phoneNumberView.setText(phoneNumber);
        accountView.setText(accountNumber);

        return convertView;
    


ConsumerDescAndEdit.java(我需要在哪里使用变量):


public class ConsumerDescAndEdit extends AppCompatActivity 

    Button updateButton;

    EditText nameEditTextVar;
    EditText phoneEditTextVar;
    EditText accountEditTextVar;

    //      Variables to store user inputted data.
    String nameEdit;
    String phoneEdit;
    String accountEdit;

    DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumer_desc_and_edit);

        dbHelper = new DatabaseHelper(this);

        updateButton = findViewById(R.id.updateButton);

        nameEditTextVar = findViewById(R.id.nameEditScreen);
        phoneEditTextVar = findViewById(R.id.phoneNumberEditScreen);
        accountEditTextVar = findViewById(R.id.accountNumberEditScreen);

        String s_intent = getIntent().getStringExtra("EXTRA_SESSION_ID");
        nameEditTextVar.setText(s_intent);


        updateButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 


                nameEdit = nameEditTextVar.getText().toString();
                phoneEdit = phoneEditTextVar.getText().toString();
                accountEdit = accountEditTextVar.getText().toString();

                if (nameEdit.isEmpty() || phoneEdit.isEmpty() || accountEdit.isEmpty()) 
                    Toast.makeText(ConsumerDescAndEdit.this, "Data Insufficient", Toast.LENGTH_SHORT).show();
                 else 
                    boolean b = dbHelper.updateData(nameEdit, phoneEdit, accountEdit);
                    if (b)
                        Toast.makeText(ConsumerDescAndEdit.this, "Data updated", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(ConsumerDescAndEdit.this, "Could not update data", Toast.LENGTH_SHORT).show();

                
            
        );
    

【问题讨论】:

【参考方案1】:

当您为编辑Activity 创建Intent 时,将所选项目中的数据作为“附加”添加到Intent,如下所示:

Intent intent = new Intent(ShowAll.this,ConsumerDescAndEdit.class);
intent.putExtra("name", name);
intent.putExtra("phone", phone);
intent.putExtra("account", account);
startActivity(intent);

在编辑Activity 中,从Intent 中提取数据,如下所示:

String name = intent.getStringExtra("name");
String phone = intent.getStringExtra("phone");
String account = intent.getStringExtra("account");

【讨论】:

【参考方案2】:

试试下面的代码

public class CustomAdapter extends BaseAdapter 

    Context mContext;

    TextView nameView;
    TextView phoneNumberView;
    TextView accountView;

    String name;
    String phoneNumber;
    String accountNumber;

    ArrayList<Consumer> objects;
    private ItemClickListener itemClickListener;

    public interface ItemClickListener 
        void onItemClick(Consumer consumer);
    


    public CustomAdapter(Context context, int resource, ArrayList<Consumer> objects, ItemClickListener itemClickListener) 
        this.objects = objects;
        this.mContext = context;
        this.itemClickListener = itemClickListener;
    

    public CustomAdapter() 

    

    @Override
    public int getCount() 
        return objects.size();
    

    @Override
    public Object getItem(int position) 
        return objects.get(position);
    

    @Override
    public long getItemId(int position) 
        return 0;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        final Consumer consumer = (Consumer) getItem(position);

        // Values to be displayed.

        name = consumer.getName();
        phoneNumber = consumer.getPhoneNumber();
        accountNumber = consumer.getAccountNumber();


        LayoutInflater inflater = LayoutInflater.from(mContext); // generate an inflater using context.
        convertView = inflater.inflate(R.layout.details_layout, null); // inflate details_layout and store it in convertView.

        nameView = convertView.findViewById(R.id.nameView);
        phoneNumberView = convertView.findViewById(R.id.phoneNumberView);
        accountView = convertView.findViewById(R.id.accountView);

        nameView.setText(name);
        phoneNumberView.setText(phoneNumber);
        accountView.setText(accountNumber);
        convertView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (itemClickListener != null) 
                    itemClickListener.onItemClick(consumer);
                

            
        );
        return convertView;
    


在活动中进行以下操作

public class ConsumerActivity extends AppCompatActivity implements CustomAdapter.ItemClickListener 
    CustomAdapter adapter;
    ArrayList<Consumer> objects;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumer);
        adapter = new CustomAdapter(this, objects, this);
    

    @Override
    public void onItemClick(Consumer consumer) 
        Intent intent = new Intent(this, CustomerEdit.class);
        intent.putExtra("Consumer", consumer);
        startActivity(intent);
    

【讨论】:

以上是关于如何通过 onItemClickListener 在不同的类( ConsumerDescAndEdit.java )中使用某个类的变量?的主要内容,如果未能解决你的问题,请参考以下文章

执行长按时如何防止 OnItemClickListener 工作?

如何在 simpleadapter 中实现 OnItemClickListener?代码删除、更新等,

如何在 ListView 中使用 onItemClickListener 获取其他列

设置为null后如何将onItemClickListener重置为true?

如何使用 onItemClickListener 存储 ListView 中的可点击项目?

微调器的 OnItemClickListener