ListView - 显示点击,烤面包未显示的详细信息

时间:2014-06-02 21:07:05

标签: android listview toast baseadapter onitemclicklistener

解决

我无法弄清楚我做错了什么。 ListView中所有添加的位置都显示,但是当我点击特定位置时,详细信息未显示,当我点击任何位置时,甚至不会显示简单的tost消息。

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

SearchResult所

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>

3 个答案:

答案 0 :(得分:1)

尝试使用getApplicationContext()...

答案 1 :(得分:1)

你应该试试这个:

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

此外,为了创建适配器,您可以使用

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

答案 2 :(得分:1)

更改

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

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