Reputation: 1
Hi I am creating a layout and a ListView
within it to display small icon in 5 columns. I am using AysncTask
to parse JSON
to get the images details which I need to display in list view. Now I am getting error. I don't know how to solve. Please help me to solve.
My layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_seatvisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notebook.runtowin.Activity.Seatvisible">
<TextView
android:id="@+id/from_to"
android:layout_width="match_parent"
android:layout_height="35sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/colorPrimaryDark"
android:text="Select your seat"
android:padding="5sp"
android:gravity="center_vertical"
android:textStyle="bold"
android:textColor="@color/colorwhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/from_to">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFEB3B"
android:id="@+id/headinglayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/busname"
android:padding="5sp"
android:text="RUN TO WIN"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/busname"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seatnumber"
android:padding="5sp"
android:text="Seats :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@+id/seatnumber"
android:text="25,24"
android:id="@+id/seatsnumber"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ticketprice"
android:layout_alignParentRight="true"
android:text="hsdshd"
android:textStyle="bold"
android:padding="5sp"
android:textSize="30sp"
android:layout_marginRight="10sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/headinglayout"
android:layout_marginTop="3sp"
android:paddingTop="10sp"
android:paddingRight="25sp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/iconsteer"
android:id="@+id/imageView2"
android:layout_marginEnd="50sp"
android:layout_alignParentEnd="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:layout_marginTop="20sp"
android:id="@+id/listviewseat">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25sp"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:background="@color/colorPrimaryDark"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BOOK TICKETS"
android:padding="10sp"
android:id="@+id/bt_book"
android:textSize="20sp"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
My Listview Layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8sp"
android:gravity="top">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewseat"/>
</LinearLayout>
My code is:
public class Seatvisible extends AppCompatActivity {
ArrayList<HashMap<String, String>> busdetails1;
private String BusID, servicedate;
private static String url="http://appsilonz.com/demo/v2/bus_seat_abhi.php?journey_date=2017-01-05&bus_id=20";
private ListView lv;
private TextView busname, from_to, seatsnumber, ticketprice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seatvisible);
lv = (ListView) findViewById(R.id.listviewbusdetails);
inilize();
new Seatparsing().execute();
}
private void inilize() {
Intent intent = getIntent();
BusID = intent.getStringExtra("BusID");
servicedate = intent.getStringExtra("servicedate");
busname = (TextView) findViewById(R.id.busname);
from_to = (TextView) findViewById(R.id.from_to);
seatsnumber = (TextView) findViewById(R.id.seatsnumber);
ticketprice = (TextView) findViewById(R.id.ticketprice);
}
public class Seatparsing extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
String response;
String jsonstr, seat_name, ticket_status, seatladies;
ProgressDialog dialog = new ProgressDialog(Seatvisible.this);
AndroidHttpClient maclient = AndroidHttpClient.newInstance("");
@Override
protected void onPreExecute() {
dialog.setCancelable(false);
dialog.setMessage("Please wait..");
dialog.setIndeterminate(false);
dialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response = maclient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
return null;
} catch (IOException exception) {
exception.printStackTrace();
return null;
}
Log.i("Seats", "" + response);
Log.i("seaturl", "" + url);
jsonstr = response;
busdetails1 = new ArrayList<>();
if (jsonstr != null) {
HashMap<String, String> map2 = new HashMap<>();
try {
JSONArray array1 = new JSONArray(jsonstr);
for (int i = 0; i < array1.length(); i++) {
JSONObject objct1 = array1.getJSONObject(i);
JSONArray array2 = objct1.getJSONArray("row1");
for (int j = 0; j < array2.length(); j++) {
JSONObject objct2 = array2.getJSONObject(j);
seat_name = objct2.getString("seat_name ").toString();
ticket_status = objct2.getString("ticket_status");
seatladies = objct2.getString("seatIsLadies");
map2.put("seat_name", seat_name);
map2.put("ticket_status", ticket_status);
map2.put("seatIsLadies", seatladies);
}
busdetails1.add(map2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return busdetails1;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> busdetails1) {
dialog.dismiss();
customadapter adapter = new customadapter(getApplicationContext(), R.layout.seatlistview, busdetails1);
lv.setAdapter(adapter);
}
}
public class customadapter extends ArrayAdapter {
private ArrayList<HashMap<String, String>> list;
private int resource;
private LayoutInflater inflater;
public customadapter(Context context, int resource, ArrayList<HashMap<String, String>> busdetails1) {
super(context, resource, busdetails1);
list = busdetails1;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(int i, View convertView, ViewGroup parent) {
convertView = inflater.inflate(resource, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewseat);
String seat_name = list.get(i).get("seat_name");
String ticket_status = list.get(i).get("ticket_status");
String seatIsLadies = list.get(i).get("seatIsLadies");
if (!seat_name.equals(null)) {
if (ticket_status.equals("booked")) {
if (seatIsLadies.equals("true")) {
imageView.setImageResource(R.drawable.reservedladies);
imageView.setClickable(false);
} else {
imageView.setImageResource(R.drawable.bookedseat);
imageView.setClickable(false);
}
} else {
imageView.setImageResource(R.drawable.available_seat_img);
}
} else {
imageView.setImageDrawable(null);
imageView.setClickable(false);
}
return convertView;
}
}
}
Upvotes: 0
Views: 60
Reputation: 6828
You should use dp
for dimensions and sp
for textSize
. Currently you are using sp
for dimensions.
See the edited layout below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_seatvisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notebook.runtowin.Activity.Seatvisible">
<TextView
android:id="@+id/from_to"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/colorPrimaryDark"
android:text="Select your seat"
android:padding="5dp"
android:gravity="center_vertical"
android:textStyle="bold"
android:textColor="@color/colorwhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/from_to">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFEB3B"
android:id="@+id/headinglayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/busname"
android:padding="5dp"
android:text="RUN TO WIN"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/busname"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seatnumber"
android:padding="5dp"
android:text="Seats :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="25,24"
android:id="@+id/seatsnumber"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ticketprice"
android:layout_alignParentRight="true"
android:text="hsdshd"
android:textStyle="bold"
android:padding="5dp"
android:textSize="30sp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/headinglayout"
android:layout_marginTop="3dp"
android:paddingTop="10dp"
android:paddingRight="25dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/iconsteer"
android:id="@+id/imageView2"
android:layout_marginEnd="50dp"
android:layout_alignParentEnd="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:layout_marginTop="20dp"
android:id="@+id/listviewseat">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:background="@color/colorPrimaryDark"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BOOK TICKETS"
android:padding="10dp"
android:id="@+id/bt_book"
android:textSize="20sp"/>
</RelativeLayout>
</LinearLayout>
For ListView layout,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:gravity="top">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewseat"/>
</LinearLayout>
Also you can't layout_marginRight
to an id
at this line,
android:layout_marginRight="@+id/seatnumber"
You probably meant,
android:layout_marginRight="5dp"
See @Baseem Samy's answer too.
Upvotes: 1
Reputation: 2976
in the textview with id android:id="@+id/seatsnumber" you set layout_marginRight to an id, that's not available in android you can't set a padding with a view
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@+id/seatnumber"
android:text="25,24"
android:id="@+id/seatsnumber"/>
Upvotes: 0
Reputation: 3654
I see sp units in ur layout files for padding size. sp is for font size only where dp or dip is for all dimensions.
Upvotes: 0