无法在ListView中同时显示从编辑文本和时间中获取的数据

时间:2014-06-02 13:42:23

标签: android android-listview

我正在尝试制作一个采用这种状态的应用。所以我把编辑文本放在状态和列表视图中,其中包含输入的文本和更新状态的时间。但是当我点击进入时,我无法将编辑文本和日期中的数据都放入列表中。

这是java代码

    package com.veda.nofragment;

    import java.util.ArrayList;
    import java.util.Calendar;
    import android.support.v7.app.ActionBarActivity;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.os.Bundle;

    public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editText = (EditText) findViewById(R.id.editText);
    ListView listView = (ListView) findViewById(R.id.listView);

    // creating a array list for statuses
    final ArrayList<String> statusItems = new ArrayList<String>();

    // creating an array adapter for binding the array to the statuses
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, statusItems);

    listView.setAdapter(aa);

    editText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if (keyCode == KeyEvent.KEYCODE_ENTER) {

                    String editTextString = editText.getText().toString();
                    statusItems.add(0, editTextString);
                    aa.notifyDataSetChanged();
                    editText.setText("");
                    return true;
                }
            return false;
        }
    });
}

public class InsertMultiText extends ArrayAdapter<String> {

    // getting time to display the time of status
    Calendar c = Calendar.getInstance();
    final int mHour = c.get(Calendar.HOUR);
    final int mMinute = c.get(Calendar.MINUTE);
    final int mSeconds = c.get(Calendar.SECOND);

    public InsertMultiText() {
        super(MainActivity.this, R.layout.text_item, R.id.list_large);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = super.getView(position, convertView, parent);
        TextView size = (TextView) row.findViewById(R.id.list_small);
        size.setText(mHour + ":" + mMinute + ":" + mSeconds);
        return (row);
    }

}
  }

这里是activitymain.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin" >

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:hint="@string/editHint"
    android:maxHeight="50dp" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:background="@drawable/roundedcorner"
    android:divider="#dbdbdbdb"
    android:dividerHeight="1dp"
    android:padding="8dp"
    android:scrollbarFadeDuration="1000" >
</ListView>

这是text_item.xml

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

<TextView
    android:id="@+id/list_large"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/largeText"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/list_small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/list_large"
    android:text="@string/smallText"
    android:textAppearance="?android:attr/textAppearanceSmall" />

1 个答案:

答案 0 :(得分:0)

您的代码无效,因为您已创建适配器InsertMultiText,但在onCreate中您没有使用它,您使用的是简单的ArrayAdapter,而不是您使用的那个创建

我想我用简单的适配器做得更简单了,如果你想用你的适配器告诉我,我会修改我的代码:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText editText = (EditText) findViewById(R.id.editText);
        ListView listView = (ListView) findViewById(R.id.listView);

        // creating a array list for statuses
        final ArrayList<String> statusItems = new ArrayList<String>();
        // creating an array adapter for binding the array to the statuses
        final ArrayAdapter<String> aa;

        aa = new ArrayAdapter<String>(this,
                                      android.R.layout.simple_list_item_1, statusItems);

        listView.setAdapter(aa);

        editText.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    Calendar c = Calendar.getInstance();
                    final int mHour = c.get(Calendar.HOUR);
                    final int mMinute = c.get(Calendar.MINUTE);
                    final int mSeconds = c.get(Calendar.SECOND);

                    String text =
                            editText.getText().toString() + "\n\n Time » " + mHour + ":" + mMinute
                                    + ":"
                                    + mSeconds;

                    statusItems.add(0, text);
                    aa.notifyDataSetChanged();
                    editText.setText("");
                    return true;
                }
                return false;
            }
        });
    }
}

我已编辑activity_main

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:singleLine="true" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/editText"
    android:divider="#dbdbdbdb"
    android:dividerHeight="1dp"
    android:padding="8dp"
    android:scrollbarFadeDuration="1000" >
</ListView>

这里有结果:

result

希望它有所帮助。