java listview change Content - java

private String[] listView2 = {"aa","bb","cc","dd","ee"};
listView1 = (ListView)findViewById(R.id.listView1);
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listView2);
listView1.setAdapter(listAdapter);
Change listView2[1] to "zzz".
Following code isn't work.
listView2[1] = "zzz";

you also need to call
listAdapter.notifyDataSetChanged();
Or use one of the listAdpater methods for changing the data:
add(T), insert(T, int), remove(T), clear()

you can update adapter like this:
adapter.insert("zz", 1);
adapter.notifyDataSetChanged();

Try this.^_^
public void newListView(){
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listView2);
listView1.setAdapter(listAdapter);
}
listView2[0] = "zz";
newListView();

Related

How to add EditText's value to the listview items?

I want to add the content from an EditText to a ListView here is my code that dosen't work:
mass = userInput.getText().toString();
//fruits is a String[]
final List<String> fruits_list = new ArrayList<String(Arrays.asList(fruits));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context.getApplicationContext(), android.R.layout.simple_list_item_1, fruits_list);
fruits_list.add(mass);
lv.setAdapter(arrayAdapter);
The code adds the item to the listview but when the process is repeated it replaces the previously added string from the EditText.
Your code appears to create a new fruits_list array every time you read the user's entry from the EditText. This will result in only the latest entry being added to your list, replacing any previously added entries.
You need to create the array once (probably as a class member) and then update it each time you read the user input, e.g. something like:
private ArrayList<String> fruits_list;
private ArrayAdapter<String> arrayAdapter;
...
public void initialise(Context context) {
fruits_list = new ArrayList<>(Arrays.asList(fruits));
arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, fruits_list);
lv.setAdapter(arrayAdapter);
}
....
public void getInput(
mass = userInput.getText().toString();
fruits_list.add(mass);
arrayAdapter.notifyDataSetChanged();
}
Note the use of arrayAdapter.notifyDataSetChanged() to signal that new data has been added to the underlying data set.
As #Aaron says, you should also look at RecyclerView as a possible replacement, although that's a little bit more complicated to set up.

Java.Lang.NullPointerException while using listview simple adapter

I have a listview that shows two sqlite tables' values. First I tried to use ArrayAdapter to view them and all values were shown. But they looked messy, so I tried using a simple adapter. hHre is my code.
for(int i=0; i<= promo.size();i++){
ProductModulePromo b =dataSource.getproductmodulepromo(moduleid);
productmoduledesc = b.getProductmoduledesc().toString();
producttype = b.getProducttype().toString();
productpromo = b.getproductpromotion().toString();
map1.put("three",productpromo);
map1.put("two",producttype);
map1.put("one",productmoduledesc);
Globals.promo.add(map1);}
promo is my arraylist<productmodulepromo>(). when i try to compile those code. all values that shown are same. so i try to change with this code on datasource.java and use those arraylist on formpromo.java. here is my DBDatasource.java's code:
public ArrayList<HashMap<String, String>> getproduct1() {
ArrayList<HashMap<String, String>> promo = new ArrayList<HashMap<String, String>>();
database = dbHelper.getReadableDatabase();
Cursor cursor = database.rawQuery("select productmodule.productmoduledescription, productdetail.producttype, productdetail.productpromotion from productmodule, productdetail where productmodule.productmoduleid=productdetail.productmoduleid", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
productmoduledesc=cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
producttype = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_TYPE));
productpromo = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_PROMOTION));
map1.put("one",productmoduledesc);
map1.put("two",producttype);
map1.put("three",productpromo);
promo.add(map1);
} while (cursor.moveToNext());
}
cursor.close();
return promo;
}
here is my Formpromo.java :
dataSource = new DBDataSource(this);
dataSource.open();
promo = dataSource.getproduct1();
adapter = new SimpleAdapter(FormPromo.this, promo, R.layout.item_to,
new String[] {"one","two","three"},
new int[] {R.id.edtbrandto,R.id.edtqtyto,R.id.edtharga});
setListAdapter(adapter);
final ListView listpromo = (ListView) findViewById(android.R.id.list);
setListAdapter(adapter);
and I am getting an error as Java.Lang.NullpointerException on map1.put("one",productmoduledesc);. I have no idea how to fix them all. Thanks for help. any help will be appreciated.
Seems like you never set map1, add it to the beginning of the loop, because else you'd overwrite the old values
like this
...
do {
map1 = newHashMap<String, String>(); // add
productmoduledesc=cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
producttype = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_TYPE));
productpromo = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_PROMOTION));
map1.put("one",productmoduledesc);
map1.put("two",producttype);
map1.put("three",productpromo);
promo.add(map1);
} while (cursor.moveToNext());
...

How to search within ListView with custom adapter

I looked at the following site: ListView Example
Which describes how to implement a search function in a listview which uses the default adapter and it works fine.
How can I modify it so I can use the same for a Custom Adapter for my listview?
Partial code is:
dataList = (ListView) findViewById(R.id.lvFiles);
tvQuote = (TextView) findViewById(R.id.tvDisplay);
tvQuote.setTypeface(Typeface.createFromAsset(MainActivity.this.getAssets(), "fonts/roboto.ttf"));
for (int y=0; y<strNamesOfAllah.length;y++) {
name = strNamesOfAllah[y];
meaning = strMeaning[y];
rowsArray.add(new SetRows(R.drawable.icon, name, meaning));
}
adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.customlist, rowsArray);
dataList.setAdapter(adapter);
dataList.setClickable(true);
You need to override getFilter inside of your adapter and return a new customFilter object that you create. See this answer: No results with custom ArrayAdapter Filter
Edit:
#Override
public Filter getFilter() {
if(customFilter == null){
customFilter = new CustomFilter();
}
return customFilter;
}

Remove or Clear content in SimpleAdapter in Android?

Here is the code:
ibtSearchStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
try{
searchQuery = etSearchThis.getText().toString();
searchQuery = searchQuery.toUpperCase();
cursor = searchActivity.getData(product, "product", tableColumns);
//Clean ArrayList
resultRow.clear();
resultTable.clear();
//Get Search Result
resultTable = searchActivity.searchByProductName(cursor, searchQuery);
//Display Search Result
for(int ctr = 0; ctr < resultTable.size(); ctr++){
HashMap<String, String> map = new HashMap<String, String>();
resultRow = resultTable.get(ctr);
String result = resultRow.get(2);
map.put("ProductName",result);
list.add(map);
}
Log.e("resultProduct", "" + list);
adapter = new SimpleAdapter(
SearchMain.this,
list,
R.layout.search_result,
new String[]{"ProductName"},
new int[]{R.id.tvProductName}
);
lvSearchResult.setAdapter(adapter);
}
finally{
product.close();
}
}
});
The function of this is that it will search for a match in the database then it will insert the result of the search in a HashMap then on a adapter.
But every click i am not able to remove the previous result.
What is the proper implementation to this?
When you are working with adapters and the information changes, be sure that you are calling adapter.notifyDataSetChanged();
You set a new adapter each time the OnClick event is triggered. As Jay Snayder wrote you should use adapter.notifyDataSetChanged(); instead. But be sure to set the adapter only once and move the following part of your code (e.g. to your onCreate() method of the activity):
adapter = new SimpleAdapter(
SearchMain.this,
list,
R.layout.search_result,
new String[]{"ProductName"},
new int[]{R.id.tvProductName}
);
lvSearchResult.setAdapter(adapter);
Just update the DataSet of your adapter (here: 'list').

Constructor is undefined in ArrayAdapter

I am trying to populate a ListView control in an Android app. I have looked at many code samples and all seem to suggest doing it this way. However, when I try to implement this I get the following error:
The constructor ArrayAdapter(MainActivity.ReadJsonTask, int, ArrayList) is undefined
protected void onPostExecute(String result){
try{
ListView lv = (ListView)findViewById(R.id.lstItems);
JSONObject jsonObj = new JSONObject(result);
ArrayList<String> items = new ArrayList<String>();
Iterator<String> looper = jsonObj.keys();
while(looper.hasNext()){
String key = looper.next();
items.add(jsonObj.get(key).toString());
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
}catch(Exception ex){
Log.d("ReadAdscendJsonTask", ex.getLocalizedMessage());
}
}
What is wrong here? Thanks!
It seems so that this piece of code is from inner class. And in such case this refers to this inner class instead of MainActivity. Changing Context parameter to MainActivity.this for ArrayAdapter should fix the error.

Categories