I have created a custom ArrayList which is working as it should barring an issue with the onClick. I orginally had the ArrayList highlight the selected row in red when the user clicked on the row. However, I also required the position of that click outside of the ArrayAdapter class.
The only way I could seem to do this is by adding an onClickListener, however this is were the issues began, although this solved my issue of getting the position the user click on, the row highlighting now seems to get overridden.
So basically my issue is allowing both selected rows to be highlighted and position to be passed to a variable outside of the ArrayAdapter class, please see my code below:
Array Adapter Class:
public class Shop extends ListActivity
{
private static class MyAdapter extends ArrayAdapter<String>
{
private ArrayList<String> data1, data2, data3, data4;
public MyAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> data1, ArrayList<String> data2, ArrayList<String> data3, ArrayList<String> data4)
{
super(context, resource, textViewResourceId, data1);
this.data1 = data1;
this.data2 = data2;
this.data3 = data3;
this.data4 = data4;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = super.getView(position, convertView, parent);
TextView t1 = (TextView) v.findViewById(R.id.data1);
t1.setText(data1.get(position));
TextView t2 = (TextView) v.findViewById(R.id.data2);
t2.setText(data2.get(position));
TextView t3 = (TextView) v.findViewById(R.id.data3);
t3.setText(data3.get(position));
TextView t4 = (TextView) v.findViewById(R.id.data4);
t4.setText(data4.get(position));
spnCharacters.setVisibility(View.GONE);
Iterator<Integer> iterator = unavailableItem.iterator();
while (iterator.hasNext())
{
int NotAvailable = iterator.next();
if(position == NotAvailable)
{
v.setBackgroundColor(Color.rgb(170, 170, 170));
}
}
if(unavailableItem.size() == 1)
{
if(position == unavailableItem.get(0))
{
v.setBackgroundColor(Color.rgb(170, 170, 170));
}
}
if (position == 0)
{
v.setBackgroundResource(android.R.drawable.title_bar);
}
v.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
currentPositon = position;
if(data2.get(currentPositon).equals("0"))
{
btnSell.setEnabled(false);
}
else
{
btnSell.setEnabled(true);
}
}
});
return v;
}
}
private void NeedPosition()
{
int position = // need ArrayAdapter exact position here
}
}
ArrayAdapter XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/data1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/data2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/data3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/data4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="0.5"
android:gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
List View XML:
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearButtons"
android:layout_above="@+id/linerBuyBtns"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:listSelector="@drawable/list_selector">
</ListView>
ListActvity has its own way to manage cell click.
This method will be called when you click on an row of the
ListView. As you can see the second paramter is thepositionyou need