Reputation: 179
I am trying to display a list of items I am getting from a mysql database on a webserver. Here is my create for the page.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getlist);
showBusyDialog();
new GetData()
.execute(picdetails);
}
Here is my GetData class. The problem here is with the lines that are adding items to the listview. I am passing a JSON array as a text result.
public class GetData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... pic) {
// You might be tempted to display the busy dialog here, but you
// CAN'T update the UI from this method!
return loadImageFromNetwork(pic[0]);
}
@Override
protected void onPostExecute(String result) {
// Our long-running process is done, and we can safely access the UI thread
int ct_id;
String item_make;
String item_model;
String item_price;
String item_desc;
String item_location;
String item_image;
dismissBusyDialog();
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout1);
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ct_id=json_data.getInt("id");
item_make=json_data.getString("make");
item_model=json_data.getString("model");
item_price = json_data.getString("price");
item_desc = json_data.getString("cardesc");
item_location = json_data.getString("location");
item_image = json_data.getString("image");
String image_URL = "http://www.myurl.com/images/" + car_image;
TableRow tr = new TableRow(null);
tr.setId(ct_id);
tr.setClickable(true);
tr.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
String sdet_id;
int det_id;
// v.setBackgroundColor(Color.GRAY);
det_id = v.getId();
sdet_id = String.valueOf(det_id);
final Intent i = new Intent();
i.setClassName("demo.example.com", "demo.example.com.viewdetails");
i.putExtra("Det_id", sdet_id);
startActivity(i);
// v.setBackgroundColor(Color.TRANSPARENT);
// TODO Auto-generated method stub
}
});
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=10;
int rightMargin=15;
int bottomMargin=20;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
/* Create a Button to be the row-content. */
ImageView myimage = new ImageView(R.layout.getlist);
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = LoadImage(image_URL, bmOptions);
myimage.setImageBitmap(bm);
tr.addView(myimage);
TextView tmake=new TextView(R.layout.getlist);
// tmake.setText(item_make);
tmake.setText(Html.fromHtml("<H1>" + " " + item_location + "</H1>" + "<br /> ;" + item_desc
));
tr.addView(tmake);
TextView tmodel=new TextView(R.layout.getlist);
tmodel.setText(Html.fromHtml("<b><H1>" + " " + "€" + item_price + "</b></H1></br></br>" ));
tr.addView(tmodel);
/* Add row to TableLayout. */
// tr.setPadding(50, 0, 0, 0);
tl.addView(tr);
View v = new View(R.layout.getlist);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tl.addView(v);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Category Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
The problem appears to be with creating new items to add to the view, lines like this
View v = new View(R.layout.getlist);
Is my overall structure ok or do I need to do this another way?
Upvotes: 0
Views: 174
Reputation: 4354
1.Usually we show the busy dialog in the onPreExecute
of the
asyncTask.
2.You need to inflater your views thanks to an inflater.
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.view, null);
Hope this will help you.
Upvotes: 2