首页 新闻 搜索 专区 学院

Android中怎么获取ListView中的数据到服务器

0
悬赏园豆:10 [已解决问题] 解决于 2016-10-04 17:44

我的需求是:当点击checkbox,获取题号并把题号上传到服务器,并得到服务器的响应。

 

我的代码是:

复制代码
package cn.edu.aynu.rjxy.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.edu.aynu.rjxy.entity.Data;
import cn.edu.aynu.rjxy.utils.SharedPreferencesUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SetlectActivity extends Activity {
    private static final int CHANGE_UI = 1;
    private static final int CHANGE = 3;
    private ListView lv;
    private List<Data> datas = new ArrayList<Data>();
    //主线程创建消息处理器
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_UI) {
                try {
                    JSONArray arr = new JSONArray((String)msg.obj);  
                      for (int i = 0; i < arr.length(); i++) {   
                          JSONObject temp = (JSONObject) arr.get(i); 
                         // Log.d("json", temp.getInt("id")+temp.getString("exp_name")+temp.getString("exp_tech"));
                          Data data = new Data();                              
                          data.setId(temp.getInt("id"));                     
                          data.setExp_name(temp.getString("exp_name"));
                          data.setExp_tech(temp.getString("exp_tech"));                          
                         //这个地方可以获取到值但是适配器那位0
                          datas.add(data);
                         
                          
                      }
                      lv.setAdapter(new MyAdapter());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }else if (msg.what == CHANGE) {
                 Toast.makeText(SetlectActivity.this,(String)msg.obj, 1).show();
            }
        };
    };
    private String sno;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        lv = (ListView) findViewById(R.id.lv);
        select();
         //取出账号和密码
        Map<String,String> userInfo = SharedPreferencesUtils.getUserInfo(this);
        
        if (userInfo != null) {
              sno = userInfo.get("sno");
              id = userInfo.get("id");
        }
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                
                
                
            }
        });
    }
    
        
        

    private void select(){
        //子线程更新UI
         new Thread(){
                public void run(){
                    StringBuilder builder = new StringBuilder();
                    HttpClient client = new DefaultHttpClient();
                    String path = "http://10.6.78.254:2016/xampp/graduate/index.php/home/Student/test_android";        
                    HttpGet httpGet = new HttpGet(path);
                    try {
                    HttpResponse response = client.execute(httpGet);
                    StatusLine statusLine = response.getStatusLine();
                    int statusCode = statusLine.getStatusCode();
                    if (statusCode == 200) {    
                        
                      BufferedReader reader = new BufferedReader
                                  (new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
                          for(String s=reader.readLine();s!=null;s=reader.readLine())
                          {
                              builder.append(s);
                          }
                          String content = builder.toString();
                          System.out.println(content);
                        //通知主线程更新UI
                          Message message = new Message();
                          message.what = CHANGE_UI;
                          message.obj = content;
                          handler.sendMessage(message);
                    }else{
                        Log.e(HomeActivity.class.toString(), "Failed");
                    }
                } catch (ClientProtocolException e) {
                    
                    e.printStackTrace();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
                    
                    
                };
            }.start();
    }
     class MyAdapter extends BaseAdapter{

            @Override
            public int getCount() {
                Log.d("AAA", ""+datas.size());        
                return datas.size();
                
                    
            }

            @Override
            public Object getItem(int position) {
        
                return datas.get(position);
            }

            @Override
            public long getItemId(int position) {
                
                return position;
            }
            
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View view = View.inflate(SetlectActivity.this, R.layout.item_listview, null);
                TextView id = (TextView) view.findViewById(R.id.tv_id);
                TextView exp_name = (TextView) view.findViewById(R.id.tv_name);
                CheckBox cb_select = (CheckBox) view.findViewById(R.id.cb_select);
                Data data = datas.get(position);
                Log.d("aaaaa",datas.get(position).getExp_name() );
                
                id.setText(String.valueOf(datas.get(position).getId()));
                exp_name.setText(datas.get(position).getExp_name());
                exp_name.setOnClickListener(new OnClickListener() {
                    
                    @Override
                    public void onClick(View v) {
                          Toast.makeText(SetlectActivity.this, datas.get(position).getExp_tech(),0).show();   
                        
                    }
                });
                cb_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                             boolean isSaveSuccess = SharedPreferencesUtils.saveUserInfo03(SetlectActivity.this, sno, String.valueOf(datas.get(position).getId()));
                            
                             }
                        
                    }
                });        
                return view;
            }
            
     }
    
}
复制代码
kang_ya_ping的主页 kang_ya_ping | 菜鸟二级 | 园豆:243
提问于:2016-09-28 20:54
< >
分享
最佳答案
1

设置list点击事件,事件包括checkbox的选中,获取题号临时存储,再设置选择按钮事件,提交。

收获园豆:10
彳亍者 | 初学一级 |园豆:108 | 2016-09-30 13:44

恩,我的想法也是如此,因为checkbox不知道该如何实现,所以换成了button控件了。问题已经解决了。谢谢你呀。我的qq是1531698802.希望我们可以成为好友。可以跟你一起探讨更多的问题。

kang_ya_ping | 园豆:243 (菜鸟二级) | 2016-10-04 17:43

@kang_ya_ping: 加好友了。ID是周公

彳亍者 | 园豆:108 (初学一级) | 2016-10-08 09:32
其他回答(1)
0

完全没看出来有了代码问主还想问什么

ensleep | 园豆:1393 (小虾三级) | 2016-10-04 09:37

没关系,问题已经解决了。

支持(0) 反对(0) kang_ya_ping | 园豆:243 (菜鸟二级) | 2016-10-04 17:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册