我们想在另一个类中调用列表视图'm_vwList',这样当按下签出按钮(在另一个类中)时,列表将从快速订单类中清除。
有人可以告诉如何在另一个类中调用列表视图,然后在单击按钮后清除它。我们知道按钮功能,但不知道如何清除列表
package com.zing.basket;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
/**
* Created by Student on 18/03/2016.
*/
public class QuickOrder extends android.support.v4.app.Fragment implements View.OnClickListener, View.OnKeyListener {
private EditText m_vwEditText;
private Button m_vwButton;
private ListView m_vwList;
private ArrayAdapter<String> m_adapter;
View myFragmentView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_quickorder, container, false);
m_vwEditText = (EditText) myFragmentView.findViewById(R.id.editText);
m_vwButton = (Button) myFragmentView.findViewById(R.id.button);
m_vwList = (ListView) myFragmentView.findViewById(R.id.list);
m_adapter = new ArrayAdapter<String>(getActivity(), R.layout.textview);
String[] listItems = getResources().getStringArray(R.array.listitems);
for (String item : listItems) {
m_adapter.add(item);
}
m_vwList.setAdapter(m_adapter);
m_vwButton.setOnClickListener(this);
m_vwEditText.setOnKeyListener(this);
return myFragmentView;
}
/** Called when the Button is clicked */
public void onClick(View v) {
String item = m_vwEditText.getText().toString();
m_vwEditText.setText("");
if (!item.equals("")) m_adapter.add(item);
}
/** Called whena key is pressed while the EditText view has focus */
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP) {
String item = m_vwEditText.getText().toString();
m_vwEditText.setText("");
if (!item.equals("")) m_adapter.add(item);
}
return true;
}
return false;
}
}
答案 0 :(得分:3)
要调用其他类的ListView
,您需要获取该类的实例并检查它是否为null。更多关注:
if (MainActivity.getInstance() != null) {
MainActivity.getInstance().mDrawerList.invalidateViews();
}