我正在开发一个Android应用程序,其中用户从服务器获取json数据并将其放在列表视图中。
在列表视图中,每个Item都有一些textViews和EditText。现在我希望用户在EditTexts中输入一个数字并将值发送回服务器。
Order.Java:
public class Order extends ListActivity {
UserFunctions userFunctions;
SessionManager session;
TextView text;
private static final String URL = "http://mydomain.in/webservice/json_sas.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_ITEM = "Item";
private static final String TAG_POSTS = "posts";
private static final String TAG_SALE = "sale";
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;
private JSONArray sasTotal = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
new LoadOrderForm().execute();
}
/**
* Retrieves recent products data from the server.
*/
public void updateJSONdata() {
session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
String cc = user.get(SessionManager.KEY_CC);
mCommentList = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cc", cc));
Log.d("request!", "starting");
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(URL, "POST", params);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String item = c.getString(TAG_ITEM);
String pack = c.getString(TAG_PACK);
String bal = c.getString(TAG_BAL);
String sale = c.getString(TAG_SALE);
String LM1 = c.getString(TAG_LM1);
String LM2 = c.getString(TAG_LM2);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ITEM, item);
map.put(TAG_PACK, pack);
map.put(TAG_BAL, bal);
map.put(TAG_SALE, sale);
map.put(TAG_LM1, LM1);
map.put(TAG_LM2, LM2);
// adding HashList to ArrayList
mCommentList.add(map);
}
sasTotal = json.getJSONArray(TAG_SASARRAY);
// looping through all posts according to the json object returned
for (int i = 0; i < sasTotal.length(); i++) {
JSONObject d = sasTotal.getJSONObject(i);
// gets the content of each tag
String sval = d.getString(TAG_SVAL);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post_order, new String[] { TAG_ITEM,
TAG_SALE,TAG_BAL }, new int[] { R.id.Item, R.id.sale, R.id.stk });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadOrderForm extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
updateList();
}
}
}
}
ListActivity如下所示:
https://drive.google.com/file/d/0Bz-FxbI1t7ezMzVoUWo3NXBlWW8/edit?usp=sharing
现在我想学习:
答案 0 :(得分:0)
我实际上只需要为之前的项目执行此操作。希望我能给你一些有见识的答案。
1)您需要创建自己的适配器类并将其展开。
2)展开你的editview,同样查看列表中textview的id。我假设您还有一个具有此视图的按钮。
3)将服务器对象传递给init函数,并在单击按钮时引用它的方法。
public class MyAdapter extends SimpleAdapter{
...
private final ServerClass server;
private Button submit;
public MyAdapter(Context context,
List<? extends HashMap<String, ?>> data, int resource, String[] from, int[] to, ServerClass server, Button submit) {
super(context, data, resource, from, to);
this.server = server;
this.submit = submit;
/*set up all of your init stuff here so you can figure out which view was changed when getview is called*/
}
public View getView(int position, View v, ViewGroup parent)
{
final View mView = super.getView(position, v, parent);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < parent.getChildCount(); i++){
View row = parent.getChildAt(i);
View textview= row.findViewById(/*textviewID*/);
View editview= row.findViewById(/*editviewID*/);
Log.i("CustomText!!!",textview.getText());
Log.i("CustomText!!!",editview.getText());
server.sendPOSTMethod(textview.getText(),editview.getText());
}
}
});
return mView;
}