自定义列表视图不显示列表项。

时间:2013-12-12 06:50:07

标签: android custom-lists

我不知道为什么但是当我运行此代码时,我的List视图不显示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 .

2 个答案:

答案 0 :(得分:0)

Network执行移至AsyncTask,然后将ArrayList中获得的任何数据传递给CustomList的构造函数。同时覆盖getCount中的CustomList只是为了确保。

答案 1 :(得分:0)

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

您正在主线程上执行网络操作。这是不允许的 Android系统。在单独的线程中执行。