CustomListViewAdapter不起作用

时间:2016-04-25 21:32:25

标签: android listview android-arrayadapter navigation-drawer android-studio-2.0

我是android的新手,并使用工具栏,抽屉,导航列表。我使用CustomlistviewAdapter用我的数据填充我的列表。 代码没有错误,但是当我启动程序时关闭程序。问题来自Listview / Adapter部分,因为当我删除它时,程序成功运行主活动(没有列表)。

请有人帮忙解决问题。

以前感谢您的帮助。

主要活动代码:

import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
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;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;

private Toolbar Toolbar;
private ListView Mylist;
private CustomListViewAdapter customListViewAdapter;


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

    Toolbar = (Toolbar) findViewById(R.id.toolbar);
    assert Toolbar != null;
    Toolbar.setLogo(R.drawable.logo);
    Toolbar.setNavigationIcon(R.drawable.ericsson);

    setSupportActionBar(Toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.navigation);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,Toolbar,R.string.app_name,R.string.app_name);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);


    final String[] titles = new String[] {"Home" ,"Cabinet Viewer", "Node Viewer", "Connectivity Viewer", "Connection Tracker"};
    ArrayList<HashMap<String, String>> itemslist= new ArrayList<>();


    for (int i = 0; i < 5; i++){
        HashMap<String, String> data = new HashMap<>();
        data.put("title", titles[i]);
        itemslist.add(data);
    }

    Mylist = (ListView) findViewById(R.id.navigation_list);
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), itemslist);
    Mylist.setAdapter(customListViewAdapter);




    Mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int newposition = position;
            String itemClickedId = Mylist.getItemAtPosition(newposition).toString();
            Toast.makeText(getApplicationContext(), "ID Clicked: " + itemClickedId, Toast.LENGTH_LONG).show();
        }
    });


}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.exit) {
        finish();
        return true;
    }
    if (id == R.id.home) {
        drawerLayout.openDrawer(GravityCompat.START);  // OPEN DRAWER
        return true;
   }
return super.onOptionsItemSelected(item);

}
}

我的CustomListViewUdapter代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class CustomListViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<HashMap<String, String>> Items;
private static LayoutInflater inflater = null;


public CustomListViewAdapter (Context context, ArrayList<HashMap<String, String>> data){

    mContext = context;
    Items = data;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


}

@Override
public int getCount() {
    return Items.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (convertView == null){
        view = inflater.inflate(R.layout.list_row, null);
        TextView title = (TextView)view.findViewById(R.id.listiitemtitle);
        ImageView icon = (ImageView)view.findViewById(R.id.listitemicon);

        HashMap<String, String> mitems = new HashMap<>();

        mitems = Items.get(position);

        title.setText(mitems.get("title"));
            icon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ericsson, null));

    }

    return null;
}
}

我的主要活动XML:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation"
android:fitsSystemWindows="true">
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="esmviewer.myandroid.com.esmviewer.MainActivity">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="@color/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Light"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
    </android.support.v7.widget.Toolbar>

</LinearLayout>

<ListView
android:layout_width="220dp"
android:layout_height="match_parent"
android:id="@+id/navigation_list"
android:layout_gravity= "start"
android:choiceMode="singleChoice"
android:divider="@color/colorPrimary"
android:dividerHeight="1dp"

android:background="#B9F6CA"/>
</android.support.v4.widget.DrawerLayout>

    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="esmviewer.myandroid.com.esmviewer.MainActivity">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Welcome"
                android:textStyle="bold"
                android:textSize="30sp"
                android:textColor="#EF5350"
                android:id="@+id/Welcome"
                android:layout_above="@+id/esm"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="50dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="You can explore our Datacenters
by clicking on the main menu"
                android:textSize="20sp"
                android:textAlignment="center"
                android:id="@+id/welcome_description"
                android:layout_marginBottom="124dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Ericsson Services Manager"
                android:textSize="25sp"
                android:textStyle="bold"
                android:id="@+id/esm"
                android:textColor="#1A237E"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Viewer Version"
                android:textSize="20sp"
                android:textStyle="bold"
                android:textColor="#000000"
                android:id="@+id/viewer_version"
                android:layout_below="@+id/esm"
                android:layout_centerHorizontal="true" />

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/imageView"
                android:background="@drawable/etisalat"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true" />

    </RelativeLayout>

  </RelativeLayout>

我的CustomList XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:padding="5dp"
android:orientation="horizontal">

<android.support.v7.widget.LinearLayoutCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iconid"
    android:padding="3dp"
    android:layout_alignParentLeft="true"
    android:layout_margin="5dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/listitemicon"
        android:src="@drawable/ericsson"

        />

</android.support.v7.widget.LinearLayoutCompat>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Item"
    android:id="@+id/listiitemtitle"
    android:textStyle="bold"
    android:textSize="25sp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="60dp"

    />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我可以在getView()中看到一个错误 它必须返回view,而不是null

public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
...
return view; }