dongmei9961 2015-10-21 16:06
浏览 34

查看MySQL DB中的数据到我的android自定义列表视图

I want to retrieve data from my php mysql. It will be 2 texts and image. I want to put it in a custom list view but there is a problem with my code when I'm trying run my app, any Help !

Here's my Activity Codes

SharedPreferences sh;
SharedPreferences.Editor editor;

String PHP_URL = "http://192.168.1.19:80/android/view.php";
String myStr;
ArrayList <Departments> dataList;

String depNameText;
String repNameText;
String imageURL;

ImageButton logOutBtn;
ImageButton menuBtn;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_faculty_member);

    logOutBtn = (ImageButton) findViewById(R.id.logOutButton);
    menuBtn = (ImageButton) findViewById(R.id.menuButton);
    list = (ListView) findViewById(R.id.listView);

    dataList = new ArrayList<Departments>();



    //new MyTask().execute();


}


public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
        Toast.makeText(FacultyMemberActivity.this, "You pressed back button", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

public void LogOutFunction(View view) {
    sh = getSharedPreferences("shared.txt",MODE_PRIVATE);
    editor = sh.edit();
    editor.clear();
    editor.commit();
    Intent i = new Intent(FacultyMemberActivity.this,LoginActivity.class);
    startActivity(i);
}

public void menuFunction(View view) {
}

private class MyTask extends AsyncTask<Void,Void,ArrayList<Departments>> {
    private StringBuilder sb;


    protected ArrayList<Departments> doInBackground(Void... params) {


        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(PHP_URL);
        try {
            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String s = "";
            sb = new StringBuilder();
            while ((s = br.readLine()) != null) {

                sb.append(s);

            }

            String str = sb.toString();
            JSONArray arr = new JSONArray(str);
            for (int i = 0; i < arr.length(); i++) {
                JSONObject obj = arr.getJSONObject(i);

                depNameText  = obj.getString("depName");
                repNameText    = obj.getString("repName");
                imageURL   = obj.getString("imageURL");


                dataList.add(new Departments(depNameText,repNameText,imageURL));

            }



        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return dataList;
    }

    protected void onPostExecute(ArrayList<Departments> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        for (Departments dep : result) {
            MyAdapter adapter = new MyAdapter();
            list.setAdapter(adapter);
        }



    }

}



class MyAdapter extends BaseAdapter{
    ArrayList<Departments> result;

    public MyAdapter() {
        this.result = result;
    }

    @Override
    public int getCount() {
        return result.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view==null){
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.new_layout,parent,false);
            holder = new ViewHolder();
            view.setTag(holder);

        }
        else {
           holder = (ViewHolder) view.getTag();
        }

        holder.depName = (TextView) view.findViewById(R.id.depName);
        holder.repName = (TextView) view.findViewById(R.id.repName);
        holder.image = (ImageView) view.findViewById(R.id.imageView);


        holder.depName.setText((CharSequence) result.get(0));
        holder.repName.setText((CharSequence) result.get(1));
        holder.image.setImageURI(Uri.parse(String.valueOf(result.get(2))));


        return view;
    }
}

class ViewHolder{
    TextView depName,repName;
    ImageView image;

}
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等