0

私は 2 つのフィールド Web サービスを持ってきて、リストビューを設定しています。

複数行のテキストビューが必要です。私のコンポーネントは少なくとも 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 に答える