我需要在listview中隐藏按钮,同时使用JSON获取空值

时间:2014-04-16 11:03:37

标签: android

这是我的名为Dataadapter_PaidTicket.java的

的java文件
public class Dataadapter_PaidTicket extends BaseAdapter {

    public static String ticket_id;
    private final Context context;
    public JSONArray values;
    public TextView textview;
    public TextView textview1;
    public TextView textview2;
    public TextView textview3;
    public TextView textview4;
    public Button btn;

    public Dataadapter_PaidTicket(Context context, int _resource, JSONArray values) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.values = values;
    }

    @Override
    public int getCount() {
        return values.length();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        try {
            return values.get(position);
        } catch (JSONException e) {
            return e;
        }
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row_ticket, null);
            holder.textview = (TextView) convertView.findViewById(R.id.ticket_name);
            holder.textview1 = (TextView) convertView.findViewById(R.id.ticket_start_date);
            holder.textview2 = (TextView) convertView.findViewById(R.id.ticket_end_date);
            holder.textview3=(TextView)convertView.findViewById(R.id.ticket_price);
            holder.textview4=(TextView)convertView.findViewById(R.id.ticket_qty);
            holder.btn=(Button)convertView.findViewById(R.id.btn_edit_buy);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }       
        JSONObject temp = null;
        try {
            temp = (JSONObject) values.get(position);
            String title=temp.get("paid_ticket_name").toString();
            //String Ctitle = title.substring(0, 10);
            holder.textview.setText(title.replace("null", " "));
            String tkt_s_date = temp.get("paid_start_sale").toString();
            String s_dat e= Constants.formatteddate(tkt_s_date);
            holder.textview1.setText(s_date.replace("null", " "));
            String tkt_e_date = temp.get("paid_end_sale").toString();
            String e_date = Constants.formatteddate(tkt_e_date);
            holder.textview2.setText(e_date.replace("null", " "));
            String q = temp.get("paid_qty").toString();
            holder.textview3.setText(q.replace("0", " "));
            String p = temp.get("paid_price").toString();
            holder.textview4.setText(p.replace("null", " "));
            if (Constants.id.equals(Dataadapter.user_id)) {
                holder.btn.setText("EDIT");
            } else {
                holder.btn.setText("BUY");
            }
            Log.e("Title",temp.get("paid_ticket_name").toString());
            Log.e("Venue",temp.get("paid_description").toString());
            Log.e("Date",temp.get("paid_start_sale").toString());
            ticket_id = temp.get("id").toString();
            Log.e("Ticket ID",ticket_id);
            convertView.setId(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    private class ViewHolder {
        public TextView textview;
        public TextView textview1;
        public TextView textview2;
        public TextView textview3;
        public TextView textview4;
        public Button btn;
    }
}

在这里,我需要做这样的事情。如果title,s_date,e_date,paid_qty,paid_price的值为null,那么我不想显示按钮(这里是btn)。是否可以隐藏该按钮?我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

请替换" "用""在你的替换(" null","")函数中没有空格(因为如果JSON没有值,它不是一个emptyspace,它只是null。然后再试一次。

  if (title==null || s_date==null || e_date==null || paid_qty==null || paid_price == null){
       holder.btn.setVisibility(View.INVISIBLE);
  }

  else{
      holder.btn.setVisibility(View.VISIBLE);
  }

答案 1 :(得分:0)

Button btn;
btn= (Button) findViewById(R.id.button1);
if (title.equals(" ") || s_date.equals(" ") || e_date.equals(" ") || paid_qty.equals(" ") || paid_price.equals(" "))
{
   btn.setVisibility(View.GONE);
}
else
{
btn.setVisibility(View.VISIBLE);
}