gpt4 book ai didi

android - ListView 不显示文本文件中的文本

转载 作者:行者123 更新时间:2023-11-30 00:53:12 24 4
gpt4 key购买 nike

我有一个关于 Activity 的 ListView ,我想在其中显示我从文本文件中读取的信息。这是上述 Activity (类)的代码:

public class ViewData extends AppCompatActivity {

//code to show strings from file
private ArrayList<String> readFromFile(Context context) {

ArrayList<String> receiveString=new ArrayList<String>( );

try {
InputStream inputStream = context.openFileInput(AddDataActivity.FileName);

if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tempString="";
while ((tempString=bufferedReader.readLine()) != null) {

receiveString.add(tempString);
}

inputStream.close();

}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}

return receiveString;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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();
}
});

ListView listView=(ListView)findViewById(R.id.listView);

// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,readFromFile(getApplicationContext()));


// Assign adapter to ListView
listView.setAdapter(adapter);

// ListView Item Click Listener
// listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
//
// // ListView Clicked item index
// int itemPosition = position;
//
// // ListView Clicked item value
// String itemValue = (String) listView.getItemAtPosition(position);
//
// // Show Alert
// Toast.makeText(getApplicationContext(),
// "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
// .show();
//
// }
//
// });



}


}

我有一个应该触发此 Activity (启动它)的菜单按钮,在 onCreate 中,我想从文件中读取并显示我在另一个 Activity 中保存到文件中的文本内容。但是,我目前看到以下结果,无论对提到的文本文件保存什么更改,结果似乎总是一样的:
enter image description here

这是我的布局的内容 View :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="mhd.myupliftingmate.ViewData"
tools:showIn="@layout/activity_view_data">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

这是我的主菜单布局,其中调用了 Activity ViewData:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mhd.myupliftingmate.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:icon="@drawable/settings"
app:showAsAction="ifRoom"
/>
<item app:showAsAction="ifRoom"
android:orderInCategory="101"
android:title="Add Data"
android:id="@+id/action_addData"
android:onClick="openAddActivity"
android:icon="@drawable/input"
/>
<item
android:orderInCategory="102"
android:title="View Data"
android:id="@+id/action_viewData"
android:onClick="openViewActivity"
android:icon="@drawable/text"
app:showAsAction="ifRoom"
/>


</menu>

我还添加了文件写入代码,以防出现问题。

public class AddDataActivity extends AppCompatActivity {

public static final String FileName="data.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_data);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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();
}
});
}

public void saveToFile(View view) {
// private void writeToFile(String data,Context context) {
Context context=getApplicationContext();
View userText=(EditText) findViewById(R.id.userText);
String inputString=userText.toString();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(FileName, Context.MODE_PRIVATE));
outputStreamWriter.write(inputString);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}

}

我不知道我是否需要实现我的 ArrayAdapter 版本。我实际上并不完全理解它及其参数是如何工作的。

最佳答案

改变

       String inputString=userText.toString();

       String inputString=userText.getText().toString();

关于android - ListView 不显示文本文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com