有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Listview适配器在关闭对话框后不显示列表列

设置

在我的应用程序中,我有一个创建要保存在数据库中的数据行的部分。每一行都是从文本框、数字框构建的,然后“设置”到列表视图中显示。稍后,用户可以打开之前创建的“计划”并查看数据

我创建了一个listview,一个显示行的自定义布局,并了解了如何添加/显示每一新行。但是,当我执行“打开”时,我会创建一个对话框来显示计划列表,用户选择“打开”,并且应该显示行。他们没有。我跟踪代码,看到正在填充适配器的arraylist,并调用notifydatasetchanged,但主listview仍然为空

我使用的不是客户适配器类,而是简单的适配器

这是活动的开始。我还在学习安卓编码,所以在学习的过程中我倾向于使用通用名称

public class TrackItEqMainActivity extends AppCompatActivity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
public int rowCount = 1;
private static final String eTAG = "Exception";
final Context context = this;

// global variables needed for processing the add gaits display
**private ListView lstGaits;
private ListAdapter adapter;
private ArrayList<HashMap<String, String>> mylist;**

// *****

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.i(eTAG, "onCreate Build");

    setTitle(R.string.activityManagePlans);
    setContentView(R.layout.activity_track_it_eq_build);

    Toolbar mySecondaryToolbar = (Toolbar) findViewById(R.id.my_secondaryToolbar);
    setSupportActionBar(mySecondaryToolbar);

    setActivityBuildListeners();

    // set the objects needed to handle the dynamic gaits list
    ListView lstGaits = (ListView) findViewById(R.id.lstMyGaits);
    mylist = new ArrayList<HashMap<String, String>>();
    try {
        adapter = new SimpleAdapter(this, mylist, R.layout.gaits_detail,
                new String[] { "keyGait", "keyTime" }, new int[] {
                R.id.txtdisplayGait, R.id.txtdisplayTime  });
        lstGaits.setAdapter(adapter);
    } catch (Exception e) {
        Log.i(eTAG, e.getMessage());
    }

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

因此,当用户点击一个新按钮时,会出现一组控件,允许用户选择步态并输入时间。然后他们点击set按钮,在set onClick中,我将数据添加到内部数组,并在屏幕上显示自定义布局

    private Button.OnClickListener onClick_btnSet = new OnClickListener() {
    @Override
    public void onClick(View v) {

        Spinner spinner = (Spinner) findViewById(R.id.spinner_gaits);
        TextView textGait = (TextView) spinner.getSelectedView();
        String gtResult = textGait.getText().toString();

        TextView txtTime = (TextView) findViewById(R.id.txtSelected);
        String tmeResult = txtTime.getText().toString();

        buildDisplayLine(true,gtResult,tmeResult);

        txtTime.setText("");
        spinner.requestFocus();

    }
};

构建线并添加到阵列的函数如下:

   private void buildDisplayLine(boolean isNew, String gait, String time) {

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("keyGait",gait);                  // put the col data in
    map2.put("keyTime", time);      // put the col data in
    mylist.add(map2);

    if (isNew) {
        ((BaseAdapter) (adapter)).notifyDataSetChanged();
    }

}

尽管有一个奇怪的副作用,就是如果数字键盘可见,列表将不会显示。当我将其清除时,列表出现

如果用户想要查看或更改计划,请点击“打开”按钮。它将显示一个对话框,其中显示计划列表。点击平面图,所有元素都应显示:

    private final ThreadLocal<OnClickListener> onClick_btnOpen = new ThreadLocal<OnClickListener>() {
    @Override
    protected OnClickListener initialValue() {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.activity_track_it_eq_open_plan);
                dialog.setTitle("Open Exercise Plan...");

                Button diaCancelButton = (Button) dialog.findViewById(R.id.btnCancelOpen);
                diaCancelButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });


                // get a list of files from the local app plans
                final eqDatabaseService eqDB = new eqDatabaseService(context, 2);

                ArrayList<HashMap<String,String>> allPlans = eqDB.getPlanList();

                if (allPlans.isEmpty()) {
                    Toast toast = new Toast(context);
                    toast.setGravity(Gravity.TOP, 0, 0);
                    Toast.makeText(context, "No Files to open, Please create one!", Toast.LENGTH_LONG).show();
                    return;
                }

                ListView lvPlan = (ListView) dialog.findViewById(R.id.lvPlans);

                try {

                    adapter = new SimpleAdapter(context, allPlans, R.layout.plans_columns,
                            new String[] { "keyName", "keyENum" }, new int[] {
                            R.id.txtPlanName, R.id.txtNumElements  });

                    lvPlan.setAdapter(adapter);
                } catch (Exception e) {

                }
                //********************************************************



                lvPlan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                        TextView txtplan = (TextView) v.findViewById(R.id.txtPlanName);
                        String planName = txtplan.getText().toString();

                        List<eqSessions_dt> myPlan = eqDB.getCurrentPlan(planName);

                        clearGrid();

                        for (eqSessions_dt row : myPlan) {

                            buildDisplayLine(false, row.get_gait(),Integer.toString(row.get_time()));
                        }


                        ((BaseAdapter)(adapter)).notifyDataSetChanged();

                        dialog.dismiss();


                    }
                });
                dialog.show();
            }
        };
    }
};

我在这方面做过尝试,也看过其他类似的问题和答案,但没有看到任何可以解释为什么notify不起作用的东西。我避免使用自定义适配器,一开始尽量保持简单。我遗漏了什么阻止列表显示。我看到arraylist被填充,所有相关对象都是活动的全局对象。我会继续玩,但我希望其他人能看到我错过了什么

这是活动xml`

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:orientation="vertical"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:weightSum="1"
    tools:context="com.newproject.jhull3341.trackiteq.TrackItEqMainActivity">

    <安卓.support.v7.widget.Toolbar
        安卓:id="@+id/my_secondaryToolbar"
        安卓:layout_width="fill_parent"
        安卓:layout_height="?attr/actionBarSize"
        安卓:background="#236dc1"
        安卓:elevation="4dp"
        安卓:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        安卓:layout_alignParentTop="true"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentStart="true" />
    <GridLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical"
        安卓:id="@+id/lyFileAction">

        <Button
            style="?安卓:attr/buttonStyleSmall"
            安卓:layout_width="92dp"
            安卓:layout_height="match_parent"
            安卓:text="New"
            安卓:id="@+id/btnNew"
            安卓:layout_row="0"
            安卓:layout_column="0"
            安卓:layout_gravity="center_horizontal" />

        <Button
            style="?安卓:attr/buttonStyleSmall"
            安卓:layout_width="92dp"
            安卓:layout_height="match_parent"
            安卓:text="Open"
            安卓:id="@+id/btnOpen"
            安卓:layout_row="0"
            安卓:layout_column="1"
            安卓:layout_gravity="center_horizontal" />

        <Button
            style="?安卓:attr/buttonStyleSmall"
            安卓:layout_width="92dp"
            安卓:layout_height="match_parent"
            安卓:text="Save"
            安卓:id="@+id/btnSave"
            安卓:layout_row="0"
            安卓:layout_column="2"
            安卓:layout_gravity="center_horizontal" />

        <Button
            style="?安卓:attr/buttonStyleSmall"
            安卓:layout_width="92dp"
            安卓:layout_height="match_parent"
            安卓:text="Delete"
            安卓:id="@+id/btnDelete"
            安卓:layout_row="0"
            安卓:layout_column="3"
            安卓:layout_gravity="center_horizontal" />
    </GridLayout>

    <GridLayout
        安卓:layout_width="367dp"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/grdEntry">

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceMedium"
            安卓:text="@string/label_time"
            安卓:id="@+id/lblTime"
            安卓:layout_row="0"
            安卓:layout_column="1"
            安卓:layout_marginBottom="5dp" />

        <Spinner
            安卓:layout_width="147dp"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/spinner_gaits"
            安卓:layout_row="1"
            安卓:layout_column="0"
            安卓:focusable="true" />

        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:textAppearance="?安卓:attr/textAppearanceMedium"
            安卓:text="@string/label_Gait"
            安卓:id="@+id/lblGait"
            安卓:layout_row="0"
            安卓:layout_column="0"
            安卓:layout_marginBottom="5dp" />

        <EditText
            安卓:layout_width="124dp"
            安卓:layout_height="wrap_content"
            安卓:id="@+id/txtSelected"
            安卓:layout_row="1"
            安卓:layout_column="1"
            安卓:layout_gravity="fill_horizontal|center_vertical"
            安卓:singleLine="true"
            安卓:numeric="integer" />

        <Button
            style="?安卓:attr/buttonStyleSmall"
            安卓:layout_width="92dp"
            安卓:layout_height="fill_parent"
            安卓:text="Set"
            安卓:id="@+id/btnSet"
            安卓:layout_row="1"
            安卓:layout_column="2"
            安卓:layout_gravity="center_vertical"
            安卓:clickable="true" />

    </GridLayout>

    <ListView
        安卓:id="@+id/lstMyGaits"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_alignParentBottom="true"
        安卓:layout_centerHorizontal="true"
        安卓:layout_below="@+id/grdEntry"  />
</LinearLayout>

这是每个显示行的自定义布局`

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:orientation="horizontal" 安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <TextView
        安卓:id="@+id/txtdisplayGait"
        安卓:layout_width="wrap_content"
        安卓:layout_height="44dp"
        安卓:layout_weight="1"
        安卓:gravity="center_vertical"
        安卓:text="TextView"
        安卓:textSize="18sp" />

    <TextView
        安卓:id="@+id/txtdisplayTime"
        安卓:layout_width="80dp"
        安卓:layout_height="44dp"
        安卓:gravity="center_vertical"
        安卓:text="TextView"
        安卓:textSize="18sp" />

    <ImageButton
        安卓:id="@+id/btnRemoveLine"
        安卓:layout_width="25dp"
        安卓:layout_height="44dp"
        安卓:layout_weight="0.39"
        安卓:background="@color/wallet_bright_foreground_holo_dark"
        安卓:elevation="1dp"
        安卓:src="@安卓:drawable/btn_dialog" />
</LinearLayout>`

共 (0) 个答案