I have a ListView with a TextView and a Checkbox on every row. I'm trying to create a sequence where the user has to click on the Checkbox of the first row to enable the next row and remove a grayed out effect. Clicking on the Checkbox in this row will do the same to the next row and so on.
Is this possible? and how?
Create a custom Object to keep the state of your row. For example:
class Row {
private TextView text;
private CheckBox box;
private boolean grayedOut;
//implementation...
}
Inside your Activity you make an ArrayList<Row>() and give it as source to the Adapter of your ListView.
onCreate(..){
mArrayList = new ArrayList<Row>();
// fill arraylist
Adapter mAdapter = new CustomAdapter(... , ... , mArrayList);
ListView lv = findView..
lv.setAdapter(mAdapter);
}
The Adapter for your ListView is custom. You can follow the example found here. In your case some adjustments will be as followed:
public class CustomAdapter extends ArrayAdapter<Row>{
public CustomAdapter(...) {
// do stuff
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ....
if (item at position has gray state){
set layout to gray
} else {
set layout to active
}
// ....
return row;
}
Now we have a Listview, a CustomAdapter and for each item in the list we have an Row which provides a certain state. Default all the states will be set to gray-state except the first item. Using the onItemClickedListener, we can unlock other items..
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, Long id){
// do stuff...
mArrayList.get(position+1).setGrayedOut(false);
}
}
Good luck!
Related
I have one problem.
I want to get the position of Button in List View.
In below image when I clicked in Edit or Delete button i just want its position in List View.
How it possible?
This is my Office Management Screen. All the data getting in ListView run-time from my web-service.
I get the data in List View by using this code-
ListAdapter adapter = new SimpleAdapter(Office_Mgmt.this, officeList, R.layout.office_mgmt_list_model, new String[] { "office_name" }, new int[] { R.id.label});
setListAdapter(adapter);
You can create your custom adapter, passing the context of your app as argument to set the click:
listView.setAdapter(new PesquisaAdapter(this, anunciantescidades, this);
Then, in the constructor of your adapter you will have a OnClickListener to recieve the argument this you passed:
public PesquisaAdapter(Context context, List<Anunciante> anunciantes, OnClickListener onClick1)
In the getView method of the adapter, you can set onClickListener of the button:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_layout, null);
Button btn = (Button) v.findViewById(R.id.yourbutton);
btn.setOnClickListener(onClick1);
}
In your java, you can implements onClickListener, and then do what you want with your button:
#Override
public void onClick(View v) {
if(yourbutton.getId() == v.getId()){
final int position = listView.getPositionForView((LinearLayout)v.getParent());
}
}
So, you will have the position in the list that you clicked, and you can manage the click of the edit and delete separately.
Hope it helps!
implement onItemClick(AdapterView<?> parent, View view, int position, long id) for the listview.
Position and view can be identified from the handler. You can set additional info using setTag while creating the view inside the Adapter's getView method and retrieve in the handler and then verify in the handler if required.
How do i access the controls of an list view on item Click listener in a list view? As i have set the customize layout for a single list view item.
Attaching an item click listener
In the actvity where your listview is created try something similar to these
private void setupListView() {
//find by view id
customListView = ((ListView) findViewById(R.id.lv_listview);
//set custom adapter
customListView.setAdapter(
new CustomListAdapter(this, R.id.lv_listview, variableList));
//attach listener
customListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* whatever happens when you click the list item goes here */
}
});
}
I have a list view that is built using a simpleAdapter, each row is defined by a Layout containing a TableRow and a few TextView.
What should I do to know on which TextView (in the layout) was clicked on?
I am able to get the listview row (position) if I use a OnItemClickListener on the Listview, however it dosen't tell me on which TextView on that row was clicked on.
I would like to take different action depending on which TextView a user click on in a listview
listview
-----------------------
TextView1 : TextView2 | <--Row1
-----------------------
TextView1 : TextView2 | <--Row2
-----------------------
TextView1 | TextView2 | <--Row3
-----------------------
I would like to be able to tell, that row 2's TextView2 was click on.
Hopefully I am explaining myself well enough.
Thanks you
you can use below code in your List Activity
setListAdapter(new ArrayAdapter<String>(this, R.layout.your name, String Name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
If the click is being handled by onItemClick, then you cannot know from this which View WITHIN that View was clicked. An OnItemClickListener just handles clicking a list item. What you might try is using a custom adapter in which you grab hold of all the Views in the row in getView() and set onClickListeners on them there. Or you can define the onClickListeners directly in your View class if possible.
you can handle the click of different view's in Adapter.The Adapter must be customized
I needed to do something similar to set a tag on elements within the list view, so I could find which button was clicked
myAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item,
new String[] {"ItemID", "ItemText", "ItemImage"},
new int[] {R.id.item_id, R.id.item_text, R.id.item_image})
{
#Override
public View getView (int row, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.item_id).setTag(row);
view.findViewById(R.id.item_text).setTag(row);
view.findViewById(R.id.item_image).setTag(row);
return view;
}
};
I added an onClick event for each item in the layout
<TextView
...
android:clickable="true"
android:onClick="myClickEvent"
...
/>
And then in my onClick events I had something like
public void myClickEvent(View v) {
Object o = v.getTag();
if (o instanceof Integer) {
int row = (Integer) o;
// do something
}
You can either use one onClick event per item in the view to determine which item was clicked, or you can have one onClick event for all and modify the tag to contain both the item and the row.
i´m in trouble with a custom Listview and (Checkboxes or Button). I follow a guide (the android devolopers´s cookbook) and my custom Listview show correctly. An visisble Error occured when selected Checkboxes are scrolling.(Wrong Checkboexes are checked)
I followed the guide on
http://www.vogella.de/articles/AndroidListView/article.html#listviews_performance
but it doesn´t work. How to save the state correctly?
Greeting
Andreas
You will have to save the checked items in a list variable (in your Adapter subclass) and set the correct state (checked/unchecked) depending upon whether the item is present in the list variable.
private List<MyItem> mCheckedItems; //In your adapter subclass
Add/remove an item from the list variable:
//The AdapterView.OnItemClickListener, is present where you set myListView.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
MyItem item = myAdapter.getItem(position);
myAdapter.updateCheckedItems(item);
}
public void updateCheckedItems(MyItem item) //In your adapter subclass
{
if(!mCheckedItems.contains(item))
{
mCheckedItems.add(item);
}
else
{
mCheckedItems.remove(item);
}
}
Set the correct state of the checkbox:
public View getView(int position , View view , ViewGroup parent) //In your adapter subclass
{
final MyItem item = getItem(position);
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox);
checkBox.setChecked(mCheckedItems.contains(item));
}
I have the following class in my program:
public class RZoom extends Activity {
private ArrayList<FItem> m_FItems;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fchooser);
TextView tvName = (TextView)findViewById(R.id.txtTitleShow);
m_fItems = ((ArrayList<FItem>)this.getIntent().getSerializableExtra("fItems"));
tvName.setText(this.getIntent().getExtras().getString("RName"));
ListView lv = (ListView)findViewById(R.id.listView1);
FItemAdapter adapter = new FItemAdapter(this, R.layout.row, m_FItems);
lv.setAdapter(adapter);
}
}
This class displays a custom Listview and everything works fine. My questions are:
How do I capture an item click on the Listview? All the examples I've seen seem to inherit ListActivity instead of Activity, like I'm doing.
Is there a way to iterate through the items in the listview and set the background color of the item to Red, depending on the actual item? (In other words, I want to programmically highlight an item depending on the actual item)
Any help would be appreciated!
lv.setOnItemClickListener()
Override FItemAdapter's getView() to change colors depending on the item/position
For your first question you need to grab the ListView from its ID, as you are inheriting from Activity and not from ListActivity:
ListView lv_GetViewList = (ListView)findViewById(R.id.myListView);
lv_GetViewList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
// Your Code here
}
});
2 . You can override the getView() function and then grab the desired control of the row depending upon the conditions you want to use for a particular Control or all of the controls, say, a TextView. And you can set its background color:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView tv_Text = (TextView) v.findViewById(R.id.myRowTextView);
if(tv_Text != null)
{
tv_Text.setBackgroundColor(Color.RED);
}
return v;
}