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
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();
}
});
}
}