【问题标题】:display my arraylist results in custom listview在自定义列表视图中显示我的数组列表结果
【发布时间】:2011-09-14 19:35:28
【问题描述】:

我想使用自定义列表视图显示我的数组列表结果。

现在我使用 android 布局显示我的结果。

setListAdapter(new StudentListAdapter(this,android.R.layout.simple_list_item_1, results));

我有两个要显示的字段

  1. Lo​​cimage(来自 ImageUrl 的图片)
  2. Lo​​cname(并排)

文件在这里:ERROR/AndroidRuntime(335): Caused by: java.lang.NullPointerException

【问题讨论】:

  • 虽然在 google 上得到结果不会花费太多时间...

标签: android arraylist listadapter layout-inflater


【解决方案1】:

由于您使用的是自定义列表适配器并且您已经覆盖了 getView 方法,因此我认为以下行:

setListAdapter(new StudentListAdapter(this,android.R.layout.simple_list_item_1, results));

应该是:

setListAdapter(new StudentListAdapter(this, results));

【讨论】:

  • 感谢您的回答.. 得到了解决方案。还有一种方法是如何从 Url 在 ImageView 中显示图像。
  • 如果这是您问题的答案,而不是其他帖子的答案,您应该勾选我的答案。
  • 哦!好的,对不起,我刚开始在 starckoverflow 上发布问题。如何从 Url 在 ImageView 中显示图像。
  • 不同的问题,你应该发一个新的帖子。
【解决方案2】:

【讨论】:

    【解决方案3】:

    最终,您将需要覆盖您正在使用的适配器类的 getView() 方法。只是一个快速的谷歌搜索发现了很多例子。

    这里有一些非常基本的东西:http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

    还有一个更复杂的:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

    【讨论】:

      【解决方案4】:

      试试这个代码

      import java.util.ArrayList;
      import android.app.ListActivity;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteException;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ArrayAdapter;
      import android.widget.ImageView;
      import android.widget.ListView;
      import android.widget.TextView;
      
      public class FindPlaces extends Activity{
      
        private SQLiteDatabase DbLoc;
        ListView lv;
        private ArrayList<Fields> results = new ArrayList<Fields>();
        @Override
        public void onCreate(Bundle savedInstance) {
           super.onCreate(savedInstance);
          setContentView(R.layout.places);
          getallLocs();
          displayLocs();
      
      }
      private void displayLocs() {
          lv = (ListView)findViewById(R.id.listPlaces);
          lv.setAdapter(new StudentListAdapter(this,results));
      }
      class StudentListAdapter extends BaseAdapter<Fields>{
          private ArrayList<Fields> locationDetails;
          private Context mContext;
      
          public StudentListAdapter(Context context, ArrayList<Fields> results) {
              // TODO Auto-generated constructor stub
              System.out.println("Constructor StudentList Adapter...");
              this.locationDetails = results;
              this.mContext = context;
          }
      
      
          @Override
          public int getCount() {
              // TODO Auto-generated method stub
              return locationDetails.size();
          }
      
          @Override
          public Fields getItem(int position) {
              // TODO Auto-generated method stub
              return locationDetails.get(position);
          }
          @Override
          public long getItemId(int position) {
              // TODO Auto-generated method stub
              return super.getItemId(position);
          }
      
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
              View v = convertView;
              if(v == null){
                  LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  v = vl.inflate(R.layout.placeslist, null);
              }
              Fields o = locationDetails.get(position);
      
              if (o != null) {
      
                  TextView iv = (TextView)v.findViewById(R.id.toptext);
                  TextView tv_sNo = (TextView)v.findViewById(R.id.toptext1);
                  iv.setText(o.getLocationName());                            
                  tv_sNo.setText(o.getLocationImage());   
      
              }
             return v;
          }       
      }
      static class ViewHolder
      {
          TextView Locationname;
          ImageView Locationimage;
      }
      private void getallLocs() {
          // TODO Auto-generated method stub
          try {
              DatabaseHelper dbHelper = new DatabaseHelper(
                      this.getApplicationContext());
              DbLoc = dbHelper.getWritableDatabase();
              Cursor c = DbLoc.rawQuery("SELECT " + DatabaseHelper.LocationName+ " , " + DatabaseHelper.LocationImage + " FROM "
                      + DatabaseHelper.LOCATIONTABLE , null);
                              if (c != null) {
                  if (c.moveToFirst()) {
                      do {
                          String LocationName= c.getString(c.getColumnIndex("LocationName"));
                          String Mobile = c.getString(c
                                  .getColumnIndex("LocationImage"));
                          Fields p = new Fields(LocationName, Mobile);
                          results.add(p);
      
                      } while (c.moveToNext());
                  }
              }
          } catch (SQLiteException se) {
              Log.e(getClass().getSimpleName(),
              "Could not create or Open the database");
          } 
          finally { if (DbLoc != null) DbLoc.execSQL("DELETE FROM " +
                  DatabaseHelper.FRIENDTABLE); DbLoc.execSQL("DELETE FROM " +
                          DatabaseHelper.LOCATIONTABLE);  
                  DbLoc.execSQL("DROP TABLE IF EXISTS tablename"+ DatabaseHelper.FRIENDTABLE);
                  DbLoc.execSQL("DROP TABLE IF EXISTS tablename"+ DatabaseHelper.LOCATIONTABLE);
                  DbLoc.close();
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        相关资源
        最近更新 更多