Question

Solved

I can't figure out what I'm doing wrong. All the added positions in ListView are showing but when I click on a specific position the tost with details isn't showing, not even simple tost message will show when I click on any position.

public class MyCustomBaseAdapter extends BaseAdapter {
     private static ArrayList<SearchResults> searchArrayList;

     private LayoutInflater mInflater;

     public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
         searchArrayList = results;
         mInflater = LayoutInflater.from(context);
     }

     public int getCount() {
         return searchArrayList.size();
     }

     public Object getItem(int position) {
         return searchArrayList.get(position);
     }

     public long getItemId(int position) {
         return position;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.custom_row_view, null);
             holder = new ViewHolder();
             holder.name = (TextView) convertView.findViewById(R.id.name);

             convertView.setTag(holder);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }

        holder.name.setText(searchArrayList.get(position).getFirstName() + " " + searchArrayList.get(position).getLastName());

        return convertView;
     }

     static class ViewHolder {
         TextView name;
     }
}

SearchResults

public class SearchResults {
     private String firstName="";
     private String lastName="";
     private String city="";

     public void setFirstName(String firstName) {
         this.firstName = firstName;
     }

     public String getFirstName() {
        return firstName;
     }

     public void setLastName(String lastName) {
        this.lastName = lastName;
     }

    public String getLastName() {
        return lastName;
    }

     public void setCity(String city) {
        this.city = city;
     }

     public String getCity() {
         return city;
     }
    }

DataList

public class DataList extends ListFragment {

    String fileName="personalData.txt";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.tab3, container, false);

        ArrayList<SearchResults> list = new ArrayList<SearchResults>();

            try {
                FileInputStream fis = getActivity().openFileInput(fileName);
                InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(isr);

                String sLine = null;

                while ((sLine=br.readLine())!=null) {
                  String[] el=sLine.split(";");
                  String firstName=el[0];
                  String lastName=el[1];
                  String city=el[2];
                  SearchResults sr = new SearchResults();
                  sr.setFirstName(firstName);
                  sr.setLastName(lastName);
                  sr.setCity(city);
                  list.add(sr);
                }
                br.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            final ListView lv1 = (ListView)view.findViewById(android.R.id.list);
            lv1.setAdapter(new MyCustomBaseAdapter(getActivity(), list));

            lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = lv1.getItemAtPosition(position);
                SearchResults fullObject = (SearchResults) o;
                Toast.makeText(getActivity().getApplicationContext(), fullObject.getFirstName() + " "
                  + fullObject.getLastName() + "\n"
                  + fullObject.getCity(), Toast.LENGTH_LONG).show();
                } 
            }); 

        return view;
    }
}

tab3.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true">
</ListView>
</FrameLayout>
Was it helpful?

Solution

You should try this:

public class DataList extends ListFragment
{
    ....
      @Override
      public void onListItemClick(ListView l, View v, int position, long id)
      {
          super.onListItemClick(l, v, position, id);
          SearchResults sr = (SearchResults) this.getListAdapter().getItem(position);
          Toast.makeText(getActivity().getApplicationContext(), sr.getFirstName() + " "
              + sr.getLastName() + "\n"
              + sr.getCity(), Toast.LENGTH_LONG).show();
      } 
 }

Also, for the creation of the adapter, you could use

lv1.setAdapter(new MyCustomBaseAdapter(inflater.getContext(), list));

OTHER TIPS

Try using just getApplicationContext()...

Change

Toast.makeText(getActivity().getApplicationContext(), fullObject.getFirstName() + " "
              + fullObject.getLastName() + "\n"
              + fullObject.getCity(), Toast.LENGTH_LONG).show();
            } 

to

Toast.makeText(getActivity(), fullObject.getFirstName() + " "
              + fullObject.getLastName() + "\n"
              + fullObject.getCity(), Toast.LENGTH_LONG).show();
            } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top