如何将此活动拆分为2个ListView

时间:2016-06-19 22:01:40

标签: java android android-layout listview

我有这个列表视图与饮料(bebidas),但我想在汉堡包的同一活动中添加一个新的列表视图。 我的代码无效,因为它不断重复相同的名称。

public class Cardapios extends AppCompatActivity {



    DbController db;
    ListView listview_cardapios, listview_hamburguers;

    public int id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardapios);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        db = new DbController(getBaseContext());


        Bundle b = getIntent().getExtras();
        if (b != null) {
            id = b.getInt("id");
        }


        preencheLista();
       // preencheLista2();
    }

    private void preencheLista() {
        ArrayList<CardapioGetter> itens = new ArrayList<CardapioGetter>();


        Cursor c = db.getBebidas(id);


        while (! c.isAfterLast()) {
            itens.add(new CardapioGetter(c.getString(c.getColumnIndex(DbHelper.NOME_BEBIDAS)),c.getString(c.getColumnIndex(DbHelper.VALOR_BEBIDAS))));

            c.moveToNext();
        }


        defineAdapter(itens);


    }


    private void defineAdapter(ArrayList<CardapioGetter> itens) {
        listview_cardapios = (ListView) findViewById(R.id.listview_cardapios);
        AdapterListCardapios adapter = new AdapterListCardapios(getApplicationContext(), itens);
        listview_cardapios.setAdapter(adapter);

        listview_cardapios.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CardapioGetter itemValue = (CardapioGetter) listview_cardapios.getItemAtPosition(position);


            }
        });
    }

    private void preencheLista2() {
        ArrayList<CardapioGetter> itens = new ArrayList<CardapioGetter>();

        Cursor h = db.getHamburguers(id);
        while (! h.isAfterLast()) {
            itens.add(new CardapioGetter(h.getString(h.getColumnIndex(DbHelper.NOME_HAMBURGUERS)),h.getString(h.getColumnIndex(DbHelper.VALOR_HAMBURGUERS))));

            h.moveToNext();
        }


        defineAdapter2(itens);
    }


    private void defineAdapter2(ArrayList<CardapioGetter> itens) {
        listview_hamburguers = (ListView) findViewById(R.id.listview_hamburguers);
        AdapterListCardapios adapter = new AdapterListCardapios(getApplicationContext(), itens);
        listview_hamburguers.setAdapter(adapter);

        listview_hamburguers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CardapioGetter itemValue = (CardapioGetter) listview_hamburguers.getItemAtPosition(position);


            }
        });
        }

布局有4个文本视图,我用数据库中的数据替换。     我想把2从botton分成一个新的列表视图。

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textview_valorbebida"
        android:textColor="@color/abc_input_method_navigation_guard"
        android:layout_marginTop="133dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textview_nomebebida"
        android:textColor="@color/abc_input_method_navigation_guard"
        android:layout_alignTop="@+id/textview_valorbebida"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Bebidas"
        android:id="@+id/textView2"
        android:layout_above="@+id/textview_nomebebida"
        android:layout_alignParentStart="true"
        android:textColor="@color/abc_input_method_navigation_guard" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Pratos"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView4"
        android:textColor="@color/abc_input_method_navigation_guard" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textview_nomehamburguer"
        android:layout_below="@+id/textView4"
        android:layout_alignParentStart="true"
        android:textColor="@color/abc_input_method_navigation_guard" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textview_valorhamburguer"
        android:layout_alignTop="@+id/textview_nomehamburguer"
        android:layout_alignParentEnd="true"
        android:textColor="@color/abc_input_method_navigation_guard" />



</RelativeLayout>

我正在使用的adapterlist类。将itens设置为textviews。     我想在列表视图上分开的文本视图。

    public class AdapterListCardapios extends BaseAdapter{


    private LayoutInflater mInflater;
    private ArrayList<CardapioGetter> itens;


    public AdapterListCardapios(Context context, ArrayList<CardapioGetter> itens) {
        //Itens que preencheram o listview
        this.itens = itens;
        //responsavel por pegar o Layout do item.
        mInflater = LayoutInflater.from(context);
    }


    public int getCount() {
        return itens.size();
    }

    /**
     * Retorna o item de acordo com a posicao dele na tela.
     */

    public CardapioGetter getItem(int position) {
        return itens.get(position);
    }

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

    public View getView(int position, View view, ViewGroup parent) {
        //Pega o item de acordo com a posição.
        CardapioGetter item = itens.get(position);
        //infla o layout para podermos preencher os dados

        view = mInflater.inflate(R.layout.textview_cardapios, null);

       ((TextView) view.findViewById(R.id.textview_nomehamburguer)).setText(item.getNome_hamburguers());
       ((TextView) view.findViewById(R.id.textview_valorhamburguer)).setText("R$ "+ item.getValor_hamburguers());

        ((TextView) view.findViewById(R.id.textview_nomebebida)).setText(item.getNome_bebidas());
        ((TextView) view.findViewById(R.id.textview_valorbebida)).setText("R$ "+ item.getValor_bebidas());

        return view;
    }




What I want is. 1 listview with drinks and 1 listview with food on the same activity. thanks

1 个答案:

答案 0 :(得分:0)

  CriarInterface p = new CriarInterface();
    new Thread(p).start();

}


public class CriarInterface implements Runnable {

    @Override
    public void run() {
        preencheLista();
        preencheLista2();
    }
}

的工作。