This is a Custom listview on the android studio. here's the code.
here's the XML activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView">
</ListView>
</LinearLayout>
here's the XML row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="vertical"
tools:ignore="UselessParent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/full_name"
android:textColor="#000"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:id="@+id/Full_NameTV"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/section"
android:textStyle="bold"
android:textColor="#a9a9a9a9"
android:layout_margin="5dp"
android:textSize="10sp"
android:id="@+id/SectionTV"
tools:ignore="SmallSp" />
</LinearLayout>
</LinearLayout>
Here's the java code MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] mFull_Name = {"User 1", "User 2", "User 3", "User 4"};
String[] mSection = {"User 1 Section", "User 2 Section", "User 3 Section", "User 4 Section"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(android.R.id.list);
MyAdapter adapter = new MyAdapter(this, mFull_Name, mSection);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(MainActivity.this, "User 1 Section", Toast.LENGTH_SHORT).show();
}
if (position == 0) {
Toast.makeText(MainActivity.this, "User 2 Section", Toast.LENGTH_SHORT).show();
}
if (position == 0) {
Toast.makeText(MainActivity.this, "User 3 Section", Toast.LENGTH_SHORT).show();
}
if (position == 0) {
Toast.makeText(MainActivity.this, "User 4 Section", Toast.LENGTH_SHORT).show();
}
}
});
}
class MyAdapter extends ArrayAdapter<String> {
String[] rFull_Name;
String[] rSection;
MyAdapter(Context c, String[] Full_Name, String[] Section) {
super(c, R.layout.row, R.id.listView, Full_Name);
this.rFull_Name = Full_Name;
this.rSection = Section;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row, parent, false);
TextView myFull_Name = row.findViewById(R.id.Full_NameTV);
TextView mySection = row.findViewById(R.id.SectionTV);
return row;
}
}
}
every time I run it crashes but whenever I try to delete the java codes it runs. I don't know what's the problem or what is the error. I've been debugging it for almost 1 day now.