我无法在对话框中获取textview值

时间:2013-03-28 04:13:43

标签: android android-listview

在通过TextViews

绑定动态BaseAdapter时,我在对话框中遇到异常

我可以获得对话框,但它不会绑定TextView值地狱..

Train_Origin_Destination.java

public void loadAutocomplete1(String strCmdTrain) {

c.moveToFirst();
    if (c.isAfterLast() == false) {

        do {            

            listFrom1 = new ArrayList<String>(Arrays.asList(strComments));
            listTo1 = new ArrayList<String>(Arrays.asList(strComments));

            listFrom1.removeAll(Collections.singleton(null));

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_Start, "Hello");

            listData.add(map);
            Log.e("map", map.toString());
            Log.e("list4", listData.toString());

            customAdapter = new MyCustomAdapter2(
                    Train_Origin_Destination.this, listData);
            lvTrainTime1.setAdapter(customAdapter);


        } while (c.moveToNext());

    }

onButtonClick事件

public void onClick(View arg0) {

    Log.d("Dialog", "Dialog");
    dialog = new Dialog(context1);
    //dialog.setContentView(R.layout.traintimings_dialog);
    //dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;
    dialog.setTitle("Train Timings");
    //dialog.setContentView(R.id.lvTrainTimings1);

if (chkSunday.isChecked()) {
        if (Area_Index < Area_Index1) {
            Log.d("Sunday", "Sunday");
        } else if (Area_Index > Area_Index1) {
            Log.d("SundayReverse", "SundayReverse");
        }
    } else {
        if (Area_Index < Area_Index1) {
            Log.d("Normal", "Normal");

            if (strSpSelectedItem.contentEquals("Beach - Tambaram")) {
                int id = Area_Index + 1;
                int id1 = Area_Index1 + 1;
                /*
                 * strCmdTrainFrom =
                 * "Beach_Thirumalpur_WeekD ays where _id='" + id +
                 * "' or _id='" + id1 + "'";
                 */
                strCmdTrainFrom = "Beach_Thirumalpur_WeekDays where _id='"
                        + id + "'";
                loadAutocomplete1(strCmdTrainFrom);

            }
        } else if (Area_Index > Area_Index1) {
            Log.d("NormalReverse", "NormalReverse");
        }
    }

    Log.d("DialogEndB4", listData.toString());
    dialog.show(); 
    Log.d("DialogEnd", "DialogEnd");
}

MyCustomAdapter2.java

    public class MyCustomAdapter2 extends BaseAdapter {

    private String[] strKeys;
    private ArrayList<HashMap<String, String>> alData;
    private Context context;
    TextView txtBusNo, txtBusVia, txtBusServiceType, txtBusJourneyTime;
    TextView[] tvStart;
    ListView lvTrainTime1;
    View rowView2;

    public MyCustomAdapter2(Context context,
            ArrayList<HashMap<String, String>> listData) {

        super();
        Log.d("Custom", "Custom");
        this.alData = listData;
        this.context = context;
        Log.d("alData", alData.toString());
        Log.d("Custom1", "Custom1");

    }

    public int getCount() {
        // TODO Auto-generated method stub
        Log.d("alData.size()", String.valueOf(alData.size()));
        return alData.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("View", "View");
        rowView2 = convertView;
        if (rowView2 == null) {

            Log.d("View1", "View1");
            Train_Origin_Destination trDetails = new Train_Origin_Destination();
            // Inflate the layout, list_view.xml, in each row.
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            Log.d("View2", "View2");
            rowView2 = inflater
                    .inflate(R.layout.textview_autofrom, null, false);
}

            txtBusNo = (TextView) rowView2.findViewById(R.id.idTvAutoFrom1);


            Log.d("View3", "View3");
            HashMap<String, String> hashmap = new HashMap<String, String>();


            txtBusNo.setText("Bus No : Hello");

            Log.d("View30", "View30");


            Log.d("View5", String.valueOf(rowView2));
        } else {

        }
        return rowView2;
    }

PLZ帮助我...我做错了什么:)。

1 个答案:

答案 0 :(得分:0)

首先,为customArray创建一个构造函数,该构造函数具有对话框的引用:

Dialog _dialog;

public MyCustomAdapter2(Context context,
            ArrayList<HashMap<String, String>> listData,Dialog dog) {
//put all the code you have here and add this:
_dialog=dog;
}

并在此处传递对话框的引用:

 customAdapter = new MyCustomAdapter2(
                    Train_Origin_Destination.this, listData,dialog);

您的getView中的下一步将此代码放在您要获取TextView的位置:

txtBusNo = (TextView) _dialog.findViewById(R.id.idTvAutoFrom1);

无论何时完成对话框中的文本,只需调用

即可
 lvTrainTime1.setAdapter(customAdapter);

如果这不起作用,那么您可以尝试拨打customAdapter.notifyDataSetChanged();