如何在listview中包含更多表格字段?

时间:2013-01-17 01:40:37

标签: java android

我带来了两个字段webservice并填充了listview。

我需要一个多行的textview。我的组件足够大,可以显示至少4行,但只显示2行。

如何在listview中添加更多子项?

我想在listview中包含更多表格字段。

private void pesquisarProdutos() {
    dialog = ProgressDialog.show(this, "Aguarde...",
            "Pesquisando registros.", false, true);
    dialog.setIcon(R.drawable.ic_launcher);
    dialog.setCancelable(false);

    DSRESTConnection conn = gc.getConnection();
    final TServerMethods1 serv = new TServerMethods1(conn);

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                TDataSet ds;
                // limpa a lista a cada pesquisa
                dataList.clear();
                ds = serv.ConsultaItens(VendaClienteActivity.edtItem
                        .getText().toString());
                while (ds.next()) {
                    HashMap<String, String> item = new HashMap<String, String>();
                    item.put("nome", ds.getValue("NOME").GetAsString());
                    item.put("id", ds.getValue("CODIGO").GetAsString());
                    dataList.add(item);
                }
                // desabilita o dialog
                dialog.dismiss();
                finalizaDialogo(false, "");

            } catch (Exception e) {// desabilita o dialog
                dialog.dismiss();
                finalizaDialogo(true, e.toString());
            }
            return null;
        }
    }.execute();
}

private void finalizaDialogo(final boolean temErro, final String msg) {
    handler.post(new Runnable() {

        @Override
        public void run() {
            if (temErro)
                Toast.makeText(ListaItensProdutosActivity.this, msg,
                        Toast.LENGTH_LONG).show();
            else
                AtualizarTela();
            // finaliar o dialogo
            dialog.dismiss();
        }
    });
}

private void AtualizarTela() {
    String[] from = new String[] { "nome", "id" };
    int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
    int layoutNativo = android.R.layout.two_line_list_item;

    listItens.setAdapter(new SimpleAdapter(this, dataList, layoutNativo,
            from, to));

    if (listItens == null) {

        Toast.makeText(ListaItensProdutosActivity.this,
                "Não foi encontrado dados nesta busca.", Toast.LENGTH_LONG)
                .show();
        finish();

    }

}

我的XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/layoutCabecalho"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/barra"
        android:gravity="center" >

        <Button
            android:id="@+id/btnClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancelar"
            android:textSize="14sp" />

        <LinearLayout
            android:id="@+id/layoutCabecalhoEsquerdo"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|left" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutCabecalhoCentro"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center" >

            <TextView
                android:id="@+id/textoTitulo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Listagem"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#FFFFFF" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutCabecalhoDireito"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|right" >
        </LinearLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/listaItensProdutos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

你需要一个自定义适配器列表https://github.com/cplain/custom-list是我为朋友做的一个粗略的例子,希望它可以提供帮助

答案 1 :(得分:0)

几乎工作,加上我没有正确使用它。

只带来表格的最后一条记录

我是这样做的 课堂活动

public class ListaPacoteActivity extends ListActivity {

    private GetConnection gc = new GetConnection();
    private Context pacoteList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        List<Pacotes> pacoteList = new ArrayList<Pacotes>();

        DSRESTConnection conn = gc.getConnection();
        TServerMethods1 serv = new TServerMethods1(conn);
        TDataSet ds;
        // String idCharCodCli = VendaClienteActivity.edtCliente.getText()
        // .toString();
        // String[] idNumCli = idCharCodCli.split("-");
        // String idCharCodPro = VendaClienteActivity.edtProfissional.getText()
        // .toString();
        // String[] idNumPro = idCharCodPro.split("-");
        try {
            ds = serv.ConsultaDadosPacote("1000", "4");// idNumCli[0],
                                                        // idNumPro[0]);
            Pacotes pct = new Pacotes();

            while (ds.next()) {
                String concPacote = ds.getValue("CODIGO_PACOTE").toString()
                        + " - " + ds.getValue("NOME_PACOTE").toString();
                String concSessao = "Sessão: "
                        + ds.getValue("CODIGO_SERVICO").toString() + " - "
                        + ds.getValue("NOME_SERVICO").toString();
                String concCreditos = "Créditos restantes: "
                        + ds.getValue("CREDITOS_RESTANTES").toString();
                String concStatus = "Status: "
                        + ds.getValue("STATUS").toString();
                String concHabilidade = "Habilidade: "
                        + ds.getValue("HABILIDADE").toString() + " - "
                        + ds.getValue("CODIGO_COMPRA").toString();
                pct.setPacote(concPacote);
                pct.setSessao(concSessao);
                pct.setCreditos(concCreditos);
                pct.setStatus(concStatus);
                pct.setHabilidade(concHabilidade);
                // adiciona os dados no list
                pacoteList.add(pct);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        setListAdapter(new PacoteAdapter(getBaseContext(), pacoteList));
    }

}

POJO的

public class Pacotes {
    private String pacote = "";
    private String sessao = "";
    private String creditos = "";
    private String status = "";
    private String habilidade = "";
    public String getPacote() {
        return pacote;
    }
    public void setPacote(String pacote) {
        this.pacote = pacote;
    }
    public String getSessao() {
        return sessao;
    }
    public void setSessao(String sessao) {
        this.sessao = sessao;
    }
    public String getCreditos() {
        return creditos;
    }
    public void setCreditos(String creditos) {
        this.creditos = creditos;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getHabilidade() {
        return habilidade;
    }
    public void setHabilidade(String habilidade) {
        this.habilidade = habilidade;
    }
}

类适配器

public class PacoteAdapter extends BaseAdapter {
    private Context context;
    private List<Pacotes> pacoteList;

    public PacoteAdapter(Context context, List<Pacotes> pacotelist){
        this.context = context;
        this.pacoteList = pacotelist;
    }

    @Override
    public int getCount() {
        return pacoteList.size();
    }

    @Override
    public Object getItem(int position) {
        return pacoteList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Recupera o pacote da posição atual
        Pacotes pct = (Pacotes) getItem(position); 

        // Cria uma instância do layout XML para os objetos correspondentes
        // na View
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = inflater.inflate(R.layout.custom_row_view, null);
        // pacote
        TextView textPacote = (TextView) view.findViewById(R.id.pacote);
        textPacote.setText(pct.getPacote());
        //sessão
        TextView textSessao = (TextView) view.findViewById(R.id.sessao);
        textSessao.setText(pct.getSessao());
        //creditos
        TextView textCredito = (TextView) view.findViewById(R.id.credito);
        textCredito.setText(pct.getPacote());
        //status
        TextView textStatus = (TextView) view.findViewById(R.id.status);
        textStatus.setText(pct.getStatus());
        //habilidade
        TextView textHabilidade = (TextView) view.findViewById(R.id.habilidade);
        textHabilidade.setText(pct.getHabilidade());

        return view;
    }   
}

是XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:orientation="vertical" >

    <TextView
        android:id="@+id/pacote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000FF"
        android:textSize="14sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/sessao"
        android:textColor="#228B22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/credito"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/habilidade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>