Reputation: 5705
I'm creating an app to fetch names of txt files from phone.I fetched all data into an arraylist and added it to the adapter. But problem is that listview not showing items properly.At first, only the first item shows up and as I scroll up and down more items appear.Although I have added a static image in the row item and that is shown properly,even I'm able to get file name in toast message on listview for items whose name is not visible that time. I'm posting my code check it and tell what's wrong
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;
}
}
}
Upvotes: 1
Views: 1115
Reputation: 107231
The issue is, you are setting the data in your else
case. So when the if
case evaluate as true
, there won't be any data set and your list view will show an empty row. Change the getView
method like:
@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;
}
Upvotes: 1