0

After days upon days of searching for a solution... I implemented a Scrolling, 4 column ListView with Row Click that consumes just the middle portion of my app's main screen. It is precisely what I need for my app and very near perfect aeshetically. Screen shot below

Listview looking good

The only issue is this. When I scroll through the list, the rows become out of sequence and randomly change sequence. Columns within each row remain consistent. But the rows seem to move. Screen shot below showing out of sequence. The "Sequencing Problem" is dynamic. Meaning, the more you scroll the more random the sequence becomes.

Rows get out of sequence on scroll

I have read a listview is considered old-school, problematic with large datasets, etc but I have been unable to find anything else that will support 4 columns, scrolling, not take the whole screen, with row click, etc. My data set for the above is just 20-50 rows.

Help Needed: 1 - Is there a way to fix the row sequencing problem above? If yes, is there also a way to introduce grid lines in my current listview above? 2 - If no, how may I acheive the same visual results with something newer like RecyclerView, TableLayout etc.

Here is my code:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ftonyan.listviewmulticolumns.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="350dp"
        android:layout_height="150dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="153dp"></ListView>

</RelativeLayout>

colmn_row.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">

    <TextView
        android:id="@+id/name"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.2"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/gender"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.35" />

    <TextView
        android:id="@+id/age"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.5" />

    <TextView
        android:id="@+id/status"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1.5" />

</LinearLayout>

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
    TextView txtFourth;
    public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity=activity;
        this.list=list;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=activity.getLayoutInflater();
        if(convertView == null){
            convertView=inflater.inflate(R.layout.colmn_row, null);
            txtFirst=(TextView) convertView.findViewById(R.id.name);
            txtSecond=(TextView) convertView.findViewById(R.id.gender);
            txtThird=(TextView) convertView.findViewById(R.id.age);
            txtFourth=(TextView) convertView.findViewById(R.id.status);
        }
        HashMap<String, String> map=list.get(position);
        txtFirst.setText(map.get(FIRST_COLUMN));
        txtSecond.setText(map.get(SECOND_COLUMN));
        txtThird.setText(map.get(THIRD_COLUMN));
        txtFourth.setText(map.get(FOURTH_COLUMN));
        return convertView;
    }
}

MainActivity.java

public class MainActivity extends Activity {


    private ArrayList<HashMap<String, String>> list;

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

        String localFormatMinSec = "HH:mm:ss";
        SimpleDateFormat localSDFMinSec = new SimpleDateFormat(localFormatMinSec);

        ListView listView=(ListView)findViewById(R.id.listView1);

        list=new ArrayList<HashMap<String,String>>();

        if (1 == 1) {

            for (int i = 0; i < 14; i++) {
                HashMap<String, String> temp2 = new HashMap<String, String>();
                temp2.put(FIRST_COLUMN, String.valueOf(master.slave.get(i).getSequence()));
                temp2.put(SECOND_COLUMN, master.slave.get(i).getName());
                temp2.put(THIRD_COLUMN, localSDFMinSec.format(master.slave.get(i).getEstEnd()));
                temp2.put(FOURTH_COLUMN, master.slave.get(i).getMessage());
                list.add(temp2);
            }

        }

        ListViewAdapter adapter=new ListViewAdapter(this, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {
                int pos=position+1;
                Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
            }

        });

    }

}
Frank Zappa
  • 451
  • 2
  • 11
  • 23
  • With that `Adapter` code, you'll still need to find the `TextView`s each time in `getView()`. That is, move all four `convertView.findViewById()` lines to after the `if` block. – Mike M. Apr 07 '17 at 02:48
  • Perfect - that fixed it - thank you very much. By the way if you can point me to to a RecyclerView / LinearLayoutManager example/tutorial that produces the same visual result, I would like to give it a try to "Modernize" what I am doing. Thanks again for your help. – Frank Zappa Apr 07 '17 at 03:03
  • There's a simple `RecyclerView` example in [this post](http://stackoverflow.com/a/40584425). You'll just need to change it to handle four `TextView`s in the item layout, instead of the one it shows. – Mike M. Apr 07 '17 at 03:09

0 Answers0