In my code the assigned value comes through the server when assigned value is 1.
The checkbox value in listview should be checked while unchecked ...
and after load of listview if I try to check on already checked or change that state it shows an alert box
When I check rest of checkbox while scrolling its automatically unchecked..
boolean checked[];
ArrayList<HashMap<String, Object>> Arraymanagemember = new ArrayList<HashMap<String, Object>>();
ListView managelist = (ListView) findViewById(R.id.managememberlist);
final CheckBox cbox = (CheckBox) managelist.findViewById(R.id.usercheck);
for (int i = 0; i < array.length(); i++) {
HashMap<String, Object> membermap = new HashMap<String, Object>();
memberinfo[i] = array.getJSONObject(i).getString("username").toString();
assingedinf[i] = array.getJSONObject(i).getString("assigned").toString();
if (assingedinf[i].equals("1")) {
membermap.put("assign", true);
} else {
membermap.put("assign", isTaskRoot());
}
membermap.put("member", memberinfo[i]);
Arraymanagemember.add(i, membermap);
}
SimpleAdapter memberadp = new SimpleAdapter(
getApplicationContext(),
Arraymanagemember,
R.layout.managememberrow,
new String[]{"member", "assign"}, new int[]{R.id.manageuserinfo, R.id.usercheck}
);
managelist.setAdapter(memberadp);
managelist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(
AdapterView<?> parent,
View arg1, int postion,
long arg3) {
if (assingedinf[postion].equals("1")) {
Toast.makeText(getApplicationContext(), "not run", Toast.LENGTH_SHORT).show();
}
}
}
});
Related
I know there are several questions around this, but do not quite get how to solve it.
The problem is that I am showing some values from local SQlite database, the different options are shown ok and I can select them and the value displayed is ok. the problem is that when I try to save it, the getSelectedItem, gets the first item on the list. Any help or suggestions on how to solve it would be great.
Product product = new Product();
productsList = product.getProducts();
Spinner spinnerProduct = findViewById(R.id.spinnerProduct);
String[] arrayProduct = new String[productsList.size()];
for(int i = 0; i < productsList.size(); i++) {
arrayProduct[i] = productsList.get(i).nameProduct;
}
ArrayAdapter<String> adapterProduct = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrayProduct);
adapterProduct.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProduct.setAdapter(adapterProduct);
spinnerProduct.setOnItemSelectedListener(onItemSelectedListener1);
String productSelected=spinnerProduct.getSelectedItem().toString();
AdapterView.OnItemSelectedListener onItemSelectedListener1 =
new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Product product = new Product();
productsList = product.getProducts();
int[] arrayProduct = new int[productsList.size()];
for(int i = 0; i < productsList.size(); i++) {
arrayProduct[i] = productsList.get(i).stockCurrent;
}
String productStock = String.valueOf(arrayProduct[position]);
product_amount_available.setText(productStock);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};
A spinner uses Event listening so you cannot just do below in a linear fashion:
spinnerProduct.setOnItemSelectedListener(onItemSelectedListener1);
String productSelected=spinnerProduct.getSelectedItem().toString();
Basically what your code is doing is setting the listener, and immediately after, it's getting some arbitrary/default value from your spinnerProduct object. But you haven't even entered any input to the spinner yet. You must process all the UI and business logic in the event listener's onItemSelected() method only.
You need to implement OnItemSelectedListener and override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
More info can be found here https://developer.android.com/guide/topics/ui/controls/spinner#SelectListener
Thanks for the help. I solved it. I made the global variables.
int idProduct,idStorage;
String productSelected,storageSelected;
first fill the spinners
Product product = new Product();
productsList = product.getProducts();
Storage storage = new Storage();
storageList = storage.getStorage();
Spinner spinnerProduct = findViewById(R.id.spinnerProduct);
spinnerProduct.setOnItemSelectedListener(this);
Spinner spinnerStorage = findViewById(R.id.spinnerStorage);
spinnerStorage.setOnItemSelectedListener(this);
String[] arrayProduct = new String[productsList.size()];
for(int i = 0; i < productsList.size(); i++) {
arrayProduct[i] = productsList.get(i).nameProduct;
}
ArrayAdapter<String> adapterProduct = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrayProduct);
adapterProduct.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProduct.setAdapter(adapterProduct);
spinnerProduct.setOnItemSelectedListener(this);
String[] arrayStorage = new String[storageList.size()];
for(int i = 0; i < storageList.size(); i++) {
arrayStorage[i] = storageList.get(i).nameStorage;
}
ArrayAdapter<String> adapterStorage = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arrayStorage);
adapterStorage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStorage.setAdapter(adapterStorage);
spinnerProduct.setOnItemSelectedListener(this);
then, as suggested implemented the onclicklisteners
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
if(adapterView.getId() == R.id.spinnerProduct)
{
idProduct=(int) adapterView.getSelectedItemId();
productSelected=adapterView.getSelectedItem().toString();
Product product = new Product();
productsList = product.getProducts();
int[] arrayProduct = new int[productsList.size()];
for(int i = 0; i < productsList.size(); i++) {
arrayProduct[i] = productsList.get(i).stockCurrent;
}
String productStock = String.valueOf(arrayProduct[pos]);
product_amount_available.setText(productStock);
}
else if(adapterView.getId() == R.id.spinnerStorage)
{
storageSelected=adapterView.getSelectedItem().toString();
idStorage=(int) adapterView.getSelectedItemId();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
and at last I passed the values into the method triggered by a button
btnSaveTransferProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveTransferProduct(idProduct,productSelected, idStorage, storageSelected);
}
}
});
I'm trying to get into a listview items have different action when pressed, this action depends on a variable "state", but the problem is that all the items are in the same action, here the code:
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// Storing JSON item in a Variable
String codigo = c.getString(TAG_CODIGO);
String asignatura = c.getString(TAG_NOMBRE);
int estado = c.getInt("estado");
final int maxhoras = c.getInt("maxhoras");
final String idprogramacion = c.getString("programacionid");
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CODIGO, codigo);
map.put(TAG_NOMBRE, asignatura);
jsonlist.add(map);
list=(ListView)findViewById(R.id.lvclases);
ListAdapter adapter = new SimpleAdapter(Bienvenida.this, jsonlist,
R.layout.listview,
new String[] { TAG_CODIGO,TAG_NOMBRE, }, new int[] {
R.id.codigo, R.id.nombre,
});
list.setAdapter(adapter);
if(estado == 1) {
Log.e("estado", ""+estado);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
/*Toast toast1 = Toast.makeText(getApplicationContext(), "Correcto: el usuario existe", Toast.LENGTH_SHORT);
toast1.show();*/
//Toast.makeText(Bienvenida.this, "You Clicked at " + jsonlist.get(+position).get("asignatura"), Toast.LENGTH_SHORT).show();
}
});
}else{
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Bienvenida.this, "la clase aún no ha comenzado " + jsonlist.get(+position).get("asignatura"), Toast.LENGTH_SHORT).show();
}
});
}
}
I am working on android studio, I appreciate the help
Acually, this is not an answer to your question. But after watching your code it seems that you are not implementing ListView in a correct way.
A simple way to work with ListView goes through following abstract steps:
Make your Collection Ready -> Prepare the Adapter from the collection -> Set this adapter to ListView
In your case, assuming you are calling web services for getting data and your web services is responding with JSON array.
Now, make a POJO(plain old Java object) class like:
public class MyListItem{
private String codigo;
private String asignatura;
private int estado;
private int maxhoras;
private String idprogramacion;
public MyListItem(String codigo, String asignatura, int estado, int maxhoras, String idprogramacion){
this.codigo = codigo;
this.asignatura = asignatura;
this.estado = estado;
this.maxhoras = maxhoras;
this.idprogramacion = idprogramacion;
}
// you can write getter setter methods for this
public int get_estado(){
return estado;
}
public String get_idprogramacion(){
return idprogramacion;
}
public int get_maxhoras(){
return maxhoras;
}
public String get_asignatura(){
return asignatura;
}
}
Now, prepare list like:
ArrayList<MyListItem> mArrayList = new ArrayList<>();
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
mArrayList.Add(
new MyListItem(
c.getString(TAG_CODIGO),
c.getString(TAG_NOMBRE),
c.getInt("estado"),
c.getInt("maxhoras"),
c.getString("programacionid")
)
);
}
Now, you are ready with list, just pass it to your custom adapter. If you are not aware of preparing Custom Adapter, check this link.
Once you are ready with making custom adapter, pass the list to this adapter like:
MyCustomAdapter adapter = new MyCustomAdapter(context, mArrayList);
Note: Above line strictly depends constructor of your custom adapter, say, MyCustomAdapter
After that, you are ready with your adapter, set it to your ListView, like:
list.setAdapter(adapter);
Now, here you set the OnItemClickListener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyListItem item = (MyListItem)parent.getItem(position);
if(item.get_estado() == 1)
{
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", item.get_idprogramacion());
i.putExtra("maxhoras", item.get_maxhoras());
startActivity(i);
}
else{
Toast.makeText(Bienvenida.this, "la clase aún no ha comenzado " + item.get_asignatura(), Toast.LENGTH_SHORT).show();
}
}
});
Hope you get what you are trying to achieve..
This should help you out.
Instead of having two different onClickListeners(), try using only one and move your if-else block inside of the onItemClick() method
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(estado == 1) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
} else {
// Do something else
}
}
});
Problem is estado variable, it's global varible, so When you use listview.setOnItemClickListener(...) that mean all the item in listview will be the same action with your above code.
Solution is you should create your own custom adapter and then implement OnItemClickListener in your adapter, base on different states to set different actions for item . Also you can setOnClickListener in getView() in Adapter and base on state also.
Please refer as below :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int estado = -1;
// you should do something like this
/* ********** */
YourItem item = ((YourItemsData)data).get(position);
estado = item.getState();
/* ********** */
if(estado == 1) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
} else {
// Do something else
}
}
});
So I'm trying to make a basic foreground for another app I'm making.
I'm just trying to get a listView to populate on screen within my fragment. (To create swipe-able tab views)
I keep getting a nullPointerException on this line here:
List.setAdapter(Adapter);
Here's my entire class:
public class List_View extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listviewtest, container, false);
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
List<HashMap<String, String>> Data = new ArrayList<HashMap<String, String>>();
{
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put("Item", "Item 1");
Data.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put("Item", "Item 2");
Data.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put("Item", "Item 3");
Data.add(temp3);
}
SimpleAdapter Adapter = new SimpleAdapter(this.getActivity(),
Data, R.layout.custom_row_view, new String[] {
"Item" }, new int[] { R.id.text1});
List.setAdapter(Adapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 1:
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
getActivity());
dlgAlert.setMessage("Set Message Text");
dlgAlert.setTitle("Set Message Title");
dlgAlert.setNeutralButton("View info",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
break;
}
}
});
return rootView;
}
}
So far as I can tell this shouldn't be returning null..
I'm thinking your inflate operation is returning null. Check this line
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
You should be inflating your ListView from your parent view of your fragment, not from the parent activity.
Should be something like this instead
ListView List = (ListView) rootView.findViewById(R.id.listView1); and make sure that
I'm writing a news-app and I'm having some trouble with displaying a custom list. All I want is that list items have 2 TextViews in them:
News-Title and
News-Description
These are contained in 2 static arrays: homeScreen.title[] and homeScreen.descriptionLong[].
Lower you have my code for the HashMap and the Adapter:
final static ArrayList> data = new ArrayList>();
static{
HashMap<String, String> row = new HashMap<String, String>();
for (int i = 0; i<HomeScreen.arrayLength; i++){
row.put("Title", HomeScreen.title[i]);
row.put("Description", HomeScreen.descriptionLong[i]);
data.add(row);
}
}
SimpleAdapter adapter = new SimpleAdapter(this,
data,
R.layout.mainmenu,
new String[] {"Title", "Description"},
new int[] { R.id.textView1, R.id.textView2});
setListAdapter(adapter);
}
public void onItemClick(SimpleAdapter arg0, View arg1, int position,
long id) {
selectedNews = position;
Toast.makeText(getApplicationContext(), "This is: " + selectedNews, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainMenu.this, ReadNews.class);
startActivity(intent);
}
The problem I'm having is that it only displays my 20'th (last) news information and also the default OnItemClick isn't working anymore. I'd appreciate your help...
You should put the row instantiation inside the for loop:
for (int i = 0; i<HomeScreen.arrayLength; i++){
HashMap<String, String> row = new HashMap<String, String>();
row.put("Title", HomeScreen.title[i]);
row.put("Description", HomeScreen.descriptionLong[i]);
data.add(row);
}
Update:
Your OnItemClickListener registration should look something like this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// your code here
}
});
Hi I have a question. Wondering how do I get certain values from the OnItemClick method. I can get position but seen as I have more then 1 textview in this position is it possible to get the first textview?
Heres my code
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0; i<poi.length; i++){
map = new HashMap<String, String>();
map.put("first", poi[i][0]);
map.put("second",poi[i][1]);
mylist.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"first", "second"}, new int[] {R.id.FIRST, R.id.SECOND});
list.setAdapter(mSchedule);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.i("owen", Integer.toString(position));
//my code to get "first" string on click
}
Thanks
If you know the resource identifier for the desired textview, you can take the view passed into the onitemClick() method and find that child:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
TextView myText = (TextView) v.findViewById(R.id.my_textview);
Log.i("owen", myText.getText().toString());
}
The position relates to the row position in the AdapterView list.