我想将editText1.getText()。toString()的值传递给基本适配器,以便将此值与“num”进行比较。 ¿我该怎么做?
以下是我的两项活动:
公共类MyActivity扩展了Activity {
private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
private TCPClient mTcpClient;
private Double comp;
String info;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
if (arg1 == KeyEvent.KEYCODE_ENTER){
Toast.makeText(
editText1.getContext()
, "Write: " + editText1.getText().toString()
, Toast.LENGTH_SHORT)
.show();
return true;
}
return false;
}
});
double comp=Double.parseDouble(editText1.getText().toString());
arrayList = new ArrayList<String>();
//relate the listView from java to the one created in xml
mList = (ListView)findViewById(R.id.list);
mAdapter = new MyCustomAdapter(this, arrayList, comp);
mList.setAdapter(mAdapter);
// connect to the server
new connectTask().execute("");
}
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented -->TCPClient.java
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
}
}
}
公共类MyCustomAdapter扩展了BaseAdapter {
private ArrayList<String> mListItems;
private LayoutInflater mLayoutInflater;
private Double comp;
public MyCustomAdapter(Context context, ArrayList<String> arrayList, Double compa){
mListItems = arrayList;
comp = compa;
//get the layout inflater
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
//getCount() represents how many items are in the list
return mListItems.size();
}
@Override
//get the data of an item from a specific position
//i represents the position of the item in the list
public Object getItem(int i) {
return null;
}
@Override
//get the position id of the item from the list
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
//check to see if the reused view is null or not, if is not null then reuse it
if (view == null) {
view = mLayoutInflater.inflate(R.layout.list_item, null);
}
//get the string item from the position "position" from array list to put it on the TextView
String stringItem = mListItems.get(position);
if (stringItem != null) {
TextView itemName = (TextView) view.findViewById(R.id.list_item_text_view);
if (itemName != null) {
//set the item name on the TextView
itemName.setText(stringItem);
double num=Double.parseDouble(stringItem);
if (num>comp) {
itemName.setText(stringItem);
}
else {
itemName.setBackgroundColor(Color.BLUE);
itemName.setText(stringItem);
}
}
}
//this method must return the view corresponding to the data at the specified position.
return view;
}
}
答案 0 :(得分:4)
在setData()
中添加方法MyCustomAdapter
,然后从您的主要活动中调用它。
public class MyCustomAdapter extends BaseAdapter {
Vector listData = null;
public void setData(Vector data) {
listData = data;
}
}