如何在片段中填充listview?

时间:2014-02-09 05:59:08

标签: android listview jdbc navigation-drawer

我正在开发一个Android应用程序,它将Navigation抽屉作为主菜单,然后根据我的操作选择。现在的任务之一是使用jdbc从在线mysql数据库主机填充listview。我可以在正常活动中填充listview,但相同的代码不能在片段中工作。这就是我的Fragment的样子:

public class HomeFragment extends Fragment
{
private ProgressDialog pDialog;
    ListView listView ;
Connection conn=null;
ResultSet rs=null;
PreparedStatement st=null;

List<Map<String,String>> list = new ArrayList<Map<String,String>>();



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

    try 
    {
        initList();
    } catch (SQLException e) 
    {
        e.printStackTrace();
    }

    String[] from = { "sender", "subject" ,"file_name"};
    int[] to = {R.id.sender,R.id.subject,R.id.file_name};

    SimpleAdapter adapter = new SimpleAdapter(getActivity(),list,R.layout.list_item,from,to);

    //ListView lv=(ListView) rootView.findViewById(R.id.list);
            //getListView();
    ListView lv= (ListView) getActivity().findViewById(R.id.list);
    lv.setAdapter(adapter);


    lv.setOnItemClickListener(new OnItemClickListener() {

             @Override
             public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {

                 String sender = ((TextView) view.findViewById(R.id.sender)).getText() .toString();
                 String subject = ((TextView) view.findViewById(R.id.subject)).getText() .toString();
                 String fname = ((TextView) view.findViewById(R.id.file_name)).getText() .toString();

             }
         });




    return rootView;
}   



public void initList() throws SQLException 
{

    String username="sql428447";
    String pass="************";

    String sender,subject,file_name;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");         
        conn = DriverManager.getConnection("jdbc:mysql://sql4.freesqldatabase.com:3306/sql428447",username,pass);
    }
    catch(Exception x)
    {
        x.printStackTrace();
    }

    try
    {

       String sql="select sender,subject,file_name from files";
       st=conn.prepareStatement(sql);

       rs=st.executeQuery();

       while(rs.next())
       {
           sender=rs.getString(1);
           subject=rs.getString(2);
           file_name=rs.getString(3);
           list.add(createRecord(sender, subject,file_name));
       }
   }
   catch(Exception z)
   {
       z.printStackTrace();
   }
   finally
   {
       st.close();
       rs.close();
   }


}
public HashMap<String, String> createRecord(String key, String name,String file_name) 
    {
        HashMap<String, String> record = new HashMap<String, String>();
        record.put( "sender", key);
        record.put( "subject", name);
        record.put("file_name",file_name);
        return record;
    }

}

我正在从导航抽屉选择项目中调用此片段,如下所示:

        case 1:


         newFragment = new HomeFragment();
         transaction.replace(R.id.content_frame, newFragment);
         transaction.addToBackStack(null);
         transaction.commit();

        break;

但是在运行时日志中它在initlist()和st.close()行上抛出异常,这是我无法弄清楚的。我想我的方法几乎是正确的,这在大多数教程中都有讨论,但我不理解我的代码中的错误,因为它在正常活动中工作。任何帮助,将不胜感激。谢谢 这是例外的截图 enter image description here

2 个答案:

答案 0 :(得分:2)

你可能会因使用ListFragment而受益,我在互联网上看到很多指南假设你正在使用它,所以它会变得非常混乱。

至于您当前的错误,您在st中取消引用finally而未先验证它是否为空。如果conn抛出错误,您就会发生这种情况。

    try
    {

       String sql="select sender,subject,file_name from files";
       st=conn.prepareStatement(sql);

       rs=st.executeQuery();

       while(rs.next())
       {
           sender=rs.getString(1);
           subject=rs.getString(2);
           file_name=rs.getString(3);
           list.add(createRecord(sender, subject,file_name));
       }
   }
   catch(Exception z)
   {
       z.printStackTrace();
   }
   finally
   {
       if (st != null) {
           st.close();
           if (rs != null) {
               rs.close();
           }
       }
   }

答案 1 :(得分:1)

您必须使用ListView lv= (ListView) rootView.findViewById(R.id.list);代替ListView lv= (ListView) getActivity().findViewById(R.id.list); ... rootView代替getActivity()