gpt4 book ai didi

android - 从目录中读取项目到 android 中的 ListView

转载 作者:行者123 更新时间:2023-11-30 04:20:22 24 4
gpt4 key购买 nike

我正在制作一个 android 应用程序,它需要将 sd 卡上特定目录中的所有项目显示到 ListView 中。我已经浏览了几个教程,但似乎没有一个能给我任何帮助。我已经设法从我的 SD 卡和 ListView 中添加和删除内容。但我需要将目录中的项目(文件)显示到 ListView 中。我正在使用动态 ListView 。请帮助并提前致谢!这是我目前使用的代码,我需要阅读 onCreate 方法中的项目。

public class NotesActivity extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
Button AddItemToListView, AddItemToListViewButton, CancelButton, DeleteButton,CancelButton2, DeleteAllButton;
LinearLayout AddItemToListViewLinearLayout, DeleteItemFromListViewLinearLayout, DeleteAllItemsFromListViewLinearLayout;
public int DeleteIndexNumber;
public String NameOfSaveItemToSdCard = "";
public String NameOfDeleteItemFromSdCard = "";
public int DeleteIndexNumber2;
static final String[] COUNTRIES = new String[] {
"Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
DeleteIndexNumber = position;
DeleteIndexNumber2 = position;
NameOfDeleteItemFromSdCard = myList.get(position);
DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
DeleteItemFromListViewLinearLayout.setVisibility(View.VISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listviewmenubuttons, meny);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.AddItemToListView:
AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
CancelButton = (Button)findViewById(R.id.CancelButton);
DeleteButton = (Button)findViewById(R.id.DeleteButton);
CancelButton.setOnClickListener(this);
DeleteButton.setOnClickListener(this);
AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
AddItemToListViewButton.setOnClickListener(this);
AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
break;
case R.id.DeleteAllNotes:
DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
DeleteAllItemsFromListViewLinearLayout.setVisibility(View.VISIBLE);
CancelButton2 = (Button)findViewById(R.id.CancelButton2);
DeleteAllButton = (Button)findViewById(R.id.DeleteAllButton);
CancelButton2.setOnClickListener(this);
DeleteAllButton.setOnClickListener(this);
break;
}
return true;
}
public void onClick(View src) {
switch(src.getId()) {
case R.id.AddItemToListViewButton:
AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
myList.add(AddItemToListViewEditText.getText().toString());
NameOfSaveItemToSdCard = AddItemToListViewEditText.getText().toString();
((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
AddItemToListViewEditText.setText("");
AddItemToListViewEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
AddItemToListViewLinearLayout.setVisibility(View.GONE);
//Check if directory exists
checkIfDirectoryExist();
break;
case R.id.CancelButton:
DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
break;
case R.id.DeleteButton:
myList.remove(DeleteIndexNumber);
((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
File f = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes/" + NameOfDeleteItemFromSdCard);
if(f.exists()) {
boolean deleted = f.delete();
}
DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
break;
case R.id.DeleteAllButton:
myList.removeAll(myList);
((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
break;
case R.id.CancelButton2:
DeleteAllItemsFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteAllItemsFromListViewLinearLayout);
DeleteAllItemsFromListViewLinearLayout.setVisibility(View.INVISIBLE);
break;
}
}
private void checkIfDirectoryExist() {
// TODO Auto-generated method stub
File f = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes");
if(f.exists()) {
try {
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()
+ "/SchoolAppNotes/" + NameOfSaveItemToSdCard);
Toast.makeText(getApplicationContext(), "File created:-)",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "We failed to create the file",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
else {
//Create directory
File folder = new File(Environment.getExternalStorageDirectory() + "/SchoolAppNotes");
boolean success = false;
if(!folder.exists())
{
success = folder.mkdir();
}
if (!success)
{
// Do something on success
//Writing file...(It doesn't work)
}
else
{
// Do something else on failure
}
checkIfDirectoryExist();
}
}
}

最佳答案

我假设 myList 应该保存您目录中的文件名?看起来您从未实例化它。

为此,您需要获取文件名列表并将其加载到那里,然后再使用它来制作适配器。

所以在调用 .setAdapter() 之前添加类似这样的内容:

File mFile = new File(Environment.getExternalStorageDirectory() + "yourDirectory");
myList = mFile.list();

如果它正确地填充了你的数组,你就可以开始了。

附注File.list() docs

编辑:

糟糕,没注意到 myList 上的类型。改用这个

myList = Arrays.asList(mFile.list());

关于android - 从目录中读取项目到 android 中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9349832/

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