具有自定义行的数组列表自定义适配器:如何使单个线性布局文本视图以动态方式展开

时间:2017-07-31 03:49:23

标签: android sql dynamic textview android-linearlayout

我一直在尝试创建自定义适配器来显示SQL服务器的结果,但是我在创建更复杂的列表视图时遇到了麻烦,而且我还不清楚适配器和hashmap,所以我想显示一个自定义行但是有一行它根据SQl的结果动态展开,所以这里是布局

Red marked shows dynamically expand

但是当我尝试显示列时,它会起作用,但红色标记的行不会按预期展开。这是代码:

String dt1,dt2;
TextView txtview;
SQLclass sqlclass;
ListView lvpenjualan,lvpenjualandata;
String z;

private ArrayList<HashMap<String, String>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reportpenjualan_detail);

    Bundle extra= getIntent().getExtras();
    sqlclass = new SQLclass();
    txtview= (TextView) findViewById(R.id.totalpenjualan);
    dt1 = generator.dt1;
    dt2 = generator.dt2;
    lvpenjualan = (ListView) findViewById(R.id.lvpenjualan_detail);
    NumberFormat formatter = new DecimalFormat("###,###,###.##");
    try {
        ResultSet Result = sqlclass.query("select * from iatpenjualan a join iamcustomer c on a.kode_customer=c.kode_customer join iamsalesman d on a.kode_salesman=d.kode_salesman where a.tanggal between '"+ dt1 + "' and '"  + dt2 + "' order by tanggal,no_faktur asc");
        float total=0;
        generator.list.clear();
        //LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
        //View child = getLayoutInflater().inflate(R.layout.child, null);
        //layout.addView(child);
        while (Result.next()) {
            HashMap<String, String> datanum = new HashMap<String, String>();
            String strCurrentDate =Result.getString("tanggal");
            SimpleDateFormat Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            Date newDate = Format.parse(strCurrentDate);
            Format = new SimpleDateFormat("dd-MMMM-yyyy");
            String date = Format.format(newDate);
            datanum.put(SECOND_COLUMN,Result.getString("no_faktur"));
            datanum.put(THIRD_COLUMN, Result.getString("nama_customer"));
            datanum.put(FOURTH_COLUMN, "H");
            datanum.put(FIFTH_COLUMN,Result.getString("kode_salesman"));
            datanum.put(SIX,Result.getString("kode_lokasi"));
            ResultSet query = sqlclass.query("select * from iatpenjualan1 where no_faktur='"+Result.getString("no_faktur")+"'");
            while(query.next()) {
                datanum.put(SEVEN, query.getString("nama_stock"));
                datanum.put(EIGHT, query.getString("qty"));
                total = query.getFloat("harga_jual") * query.getFloat("qty");
                datanum.put(NINE,query.getString("harga_jual"));
                datanum.put(TEN,query.getString("discount") );
                datanum.put(ELEVEN,String.valueOf(total));
            }
            generator.list.add(datanum);
        }

        generator.lvadapter_datail adapter=new generator.lvadapter_datail(this, generator.list);
        lvpenjualan.setAdapter(adapter);

和自定义适配器:

public static class lvadapter_datail extends BaseAdapter {


    public ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String>>();
    Activity activity=new Activity();
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    TextView txtFifth;
    TextView six;
    TextView seven;
    TextView eight;
    TextView nine;
    TextView ten;
    TextView eleven;
    LinearLayout lvpenjualandetail;

    public lvadapter_datail(){
        list = new ArrayList<HashMap<String, String>>();
    }

    public lvadapter_datail(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity= activity;
        this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

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



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub



        LayoutInflater inflater=activity.getLayoutInflater();

        if(convertView == null){

            convertView=inflater.inflate(R.layout.row_sales_detail, null);

            txtFirst=(TextView) convertView.findViewById(R.id.tanggal);
            txtSecond=(TextView) convertView.findViewById(R.id.faktur);
            txtThird=(TextView) convertView.findViewById(R.id.customer);
            txtFourth=(TextView) convertView.findViewById(R.id.jangkakredit);
            txtFifth=(TextView) convertView.findViewById(R.id.sales);

            six=(TextView) convertView.findViewById(R.id.gudang);
            seven=(TextView) convertView.findViewById(R.id.salesitemname);
            eight=(TextView) convertView.findViewById(R.id.salesqty);
            nine=(TextView) convertView.findViewById(R.id.salescpu);
            ten=(TextView) convertView.findViewById(R.id.salesdiscount);
            eleven=(TextView) convertView.findViewById(R.id.salessubtotal);
            lvpenjualandetail= (LinearLayout) convertView.findViewById (R.id.lsales);
        }

        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));
        txtFifth.setText(map.get(FIFTH_COLUMN));


        six.setText(map.get(SIX));
        seven.setText(map.get(SEVEN));
        eight.setText(map.get(EIGHT));
        nine.setText(map.get(NINE));
        ten.setText(map.get(TEN));
        eleven.setText(map.get(ELEVEN));
     //   if(txtFirst.getText().toString().trim().length()>0 && txtSecond.getText().toString().trim().length()>0  && txtThird.getText().toString().trim().length()>0  && txtFourth.getText().toString().trim().length()>0 && txtFifth.getText().toString().trim().length()>0)
    //    {}
        return convertView;
    }

}

和布局:

<TextView
    android:id="@+id/tanggal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@color/cast_intro_overlay_button_background_color"
    android:gravity="center"
    android:text="date"
    android:textAlignment="center" />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/tanggal"
    android:background="#000000"/>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:id="@+id/lsales"
    android:layout_below="@+id/tanggal"
    android:layout_alignParentStart="true">
    <TextView
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@+id/tanggal"
        android:id="@+id/faktur"
        android:textAlignment="center"
        android:text="sales_invoice"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.78"
        android:text="customer"
        android:gravity="center"
        android:id="@+id/customer"
        android:textAlignment="center"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:text="day"
        android:gravity="center"
        android:textAlignment="center"
        android:id="@+id/jangkakredit"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/sales"
        android:text="sales"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:textAlignment="center"/>
    <TextView
        android:layout_width="0dp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_height="wrap_content"
        android:layout_weight="0.67"
        android:gravity="center"
        android:text="warehouse"
        android:textAlignment="center"
        android:id="@+id/gudang"/>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/lsales"
    android:background="#000000"
    android:id="@+id/view" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:id="@+id/ldetailsales"
    android:layout_below="@+id/lsales"
    android:layout_alignParentStart="true">
    <TextView
        android:layout_width="368dp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="item name"
        android:textAlignment="center"
        android:id="@+id/salesitemname"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:layout_alignParentStart="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="5sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="item_qty"
            android:textAlignment="center"
            android:id="@+id/salesqty"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="5sp"
            android:text="x"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:id="@+id/kali"
            android:textAlignment="center"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="5sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="item_price"
            android:textAlignment="center"
            android:id="@+id/salescpu"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="5sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="discount"
            android:textAlignment="center"
            android:id="@+id/salesdiscount"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="5sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="total_price"
            android:textAlignment="center"
            android:id="@+id/salessubtotal"/>
    </LinearLayout>
</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/ldetailsales"
    android:background="#000000"/>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:layout_below="@+id/ldetailsales"
    android:id="@+id/lsalessubtotal"
    android:layout_alignParentStart="true">

</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/lsalessubtotal"
    android:background="#000000"/>

是否可以制作文本视图可以是动态结果的布局,或者我应该在listview中创建listview吗?

如果代码示例不清楚,我会尝试提供信息

0 个答案:

没有答案