我想从我的php mysql中检索数据。 它将是2个文本和图像。 我想把它放在自定义列表视图中,但是当我尝试运行我的应用程序时,我的代码出现问题,任何帮助!
这是我的活动代码
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;
}
}