0
私は私のローカルサイトからデータを取得し、 リストビューにデータを表示するには、Androidでプログラムを作るが、私はプログラムの中でいくつかの問題を抱えていると私はここにリストビュー内のデータ にどのようにこのJavaコードでアンドロイドの私のサイトに接続するには?
を表示することはできませんが、私のコードです:
これは私のリストビューと私のローカルサイトに接続するには、Android Studioのコードです:
package com.example.delta.social_food;
import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Arrays;
/**
* Created by delta on 5/8/2016.
*/
public class Shared_food_list extends ListActivity {
String[] username,description;
String tusername,tdescription;
private int page=1;
private int count;
private String res="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_food_list);
new getpost().execute();
}
private void makearray(int c){
username=new String[c];
description=new String[c];
Arrays.fill(username,"");
}
public class getpost extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
try {
String data= URLEncoder.encode("txt_page", "UTF8")+"="+URLEncoder.encode(page+"","UTF8");
URL link=new URL("http://192.168.1.4/social_food/main/show_food_list");
URLConnection connect=link.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
sb.append(line);
}
res=sb.toString();
for(int y=0;y<4;y++){
if(res.charAt(y)=='|'){
count=Integer.parseInt(res.substring(0,y));
res=res.substring(y+1);
break;
}
}
makearray(count);
int f=0,c=0;
for(int i=0;i<res.length();i++){
if(res.charAt(i)=='|'){
String temp=res.substring(f,i);
if(c==0){
tusername=temp;
}
if(c==1){
tdescription=temp;
for(int t=0;t<count;t++){
if(username[t].equals("")){
username[t]=tusername;
description[t]=tdescription;
break;
}
}
c=-1;
}
f=i+1;
c+=1;
}
}
}catch (Exception e){
res=e.toString();
}
return "";
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
setListAdapter(new IA());
}
}
public class IA extends ArrayAdapter<String>{
public IA() {
super(Shared_food_list.this, R.layout.show_list,username);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.show_list, parent, false);
TextView users=(TextView)findViewById(R.id.show_list_username);
TextView desc=(TextView)findViewById(R.id.show_list_message);
users.setText(username[position]);
desc.setText(description[position]);
return (row);
}
}
}
私のリストビュー:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
、これが私のカスタマイズリストビューのコードです:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="User Name"
android:id="@+id/show_list_username" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="@+id/show_list_message"
android:layout_gravity="right" />
、これはCodeIgniterのと私のPHPのサイトコードです:
コントローラコード:
public function show_food_list(){
$page = $this -> input -> post('txt_page');
if($page=="1"){
$this -> load -> model('Database','model');
$data['infos'] = $this -> model -> get_food_list();
}
$this -> load -> view('food',$data);
}
モデルコード:
public function get_food_list(){
$query = $this -> db -> get('share_food');
return $query->result_array();
}
と私の見解コード:
<?php
foreach($infos as $info):
echo $info['username'].'|';
echo $info['description'].'|';
endforeach;
?>