I am first on the implementation of the Android app,My application is related to the car , In the company selection activity i have problem with ListView.
at the first that is like this :
But when scrolling that show my default icon Instead logo
how i can solve this problem? in this code i get my data from local json array and send to custom list for table displaying. The following code is my viewallcompnay activity:
package ir.database;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
public class ViewAllCompany extends Activity {
String url = "http://192.168.1.206/androhp/companylist.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_all_company);
LoadAllCompany lap = new LoadAllCompany();
lap.execute(url);
}
private class LoadAllCompany extends AsyncTask<String, String, String> {
private ArrayList<String> urlData;
private ListView list;
private InputStream getStreamFromURL(String urlString, String method) {
try {
URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) url
.openConnection();
huc.setRequestMethod(method);
huc.connect();
return huc.getInputStream();
} catch (Exception e) {
return null;
}
}
private String streamToString(InputStream is) {
String result = "";
String line = null;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
while ((line = br.readLine()) != null) {
result = line + "\n";
}
} catch (Exception e) {
}
return result;
}
protected String doInBackground(String... args) {
InputStream jsonStream = getStreamFromURL(args[0], "GET");
String jsonString = streamToString(jsonStream);
urlData = parseJSON(jsonString);
return null;
}
private ArrayList<String> parseJSON(String JSONString) {
try {
ArrayList<String> result = new ArrayList<String>();
JSONObject jo = new JSONObject(JSONString);
JSONArray allpersons = jo.getJSONArray("companylist");
for (int i = 0; i < allpersons.length(); i++) {
JSONObject object = allpersons.getJSONObject(i);
String objString = "";
objString = object.getString("name") + ","
+ object.getString("picture") + ","
+ object.getInt("id");
result.add(objString);
}
return result;
} catch (JSONException e) {
return null;
}
}
protected void onPostExecute(String file_url) {
String[] companyname = new String[urlData.size()];
String[] imageUrl = new String[urlData.size()];
String[] id = new String[urlData.size()];
int i = 0;
for (String s : urlData) {
String[] split = s.split(",");
id[i] = split[2];
companyname[i] = split[0];
imageUrl[i] = "http://lgapps.karnotech.ir/takhtegaz/images/companylogo/"
+ split[2] + "/" + split[1];
i++;
}
list = (ListView) findViewById(R.id.list);
CustomList adapter = new CustomList(ViewAllCompany.this,
companyname, imageUrl, id);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
viewallcompany xml layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".View_all_company" >
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:scrollingCache="true" >
</ListView>
and this code is my customlist:
package ir.database;
import android.app.Activity;
import android.content.Intent;
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[] companyname;
private final String[] imageUrl;
private final String[] id;
private ImageLoader imgLoader;
public CustomList(Activity context, String[] companyname,
String[] imageUrl, String[] id) {
super(context, R.layout.single_list, companyname);
this.context = context;
this.companyname = companyname;
this.id = id;
this.imageUrl = imageUrl;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.single_list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtTitle.setId(Integer.parseInt(this.id[position]));
txtTitle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, Cars.class);
intent.putExtra("carId", v.getId());
context.startActivity(intent);
}
});
txtTitle.setText(companyname[position]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
imgLoader = new ImageLoader(context);
imgLoader.DisplayImage(imageUrl[position], imageView);
return rowView;
}
}
customlist xml layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:gravity="right">
<ImageView
android:id="@+id/img"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/txt"
android:layout_width="90dp"
android:layout_height="30dp"
android:clickable="true"
android:onClick="onClick"
/>
</TableRow>