Android Studio Sorting a Custom Listview with buttons
<p>I'm trying to make a custom listview with multiple values, which have buttons for sorting. This way the listview can be sorted for different values, like year, name and price etc.</p>

<p>I'm still pretty new to Android Studio and coding in general, so if anything is just plain off, then i apologize!</p>

<p>Problem is, nothing is sorted when i press any of the buttons. I tried using a comparable, for sorting in ascending order numerically, but that doesn't seem to work (not getting any errors, just nothing happens).</p>

<p>Here is what i've tried, showing my custom rows first, and then custom list adapter:</p>

<p>My Custom Row Layout:
    </p>

<pre><code>    &lt;TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Time:"
        android:id="@+id/time_label_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

    &lt;TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:text="time"
        android:id="@+id/time_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

    &lt;TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Price:"
        android:id="@+id/price_label_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

    &lt;TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:text="price"
        android:id="@+id/price_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

    &lt;TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Year:"
        android:id="@+id/year_label_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

    &lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="year"
        android:id="@+id/year_row"
        android:gravity="center"
        android:layout_weight="1"/&gt;

&lt;/LinearLayout&gt;

&lt;View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorPrimaryDark"
    /&gt;
</code></pre>

<p>My Custom Listview (only the buttons insteresting here):</p>

<pre><code>&lt;LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"&gt;
&lt;Button
    android:text="Time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_time"
    android:layout_weight="1"
    android:onClick="TimeSortClick"/&gt;

&lt;Button
    android:text="Price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_price"
    android:layout_weight="1"
    android:onClick="PriceSortClick"/&gt;

&lt;Button
    android:text="Year"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_year"
    android:layout_weight="1"
    android:onClick="YearSortClick"/&gt;

&lt;/LinearLayout&gt;

&lt;ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list_view"
    android:smoothScrollbar="true"/&gt;
</code></pre>

<p>My Custom Adapter:</p>

<pre><code>public class CustomAdapter extends BaseAdapter {
String [] result1;
String [] result2;
String [] result3;
Context context;

private static LayoutInflater inflater=null;
public CustomAdapter(ListViewActivity mainActivity, String[] priceOrderList, String [] timeOrderList, String [] yearOrderList) {
    // TODO Auto-generated constructor stub
    result1=priceOrderList;
    result2=timeOrderList;
    result3=yearOrderList;
    context=mainActivity;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result1.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.row_layout, null);
    holder.tv=(TextView) rowView.findViewById(R.id.price_row);
    holder.tv.setText(result1[position]);
    holder.tv=(TextView) rowView.findViewById(R.id.time_row);
    holder.tv.setText(result2[position]);
    holder.tv=(TextView) rowView.findViewById(R.id.year_row);
    holder.tv.setText(result3[position]);
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result1[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}
</code></pre>

<p>My ListView Activitiy(or activity where i inflate my custom listview):</p>

<pre><code>public class ListViewActivity extends Activity implements View.OnClickListener {

ListView view_list;
Context context;

ArrayList priceOrder;
public static String [] priceOrderList={"50$","100$","120$","40$"};
public static String [] timeOrderList={"10min","12min","5min","4min"};
public static String [] yearOrderList={"2010","2011","2012","2004"};

private Button mTimeButton;
private Button mPriceButton;
private Button mYearButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    context=this;

    mTimeButton = (Button) findViewById(R.id.sort_time);
    mPriceButton = (Button) findViewById(R.id.sort_price);
    mYearButton = (Button) findViewById(R.id.sort_year);
    view_list = (ListView) findViewById(R.id.list_view);
    view_list.setAdapter(new CustomAdapter(this, priceOrderList, timeOrderList, yearOrderList));

    mTimeButton.setOnClickListener(this);
    mPriceButton.setOnClickListener(this);
    mYearButton.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    if (v == mPriceButton){

        String pricesort = ("Sorting for Price");

        Toast.makeText(ListViewActivity.this, pricesort, Toast.LENGTH_SHORT).show();

        Comparator&lt;String&gt; StringAscComparator = new Comparator&lt;String&gt;() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(priceOrderList), StringAscComparator);

    } else if (v == mTimeButton){

        String timesort = ("Sorting for Time");

        Toast.makeText(ListViewActivity.this, timesort, Toast.LENGTH_SHORT).show();

        Comparator&lt;String&gt; StringAscComparator = new Comparator&lt;String&gt;() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(timeOrderList), StringAscComparator);

    } else if (v == mYearButton){

        String yearsort = ("Sorting for Year");

        Toast.makeText(ListViewActivity.this, yearsort, Toast.LENGTH_SHORT).show();

        Comparator&lt;String&gt; StringAscComparator = new Comparator&lt;String&gt;() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(yearOrderList), StringAscComparator);

    }
}
</code></pre>

<p>Anyways, thanks for the help! It's much appreciated!</p>