java - edittext 中的输入文本恢复为可扩展 ListView 中的预设文本

标签 java android eclipse expandablelistview

我正在使用一个可扩展的 ListView ,其子布局包含 1 个 EditText、2 个 ToggleButtons 和 1 个搜索栏。我能够使 EditText 处于焦点状态并弹出键盘,并且我能够在 EditText 中输入任何内容,但是当我向下滚动远离聚焦的 EditText 或从可扩展 ListView 中打开一个新组时,文本我写的内容恢复为预设文本。

切换按钮和搜索栏都不会恢复到之前的状态。

Prestate During After

我尝试在 xml 中添加 android:descendantFocusability="beforeDescendants"和 android:descendantFocusability="afterDescendants"但没有成功。在 AndroidManifest 中添加 android:windowSoftInputMode="adjustPan"> 帮助我解决了之前的问题,即无法弹出键盘,而只是在瞬间闪烁。

我的 MainActivity 如下所示:

public class MainActivity extends ExpandableListActivity {
private List<EditText> editTextList = new ArrayList<EditText>();
public FileOutputStream fOut;
public String str;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
EditText editText;
String temp;


@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {

    editText = (EditText) findViewById(R.id.grp_child);
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
        int count = 200;

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                createGroupList(),              // Creating group List.
                R.layout.group_row,             // Group item layout XML.
                new String[] { "Turn" },  // the key of group item.
                new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.
                createChildList(),              // childData describes second-level entries.
                R.layout.child_row,             // Layout for sub-level entries(second level).
                new String[] {"Sub Item"},      // Keys in childData maps to display.
                new int[] { R.id.grp_child}     // Data under the keys above go into these TextViews.
            );
        setListAdapter( expListAdapter );       // setting the adapter in the list.

    }catch(Exception e){
        System.out.println("Errrr +++ " + e.getMessage());
    }
}

/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < 200 ; ++i ) { // 15 groups........
        HashMap m = new HashMap();
        m.put( "Turn","Turn" + i ); // the key and it's value.
        result.add( m );
      }
      return (List)result;
}

/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 200 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
      /* each group need each HashMap-Here for each group we have 3 subgroups */
      ArrayList secList = new ArrayList();
      for( int n = 0 ; n < 1 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", "Sub Item " + n );
        secList.add( child );
      }
     result.add( secList );
    }
    return result;
}
public void  onContentChanged  () {
    System.out.println("onContentChanged");
    super.onContentChanged();
}

private View.OnClickListener submitListener = new View.OnClickListener() {
    public void onClick(View view) {
        StringBuilder stringBuilder = new StringBuilder();
        for (EditText editText : editTextList) {
            stringBuilder.append(editText.getText().toString());
        }
    }
};

/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
    System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
    return true;
}

/* This function is called on expansion of the group */
public void  onGroupExpand  (int groupPosition) {
    try{
         System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
    }catch(Exception e){
        System.out.println(" groupPosition Errrr +++ " + e.getMessage());
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.menu, menu);
    return true;
}
@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle("Exit")
            .setMessage(
                    "(Do not forget to save before exiting)"
                            + "\nAre you sure you want to exit?")
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            finish();
                            System.exit(0);
                        }
                    }).setNegativeButton("No", null).show();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.menu_save:
        StringBuilder stringBuilder = new StringBuilder();
        for (EditText editText : editTextList) {
            stringBuilder.append(editText.getText().toString());
        }
        String[] items = new String[editTextList.size()];
        for (int i = 0; i < editTextList.size(); i++) {
            items[i] = editTextList.get(i).getText().toString();

        }

        File path = new File(Environment.getExternalStorageDirectory(),
                "/text");
        if (!path.exists()) {
            path.mkdirs();
        }

        try {
            File text = new File(path, dateFormat.format(date) + ".txt");
            FileWriter writer = new FileWriter(text);
            for (String str : items) {
                writer.write(str);
            }
            writer.flush();
            writer.close();
            System.out.println("Nu fungerar det");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(MainActivity.this, "Save is Selected",
                Toast.LENGTH_SHORT).show();
        return true;

    case R.id.menu_about:
        Toast.makeText(
                MainActivity.this,
                "This app is made as a tool to capture data of game sessions of the City Game",
                Toast.LENGTH_LONG).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}

最佳答案

我知道这个答案已经太晚了。不过,它可能会对其他用户有所帮助。

如果 ListView 展开到屏幕之外,

Edittext 通常在 ListView 中无法正常工作。当焦点改变时, View 被回收,因此 Edittext 失去了它的值(value)。您可以使用 TableLayout 来实现相同的结果。

关于java - edittext 中的输入文本恢复为可扩展 ListView 中的预设文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655718/

相关文章:

java - AppEngine 连接的 Android 项目中的注释失败

java - 跳过可选的 bean 引用属性

java - 在 Java 中模拟 6502 处理器时,对象开销重要吗? (使用包装器而不是原始类型)

java - Spring MVC : RequestBody + ResponseEntity: How to return an object/entity without some of the obj fields

Android:使用 fragment 共享元素转换

java - Eclipse 中缺少动态 Web 项目选项

java - java迷宫生成器中的算法错误

java - message.obj 的含义 [android 网络]

java - 在viewpager中单击时如何在新 Activity 中打开图像?

Eclipse - “Workspace in use or cannot be created, chose a different one.” 即使我删除了 .lock