2013-12-12 194 views
0

我不知道爲什麼,但是當我運行這段代碼時,我的List view不顯示Items。我嘗試了很多次。但它仍然無法正常工作。不顯示列表項或錯誤。 這是我的Java代碼: 這是我定義自定義ArrayAdapter自定義列表視圖不顯示列表項目。

package learn2crack.customlistview; 

import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomList extends ArrayAdapter<String>{ 

    private final Activity context; 
    private final String teamID; 
    private final String startTime; 

    public CustomList(Activity context,String teamID, String startTime) { 

     super(context, R.layout.list_single); 
     Log.i("CustomList", "InCustomList"); 
     this.context = context; 
     this.teamID =teamID; 
     this.startTime = startTime; 
     Log.i("teamID", teamID); 
     Log.i("teamID", startTime); 


    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent) { 

    Log.i("CustomList", "GetView"); 
    String response=null; 
    String userName = null; 
    JSONArray memberData=null; 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView= inflater.inflate(R.layout.list_single, null, true); 
    TextView txt_Name = (TextView) rowView.findViewById(R.id.txtName); 
    TextView txt_Status = (TextView) rowView.findViewById(R.id.txtStauts); 
    ImageView imgPlayer = (ImageView) rowView.findViewById(R.id.imgPlayer); 

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
    .detectDiskReads().detectDiskWrites().detectNetwork() 
    .penaltyLog().build()); 

    JSONObject jArray = null; 
     //Log.i("MemberiD", memberID); 
     ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("teamID",teamID)); 
    postParameters.add(new BasicNameValuePair("startTime",startTime)); 

    try { 

      response = CustomHttpClient.executeHttpPost(
        "http://10.0.2.2/football365/sankashaList.php", 
      postParameters); 
      Log.i("Response", response+""); 

      jArray = new JSONObject(response); 
      memberData=jArray.getJSONArray("memberdata"); 

      for(int i = 0 ; i < memberData.length(); i++){ 

       userName = (memberData.getJSONObject(i).getString("name").toString()+"\t"); 
       txt_Name.setText(userName); 
       txt_Status.setText("Status"); 
       String path="path"; 
       String url="http://10.0.2.2/football365/Photo"+path; 
       Bitmap bitmap= BitmapFactory.decodeStream((InputStream) new URL(url).getContent()); 
       imgPlayer.setImageBitmap(bitmap); 
      } 

} 

     catch(JSONException e){ 
     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
    catch (Exception e) { 

     Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 

      Log.i("AVCE", userName+""); 

    return rowView; 


    } 

} 

這就是我所說的自定義列表:

public class MainActivity extends Activity { 

TextView txt_Date ; 
TextView txt_Location; 
String teamID; 
String startTime; 
String location; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Log.i("AVCDEF", "InMain"); 
    Intent intent= getIntent(); 
    startTime=intent.getStringExtra("startTime"); 
    location=intent.getStringExtra("location"); 
    teamID = intent.getStringExtra("teamID"); 
    RelativeLayout r1 = (RelativeLayout)findViewById(R.id.relative1); 
    ListView list= (ListView)findViewById(R.id.list); 
    txt_Date = (TextView)findViewById(R.id.txt_date); 
    txt_Location = (TextView) findViewById(R.id.txt_location); 
    txt_Date.setText(startTime); 
    txt_Location.setText(location); 
    Log.i("AVCDEF", startTime); 
    Log.i("AVCDEF", location); 
    Log.i("AVCDEF", teamID); 

    CustomList adapter = new CustomList(MainActivity.this, teamID, startTime); 
    Log.i("AVCDEF", "InMain"); 
    list.setAdapter(adapter); 


} 

} This is the Listview photo . It should display List Item But not .

+0

等待getView()方法完成所有工作。爲什麼在getView()中做了太多工作? –

+0

在getView中做太多的工作是不允許的?對不起,我真的不知道。我是非常新的Android。 : – user3032822

+0

喜歡做網絡操作和解碼Bitmap流。你知道,getView()方法被多次調用,因爲ListView可以上下滾動... –

回答

0

移動你的Network執行到AsyncTask然後將您在ArrayList中獲得的任何數據傳遞給您的CustomList的構造函數。 CustomList也只是爲了確認而忽略getCount

+0

這樣做對我來說,我總是忘記重寫'getCount()'和'getItem()',謝謝。 –

0
response = CustomHttpClient.executeHttpPost(
       "http://10.0.2.2/football365/sankashaList.php", 
     postParameters); 

您正在主線程上執行網絡操作。這是不允許在Android的 。做一個單獨的線程。

+0

但是result從PHP返回,這似乎是正確的。 – user3032822