0

我带来了两个领域的网络服务并填充了一个列表视图。

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

如何在列表视图中添加更多子项?

我想在列表视图中包含更多表字段。

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>
4

2 回答 2

1

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

于 2013-01-17T02:02:36.013 回答
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));
    }

}

波乔

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>
于 2013-01-17T21:32:50.483 回答