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
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
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
if supplier id is !=0 then
if(user.getString("iSupplierID")!="0")
{
String SUPPLIER_NAME=user.getString(TAG_NAME);
String PRIMARY_SERVICE_CATEGORY = user.getString(TAG_PRIMARY);
String SECONDARY_SERVICE_CATEGORY = user.getString(TAG_SECONDARY);
String SOURCE_DIRECTORY_TYPE = user.getString(TAG_SOURCE);
Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, SUPPLIER_NAME);
map.put(TAG_PRIMARY, PRIMARY_SERVICE_CATEGORY);
map.put(TAG_SECONDARY, SECONDARY_SERVICE_CATEGORY);
map.put(TAG_SOURCE, SOURCE_DIRECTORY_TYPE);
supplierlist.add(map);
creating an id for list view
lv=(ListView)findViewById(R.id.listView1);
listview adapter
ListAdapter adapter = new SimpleAdapter(SupplierDetails.this, supplierlist,
R.layout.list_view,
new String[] { TAG_NAME,TAG_PRIMARY,TAG_SECONDARY, TAG_SOURCE }, new int[] {
R.id.primary,R.id.secondary, R.id.source});
lv.setAdapter(adapter);
I am using SQlite Database.I create 2 tables and populate them(working fine) and use there data to show in list view. when first time list adapter is populating list view show newDATA varibale as zero. some data. but when i go back and furter start that activity newDATA has correct values. . I use below code
public ArrayList<HashMap<String, Object>> GetDataFirst(String id) {
ArrayList<HashMap<String, Object>> firstvariable = new ArrayList<HashMap<String, Object>>();
Cursor c =sdb.rawQuery("Select * from table_name ",new String[]{id} );
String[] colums = c.getColumnNames();
while (c.moveToNext()) {
HashMap<String, Object> record = new HashMap<String, Object>();
for (int i = 0; i < colums.length; i++) {
record.put(colums[i], c.getString(c.getColumnIndex(colums[i])));
}
{
ArrayList<HashMap<String, String>> now_add = GetmainData(id, record.get("vget").toString());
record.put("NewDATA", now_add);
hint.add(record);
}
}
}
c.close();
return firstvariable ;
}
public ArrayList<HashMap<String, String>> GetCluesDataVerical(String id,String vhid) {
ArrayList<HashMap<String, String>> hint = new ArrayList<HashMap<String, String>>();
Cursor c = sdb.rawQuery("SELECT * FROM table_data", new String[] {id,vhid});
String[] colums = c.getColumnNames();
while (c.moveToNext()) {
HashMap<String, String> record_add = new HashMap<String, String>();
for (int i = 0; i < colums.length; i++) {
record_add.put(colums[i], c.getString(c.getColumnIndex(colums[i])));
}
hint.add(record_add);
}
c.close();
return hint;
}
and in list adapter i call these as below:
recordData=context.crossDatabaseAdapter.GetDataFirst(value);
wArray = (ArrayList<HashMap<String, String>>) recordData.get(position).get("NewDATA");
I tried a lot But still not come with any solution.
You must use an instance state to save the content of the list adapter when you stop the application and when application is starting you must check if you can retrieve data thanks to instance state, else load data from database, this might solve your problem
I'm trying to figure out if it is possible to launch an intent within an ExpandableListView. Basically one of the "Groups" is Phone Number and its child is the number. I want the user to be able to click it and have it automatically call that number. Is this possible? How?
Here is my code to populate the ExpandableListView using a Map called "data".
ExpandableListView myList = (ExpandableListView) findViewById(R.id.myList);
//ExpandableListAdapter adapter = new MyExpandableListAdapter(data);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
Iterator it = data.entrySet().iterator();
while (it.hasNext())
{
//Get the key name and value for it
Map.Entry pair = (Map.Entry)it.next();
String keyName = (String) pair.getKey();
String value = pair.getValue().toString();
//Add the parents -- aka main categories
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put("NAME", keyName);
//Add the child data
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put("NAME", value);
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
mContext,
groupData,
R.layout.exp_list_parent,
new String[] { "NAME", "IS_EVEN" },
new int[] { R.id.rowText1, R.id.rowText2 },
childData,
R.layout.exp_list_child,
new String[] { "NAME", "IS_EVEN" },
new int[] { R.id.rowText3, R.id.rowText4}
);
myList.setAdapter(mAdapter);
You can add an OnChildClickListener to your expandable list view. When OnChildClick is called, you have the group and child position, so you should be able to figure out which phone number was clicked.
Then you just need to fire off an intent to make the phone call, i.e.:
Intent intent = new Intent(Intent.CALL_ACTION);
intent.setData(Uri.parse("tel:+436641234567"));
startActivity(intent);