简体   繁体   中英

Cant display the item of my List View

I have a project for my school making a FAB and floating label.

The question is, the item on the list view is not diplayed and i having a hard time fixing on that.

I have two java class. MainActivity.java and MyCustomAdapter.java

Here is code for MainActivity.java

package com.example.sugara.floatingaction_mario;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ListView myList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




    myList = (ListView) findViewById(R.id.list);
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "Row " + position + " clicked", Toast.LENGTH_SHORT).show();
        }
    });



    FloatingActionButton FAB = (FloatingActionButton) findViewById(R.id.fab);
    FAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showInputDialog();
        }
    });

    final ArrayList list = new ArrayList<>();

    list.add("Richard Felmon age 23");
    list.add("Nestor Mersy age 44");
    list.add("Bruto Char age 12");
    list.add("Filemon Mandela age 33");
    list.add("Sukyuu Nirasu age 39");

  //  final MainActivity adapter = new MyCustomAdapter(MainActivity.this, list);
  //  myList.setAdapter(adapter);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

protected void showInputDialog() {

    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.activity_second, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilder.setView(promptView);

    alertDialogBuilder.setCancelable(false).setPositiveButton("Save", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            Toast.makeText(getApplicationContext(), "Data saved ", Toast.LENGTH_LONG).show();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}
}

And here is my MyCustomAdapter.java

package com.example.sugara.floatingaction_mario;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class MyCustomAdapter extends AppCompatActivity {


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    //Displaying TextInputLayout Error
    TextInputLayout lNameLayout = (TextInputLayout) findViewById(R.id.lNameLayout);
    lNameLayout.setErrorEnabled(true);
    lNameLayout.setError("Min 2 chars required");

    //Displaying EditText Error
    EditText age = (EditText) findViewById(R.id.age);
    age.setError("Required");
}

}

I have tried making adapter for listview but when i do so It is clashing with

public class MyCustomAdapter extends AppCompatActivity {

Can you help me fixing this and make the input displayed on the listview too?

I am sorry if it's confusing, it's my first time posting a question like this

Your Adapter must extend an Adapter class of Android. Try

public class MyCustomAdapter extends BaseAdapter {

and then implementing the necessary methods.

If you want only text in the ListView than you may not customize the Adapter Class. Please use this. This will help you.

Create layout with TextView(layout_list.xml)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:padding="10dp"
    android:textSize="20sp" >
</TextView>

Modify MainActivity.java like this.

...
final ArrayList list = new ArrayList<>();
list.add("Richard Felmon age 23");
list.add("Nestor Mersy age 44");
list.add("Bruto Char age 12");
list.add("Filemon Mandela age 33");
list.add("Sukyuu Nirasu age 39");
myList.setAdapter(new ArrayAdapter<String>(this, R.layout.layout_list, list));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM