Custom List View doesn't show list Item .

723 views Asked by At

I don't know why but when I ran this code my List view does not display Items . I tried many time . But it's still not working . Either list item or error is not shown . This is my Java Code : This is where I define custom 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;


    }

}

This is where I call Custom List :

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

There are 2 answers

1
Apoorv On BEST ANSWER

Move your Network execution to an AsyncTask and then pass whatever data you get in an ArrayList to the constructor of your CustomList. Also overrride getCount in CustomList just to make sure.

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

You are performing network operations on main thread. This is not allowed in Android. Do it in a separate thread.