7
I would like to ask a question: I have one ListView
, in which his Adapter is clarified in the same Activity and the content of Adapter (as strings) are in another class, in another package. How do I delete a specific item from ListView
, same code, no user interaction? (for example, an application update where an item will no longer appear).
In the case I am studying, the user will click on an item from listView
, and a string inside Activity will be filled with the string in otherClass.lista
and depending on the position (position) clicked, a different text will appear.
EDITED
Follow the Listview click action:
ListView listView = (ListView) findViewById(R.id.lista);
listView.setAdapter(new listAdapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
nome.setText(otherClass.string.get(position));
}
});
Follow the Adapter I’m using:
public class listAdapter extends BaseAdapter implements ListAdapter {
public Context context;
public ArrayList<String> lug;
public Typeface fonte;
public listAdapter(Context context) {
lug = otherClass.string;
this.context = context;
}
@Override
public int getCount() {
return lug.size();
}
@Override
public Object getItem(int position) {
return lug[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView title = new TextView(context);
title.setText(lug.get(position));
title.setTextSize(18);
return title;
}
}
Now follow the Array in the class that is in another package
public class otherClass {
public static ArrayList<String> string = new ArrayList<String>();{
string.add("primeiro item");
string.add("Segundo item");
}
}
The current problem is that the Arraylist items above are not being printed... and I would like to know (when to print the items) how to delete the items.
The intention, as follows the images, would be that in the otherClass
have the items that will appear on listView
and clicking on some item will appear the respective information of the clicked item (these declared in the same class not to get confused all separate). More items will be added to each app update, so it needs to be a dynamic and organized mode. Ideas? (since Strings doesn’t seem like a good shape)
I think you want your
getItem(position)
returnlug[position]
and notposition
.– Piovezan
@Piovezan: 'position' is a global variable, being a reference for several other activities. In this case, position has the same function as lug[position], because they are the same numbers always. (in case I understood what you meant)
– Guilherme Ramos
An Adapter is a class that mediates access to
ListView
to the data source, which can be an array, a database cursor, etc. The methodgetItem(position)
you have to return an item within that data source that is in the position corresponding to the parameterposition
. Therefore, if your data source is an array or an Arraylist, which are already naturally ordered, your methodgetItem(position)
must returnmeuArray[position]
orminhaLista.get(position)
, representing items from these data sources in the position in question.– Piovezan
In addition, the position will never be the same as the item in that position, unless you have an array of integers and the value of each item matches the position in which it is in the array. So I don’t understand what you mean by position being the same as lug[position], because you’re dealing with strings and not integers. Do you mean that the items in the array are "0", "1", "2", etc.? In this case
getItem(position)
should returnString.valueOf(position)
and notposition
. But for all intents and purposes, returnlug[position]
makes more sense.– Piovezan
@Piovezan: I updated the code with more information. I used lug[position]. In my string, the items they are identified by the same position: "first item" = 0, "second item" = 1, and etc. take a look please. What I put as an update is the original mode with which I made the application, ran beautiful but the only thing is that I do not know delete the items, which maybe as they said there is as if it is declared as string[], correct?
– Guilherme Ramos
See my comment below the reply.
– Piovezan
I advise you to rephrase your question. Putting the code of each class and showing exactly where the error is, is very confusing the way it is, even following the comments.
– Lucas Santos
@Lucassantos: Reformulated, are you less confused? I tried to dry up..
– Guilherme Ramos
Enter your Activity code. You’re getting better. I’ll help you.
– Lucas Santos
@Lucassantos: I forgot to put as I declared the Adapter and the Listview.. but it is practically just that my Activity..
– Guilherme Ramos
@GH_ Ok, I’ll try to post a solution even without the Activity code. Wait I’m posting...
– Lucas Santos