簡體   English   中英

自定義列表視圖不顯示列表項。

[英]Custom List View doesn't show list Item .

我不知道為什么,但是當我運行此代碼時,我的列表視圖不顯示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);


}

} 這是Listview的照片。它應該顯示List Item但不是。

將您的Network執行移動到AsyncTask ,然后將在ArrayList獲得的所有數據傳遞給CustomList的構造函數。 還要確保在CustomList重寫getCount

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM