ONCREATE I PUT
ListView listView = (ListView) findViewById(R.id.listView3);
Below is the code i have my listview getting null pointer crashing app for some reason my listview from the code below is not finding please tell me what am doing
#Override
protected void onPostExecute(Void result) {
parsedHtmlNode.setText(htmlContentInStringFormat);
String input = amarrdatta;
String dayyz = dayys;
dayyz = dayyz.replace(" ", System.getProperty("line.separator"));
input = input.replace(" ", System.getProperty("line.separator"));
// textViewwww.setText(dayyz +" " +input);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
count++;
temp=temp+count;
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, dayyz);
temp.put(SECOND_COLUMN, input);
temp.put(THIRD_COLUMN, "22");
temp.put(FOURTH_COLUMN, "Unmarried");
list.add(temp);
ListViewAdapter adapter=new ListViewAdapter(MainActivity.this, list);
listView.setAdapter(adapter); // getting nullpointer here listview
}
}
java null pointer still
#Override
protected void onPostExecute(Void result) {
parsedHtmlNode.setText(htmlContentInStringFormat);
String input = amarrdatta;
String dayyz = dayys;
dayyz = dayyz.replace(" ", System.getProperty("line.separator"));
input = input.replace(" ", System.getProperty("line.separator"));
// textViewwww.setText(dayyz +" " +input);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
count++;
temp=temp+count;
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, dayyz);
temp.put(SECOND_COLUMN, input);
temp.put(THIRD_COLUMN, "22");
temp.put(FOURTH_COLUMN, "Unmarried");
list.add(temp);
ListViewAdapter adapter=new ListViewAdapter(MainActivity.this, list);
ListView listView = (ListView) findViewById(R.id.listView3);
listView.setAdapter(adapter);
}
You are not initialize listView variable.
You did ListView listView = (ListView) findViewById(R.id.listView3); in onCreate(). But with this code you redeclare listView variable, it hides global listView, and global listView remains null.
Remove type in onCreate():
listView = (ListView) findViewById(R.id.listView3);
or move complete line to onPostExecute().
Check your XML ListView id properly and then you need to use like below
Fragment or other layout inflate
View view = inflater.inflate(R.layout.layoutname, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView3);
Activity
ListView listView = (ListView) findViewById(R.id.listView3);
Try to move the ListView listView; outside the onCreate:
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
listView = (ListView) findViewById(R.id.listView3);
}
#Override
protected void onPostExecute(Void result) {
parsedHtmlNode.setText(htmlContentInStringFormat);
String input = amarrdatta;
String dayyz = dayys;
dayyz = dayyz.replace(" ", System.getProperty("line.separator"));
input = input.replace(" ", System.getProperty("line.separator"));
// textViewwww.setText(dayyz +" " +input);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
count++;
temp=temp+count;
HashMap<String,String> temp=new HashMap<String, String>();
temp.put(FIRST_COLUMN, dayyz);
temp.put(SECOND_COLUMN, input);
temp.put(THIRD_COLUMN, "22");
temp.put(FOURTH_COLUMN, "Unmarried");
list.add(temp);
ListViewAdapter adapter=new ListViewAdapter(MainActivity.this, list);
listView.setAdapter(adapter); // getting nullpointer here listview
}
I guess you have not placed your AsyncTask inside your Activity class.Make sure your AsyncTask is an inner Class to your Activity
Related
I have a code here that displays data in custom listview from my database
List<Map<String, String>> data1 = new ArrayList<Map<String, String>>();
while (data.moveToNext()) {
Map<String, String> datum = new HashMap<String, String>(5);
datum.put("Customer Name", data.getString(0));
datum.put("Address", data.getString(1));
datum.put("Remarks", data.getString(2));
datum.put("Date", data.getString(3));
datum.put("Code", data.getString(4));
data1.add(datum);
}
adapter = new SimpleAdapter(getActivity(), data1,
R.layout.listview_plan,
new String[]{"Customer Name", "Address", "Remarks", "Date", "Code"},
new int[]{R.id.list_name,
R.id.list_address,
R.id.list_remarks,
R.id.list_date, R.id.list_code});
listView.setAdapter(adapter);
count.setText("" + listView.getAdapter().getCount());
My question is during population I want to add some condition like hiding a textview. How can I do that?
sample
ListView.Populate{
if (data.getString(3)) == null) {
//Hide the label on that row
}
}
My target is to hide a specific textview in listview
I have a ListView with a SimpleAdapter. The Data for the ListView comes from Sqlite.I want to remove the all the listview items when query result is empty.
How to remove listview all items with SimpleAdapter ?
SimpleAdapter adapter;
ArrayList<HashMap<String, String>> urunsorgu_liste;
Veritabani db = new Veritabani(getActivity().getApplicationContext());//My database class
urunsorgu_liste =db.sorgu(sehirSp, urunSp);
if(urunsorgu_liste.size()==0)// if query result is empty
{
Toast.makeText(getActivity(), "Query is epty", Toast.LENGTH_SHORT).show();
// ??????
}
else{
List<HashMap<String, String>> sorguMaps = new ArrayList<HashMap<String, String>>();
urun_idler = new int[urunsorgu_liste.size()];
for(int i=0;i<urunsorgu_liste.size();i++){
HashMap<String, String> map = new HashMap<String, String>();
String ad=urunsorgu_liste.get(i).get("urun_adi");
String etiket=urunsorgu_liste.get(i).get("urun_etiket");
String fiyat=urunsorgu_liste.get(i).get("urun_fiyat");
String sehir=urunsorgu_liste.get(i).get("urun_sehir");
urun_idler[i] =Integer.parseInt(urunsorgu_liste.get(i).get("id"));
map.put("ADI",ad);
map.put("ETIKETI",etiket);
map.put("FIYATI",fiyat);
map.put("SEHRI", sehir);
sorguMaps.add(map);
adapter = new SimpleAdapter(getActivity(), sorguMaps, R.layout.sorgu_fragment_list_item, from, to);
listV.setAdapter(adapter);
}
Used .clear() method of ArrayList
ArrayList.clear(From Java Doc):
Removes all of the elements from this list. The list will be empty
after this call returns
In my ListView, I save my data like this:
public void listview_fuellen(){
DBHelper db = new DBHelper(this);
ListView lv = (ListView) findViewById(R.id.lvKinder);
Cursor c = db.select();
int count = c.getCount();
TextView tv = (TextView) findViewById(R.id.secondLine);
List<String> auswahl = new ArrayList<String>();
List<String> auswahl1 = new ArrayList<String>();
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
auswahl.add(c.getString(c.getColumnIndex("name")));
auswahl1.add(" " + i);
System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" + auswahl.get(i).toString());
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.firstLine, auswahl);
//ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.secondLine, auswahl1);
lv.setAdapter(adapter);
}
Now, I tried the layout for my listview from here:
http://android-developers.blogspot.co.at/2009/02/android-layout-tricks-1.html
Now, how can I set values for the second line in each ListView-Item or the icon?
EDIT:
I tried it like this now, but there are no entries in my listview then.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();;
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
//map = new HashMap<String, String>();
map.put("name",c.getString(c.getColumnIndex("name")));
map.put("datum", c.getString(c.getColumnIndex("zeit")));
i++;
}
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.list_black_text, new String[] { "datum",
"name" }, new int[] { R.id.firstLine, R.id.secondLine });
lv.setAdapter(mSchedule);
setListAdapter(mSchedule);
What have I done wrong?
for that you can make a custom adapter and then you can set values for the second line in each ListView-Item.
You can check this link
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
i hav to display the thumbnail of the image along with the title of it in a list view by parsing the JSON feed.. My code
public class Main extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
String url;
Bitmap bitmap;
ImageView img;
int im ;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = Json_mysamActivity.getJSONfromURL("http://gdata.youtube.com/feeds/mobile/videos?max-results=3&alt=json");
try {
JSONObject jb = json.getJSONObject("feed");
JSONArray jarr = jb.getJSONArray("entry");
for(int i=0;i<jarr.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jarr.getJSONObject(i);
JSONObject det = e.getJSONObject("title");
map.put("id", String.valueOf(i));
map.put("$t", "t:" + det.getString("$t"));
map.put("type", "type: " + det.getString("type"));
JSONObject det1 = e.getJSONObject("media$group");
JSONArray det12 = det1.getJSONArray("media$thumbnail");
for(int j=0;j<1;j++)
{
JSONObject det12obj = det12.getJSONObject(i);
url = det12obj.getString("url");
map.put("url", "url:"+ det12obj.getString("url"));
mylist.add(map);
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] from = new String[] { "$t", "type","url" };
int[] to = new int[] { R.id.item_title, R.id.item_subtitle, R.id.test_image };
MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.main, mylist ,
from, to);
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
// SimpleCursorAdapter requires cursor in the place of ArrayList but I need to pass //arraylist, how should I implement it
public MySimpleCursorAdapter(Context context, int layout, ArrayList<HashMap<String, String>> mylist,
String[] from, int[] to) {
super(context, layout, mylist, from, to);
}
#Override
public void setViewImage(ImageView v, String url) {
String path =url ;
Bitmap b = BitmapFactory.decodeFile(path);
v.setImageBitmap(b);
}
}
}
What you need is a SimpleAdapter. A cursor adapter is used for displaying daat in a list that comes from a database. SimpleAdapter is used for data in RAM (your list after parsing the json).
Check this first google entry, it should do.
In case you are using arraylist as datasource you should use ArrayAdapter, so Extend your adapter from ArrayAdapter. see link for Custom Array Adapter:
http://android-er.blogspot.in/2010/06/custom-arrayadapter-with-with-different.html
When you are loading image views in list, its good practice to use LazyLoading, see below link for Lazy Loading in android:
http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/
for this you have to use LayoutInflater, write your own adapter class extends ArrayAdapter,
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
adapter = new CustomAdapter(this, R.layout.list_row, R.id.title, data,
mInflater, imgid);
i m trying to get data from my database and present them to a listView.i m using two classes for my database:
1st class is the DBAdapter and the method i use to get data is:
public String[] getData()
{
// String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
String[] columns =new String[]{DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return new String[] {sa,sb,sc};
}
2.The secind class in SQLView and thats the way i m trying to create my list
public class SQLView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this, data2[1].toString(), Toast.LENGTH_SHORT).show();
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("name",data2[0].toString());
map.put("address", data2[1].toString());
map.put("address2", data2[2].toString());
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
data2, new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
What i have to do in order to work?This is my first try to use database in android!
Now i just get an empty background...
EDIT:
With my toast i m getting the last item in position 1 that i added to db
Could anybody help me with the return statement in my DBHelper please?
Below is the fixed code. Try this out.
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
Basically you have to specify the columns/key of the map that you added. So it will map key from map to the corresponding view.
For looping issue, this will do the job:
for(int i=0; i<data2.length; i+=3) {
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
The getData fix:
ArrayList<String> arrForData = new ArrayList<String>();
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
arrForData.add(c.getString(is1));
arrForData.add(c.getString(is2));
arrForData.add(c.getString(is3));
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return arrForData.toArray();