2011-09-14 86 views
0

我想通過使用自定義列表視圖來顯示我的數組列表結果。在自定義列表視圖中顯示我的數組列表結果

現在我通過使用android佈局顯示我的結果。

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

我有兩個字段顯示

  1. Locimage(圖片來源的ImageUrl)
  2. LOCNAME(並排)

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

+0

它不會花費太多時間去對谷歌的結果,雖然。 .. –

回答

1

由於您使用的是自定義列表適配器,並且您已覆蓋了getView方法我認爲以下行:

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

應該是:

setListAdapter(new StudentListAdapter(this, results)); 
+0

感謝您的答案..得到了解決方案。還有一個我如何從Url在ImageView中顯示圖像。 – atluriajith

+0

如果這是你的問題的答案,而不是其他帖子,你應該打勾我的答案。 – C0deAttack

+0

哦!好吧,對不起,我剛剛開始在starckoverflow上發佈問題。我怎樣才能在Url的ImageView中顯示圖像。 – atluriajith

0

試試這個代碼

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