我想將editText1.getText()。toString()的值傳遞給基礎適配器,以便將此值與「num」進行比較。 ¿我該怎麼做?從主要活動傳遞數據到基礎適配器一個
這裏是我的2個活動:
公共類MyActivity延伸活動{
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;
}
}
是基礎適配器是類還是活動? – jeevamuthu
public class MyCustomAdapter extends BaseAdapter – user2431541
爲什麼要將數據傳遞給MyCustomAdapter類? – jeevamuthu