无法在textview中设置文本

时间:2017-05-02 17:45:08

标签: android

我无法在EditText中设置文本

我的代码是

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class ViewBillActivity extends AppCompatActivity {

    ListView lv_show_bill;
    ProductListAdapter adapter;
    ArrayList<Product> dataListProduct;
    ProductDatabase productDatabase = new ProductDatabase(this);
    EditText et_enterBillNo,editText2,editText3;
    TextView tv_billview_total_amt, tv_show_custname, tv_show_date;
    Button btn_showBill;
    Product product;
    Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_bill);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        dataListProduct = new ArrayList<>();

        et_enterBillNo = (EditText)findViewById(R.id.et_enterBillNo);
        tv_show_date = (TextView) findViewById(R.id.tv_show_date);
        tv_show_custname = (TextView) findViewById(R.id.tv_show_custname);
        tv_billview_total_amt = (TextView)findViewById(R.id.tv_billview_total_amt);
        lv_show_bill = (ListView)findViewById(R.id.lv_show_bill);
        //dialog box

        editText2 = (EditText)findViewById(R.id.editText2);
        editText3 = (EditText)findViewById(R.id.editText3);

        btn_showBill = (Button)findViewById(R.id.btn_showBill);
        btn_showBill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String showbillno = et_enterBillNo.getText().toString().trim();
                int totalamt = 0;
                dataListProduct = productDatabase.dispalySingleBill(Integer.valueOf(showbillno));
                for (int i = 0; i < dataListProduct.size();i++) {
                    product = dataListProduct.get(i);
                    totalamt += product.amt;
                }
                String dateAndCustName[] = productDatabase.displayDateAndCustName(Integer.valueOf(showbillno));
                tv_show_date.setText("Date : " + dateAndCustName[0]);
                tv_show_custname.setText("Customer : "+ dateAndCustName[1]);
                tv_billview_total_amt.setText("Total : "+ String.valueOf(totalamt));
                adapter = new ProductListAdapter(getApplicationContext(), dataListProduct);
                lv_show_bill.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });

        lv_show_bill.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.update_bill_dialog_box);
                dialog.setTitle("Update bill");
               EditText editText = (EditText)findViewById(R.id.editText);
                String s = dataListProduct.get(position).getItem();
              editText.setText(s); 
              dialog.show();
              return true;

            }

        });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}
  

editText.setText(一个或多个); //这一行正在杀死应用

我要做的是创建一个对话框,从数据库中获取数据并将其放在自定义对话框中的edittext字段中。我真的不知道我在做什么错误...谢谢推进..

1 个答案:

答案 0 :(得分:0)

使用

EditText editText = (EditText) dialog.findViewById(R.id.editText);

而不是

EditText editText = (EditText) findViewById(R.id.editText);