gpt4 book ai didi

android - ListView 没有正确显示项目

转载 作者:行者123 更新时间:2023-11-30 01:41:58 25 4
gpt4 key购买 nike

我正在创建一个应用程序来从手机中获取 txt 文件的名称。我将所有数据提取到数组列表中并将其添加到适配器中。但问题是 ListView 没有正确显示项目。起初,只有第一个项目显示出来,当我上下滚动时,更多的项目出现了。虽然我在行项目中添加了一个静态图像并且显示正确,但即使我'对于当时名称不可见的项目,我能够在 ListView 上的 toast 消息中获取文件名。我正在发布我的代码,检查它并指出问题所在

public class MainActivity extends AppCompatActivity {
private File sdcardObj=new File(Environment.getExternalStorageDirectory().getAbsolutePath());
private ArrayList<String> filelist=new ArrayList<String>();
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.listfiles);
LongOperation longOperation=new LongOperation();
longOperation.execute();

}
private class LongOperation extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
listFiles(sdcardObj,filelist);
return "Executed";
}

@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
MyListAdapater myListAdapater=new MyListAdapater(MainActivity.this,filelist);
list.setAdapter(myListAdapater);


}

@Override
protected void onPreExecute() {
progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please Wait.Loading files list");
progressDialog.setCancelable(false);

progressDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {}
}
private void listFiles(File sdcard,ArrayList<String> filelist) {
if(sdcard.isDirectory()){
File[] files = sdcard.listFiles();

try {
for (File f : files){
if(!f.isDirectory()) {
if(f.getName().endsWith(".txt")|| f.getName().endsWith(".docx")||f.getName().endsWith(".rtf")) {
// Log.d(" FILES",f.getName());
this.filelist.add(f.getAbsolutePath());

}
}
else {
this.listFiles(f,this.filelist);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
static class ViewHolder{
public TextView cardId;

}

class MyListAdapater extends BaseAdapter {
ArrayList<String>files=new ArrayList<>();
LayoutInflater inflater ; Context context;
public MyListAdapater(Context context,ArrayList<String> cardListForDisplay) {
super();

this.files = cardListForDisplay;
this.context=context;

}



@Override
public int getCount() {
// TODO Auto-generated method stub
return files.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}



@Override
public View getView(int arg0, View view, ViewGroup parent) {
View rowView = view;
ViewHolder holder;
if(rowView==null){
holder=new ViewHolder();
inflater = ((MainActivity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder.cardId=(TextView)rowView.findViewById(R.id.tv_name);
rowView.setTag(holder);
// /storage/emulated/0/Android/data/com.gameloft.android.ANMP.GloftFWHM/files/d_o_w_n_l_o_a_d_e_d.txt
}
else{
holder=(ViewHolder) rowView.getTag();
String name=files.get(arg0).substring(files.get(arg0).lastIndexOf("/")+1,files.get(arg0).length());
holder.cardId.setText(name);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,files.get(position), Toast.LENGTH_LONG).show();
}
});
}
return rowView;
}
}
}

最佳答案

问题是,您要在 else 案例中设置数据。因此,当 if 案例评估为 true 时,将不会有任何数据集,您的 ListView 将显示一个空行。更改 getView 方法,例如:

@Override
public View getView(int arg0, View view, ViewGroup parent)
{
View rowView = view;
ViewHolder holder;
if(rowView==null)
{
holder = new ViewHolder();
inflater = ((MainActivity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder.cardId=(TextView)rowView.findViewById(R.id.tv_name);
rowView.setTag(holder);
}
holder = (ViewHolder) rowView.getTag();
String name = files.get(arg0).substring(files.get(arg0).lastIndexOf("/")+1,files.get(arg0).length());
holder.cardId.setText(name);
return rowView;
}

关于android - ListView 没有正确显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34372698/

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