如何在自定义ListView适配器中使用StringArrays

时间:2017-02-04 20:04:42

标签: java android

我正在尝试使用StringArray在ListView中使用自定义适配器显示项目。

但它不起作用。

package ir.safarbazha.safarbazha.Acts;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.AbsSavedState;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.text.Format;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import javax.xml.datatype.DatatypeConstants;

import ir.safarbazha.safarbazha.Core.adapter.DrawerCustomListAdapter;
import ir.safarbazha.safarbazha.Core.app.AppController;
import ir.safarbazha.safarbazha.R;

import static ir.safarbazha.safarbazha.R.id.content_frame;
import static ir.safarbazha.safarbazha.R.id.toolbar;

public class HomeAct extends AppCompatActivity {

    ActionBar homeActionbar;
    String[] draweritems;
    List<String> drawerItems;
    int homeDrawerMenuItemsNumber=1;
    DrawerCustomListAdapter drawerCustomListAdapter;
    DrawerLayout homeDLayout;
    ListView homeDrawerListView;
    ImageView mainToolbarMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Localize...
        if (!Locale.getDefault().getLanguage().equals("fa")){
            String languageToLoad  = "fa";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());
            this.setContentView(R.layout.act_about);
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        }
        setContentView(R.layout.act_home);
        homeDLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        homeDrawerListView=(ListView)findViewById(R.id.drawer_lv);

        //Set Custom ToolBar...
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        homeActionbar=getSupportActionBar();
        final LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.toolbar, null);
        homeActionbar.setCustomView(v);
        homeActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        //Set Custom ToolBar Objects...
        //MainToolBar Menu Image Object...
        mainToolbarMenu=(ImageView)findViewById(R.id.nav_drawer_menu);
        mainToolbarMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (homeDLayout.isDrawerOpen(GravityCompat.START))
                    homeDLayout.closeDrawer(GravityCompat.START);
                else homeDLayout.openDrawer(GravityCompat.START);
            }
        });

        //Set Custom Home Navigation Drawer...
        draweritems= getResources().getStringArray(R.array.main_titles);
        drawerItems=new ArrayList<String>(Arrays.asList(draweritems));
        drawerCustomListAdapter=new DrawerCustomListAdapter(this,draweritems);
        homeDrawerListView.setAdapter(drawerCustomListAdapter);
       // homeDrawerListView.setAdapter(new ArrayAdapter<String>(
        //        this,R.layout.drawer_list_row,homeDrawerMenuItems));


       // homeDrawerListView.setAdapter(new ArrayAdapter<String>(
        //        this,android.R.layout.simple_list_item_1,homeDrawerMenuItems));

        homeDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem=getResources().getStringArray(R.array.main_titles)[position];
                Toast.makeText(getBaseContext(),"برروی "+selectedItem+"کلیک شد!",Toast.LENGTH_SHORT).show();
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

    }
}
package ir.safarbazha.safarbazha.Core.adapter;


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.List;

import ir.safarbazha.safarbazha.R;

public class DrawerCustomListAdapter extends BaseAdapter{

    public Context context;
    public List<String> drawerItems;
    private static LayoutInflater inflater=null;

    public void drawerCustomListViewItemRow(
            Context context,List<String> drawerItems
    ){
        this.context=context;
        this.drawerItems=drawerItems;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return drawerItems.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return drawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.drawer_list_row, null);
        TextView title = (TextView) vi.findViewById(R.id.drawer_lv_title);
        title.setText(drawerItems.get(position));
        ImageView icon=(ImageView)vi.findViewById(R.id.drawer_lv_icon);
        icon.setImageResource(R.drawable.ic_menu_left);
        return vi;
    }

    /*

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.drawer_list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.drawer_lv_title); // title
        ImageView icon=(ImageView)vi.findViewById(R.id.drawer_lv_icon); // thumb image

        // Setting all values in listview
        title.setText(R.string.app_name);
        icon.setImageResource(R.drawable.ic_account_circle);
        return vi;
    }
    */
}

我的Android Studio说问题出在HomeAct中,我在其中键入:(this,draweritems);

3 个答案:

答案 0 :(得分:0)

您必须在适配器类中添加构造函数

将你的drawerCustomListViewItemRow方法名改为:

public DrawerCustomListAdapter(Context context, List<string> drawerItems)

还有一件事是您必须将HomeAct中的String[] draweritems;更改为List<string> draweritems;或将适配器列表更改为字符串arary

答案 1 :(得分:0)

您在此处添加了void,因此创建了一个方法,而不是构造函数。构造函数没有返回类型。

删除它

public **void** drawerCustomListViewItemRow(
        Context context,List<String> drawerItems
){
    this.context=context;
    this.drawerItems=drawerItems;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

同时将您的班级名称的D从drawerCustomListViewItemRow变为大写DrawerCustomListAdapter

在创建适配器时使用drawerItems列表,而不是数组draweritems。可能最好只存储一个或另一个,而不是两者都存储。

答案 2 :(得分:0)

我修改了你的代码。请检查区别。您的 DrawerCustomListAdapter 构造函数不正确。

  

包ir.safarbazha.safarbazha.Acts;

     

import android.content.Context; import android.content.Intent;进口   android.content.pm.ActivityInfo;进口   android.content.res.Configuration; import android.os.Bundle;进口   android.support.design.widget.FloatingActionButton;进口   android.support.design.widget.Snackbar;进口   android.support.v4.view.AbsSavedState;进口   android.support.v4.view.GravityCompat;进口   android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;   import android.support.v4.widget.DrawerLayout;进口   android.support.v7.app.ActionBar;进口   android.support.v7.app.AlertDialog;进口   android.support.v7.app.AppCompatActivity;进口   android.support.v7.widget.Toolbar; import android.view.Gravity;进口   android.view.LayoutInflater; import android.view.View;进口   android.widget.AdapterView; import android.widget.ArrayAdapter;进口   android.widget.ImageView; import android.widget.ListView;进口   android.widget.Toast;

     

import java.lang.reflect.Field;进口   java.lang.reflect.InvocationHandler; import java.text.Format;进口   java.util.ArrayList的; import java.util.Arrays;进口   java.util.Comprator接口; import java.util.List; import java.util.Locale;

     

import javax.xml.datatype.DatatypeConstants;

     

import ir.safarbazha.safarbazha.Core.adapter.DrawerCustomListAdapter;   import ir.safarbazha.safarbazha.Core.app.AppController;进口   ir.safarbazha.safarbazha.R;

     

import static ir.safarbazha.safarbazha.R.id.content_frame;进口   static ir.safarbazha.safarbazha.R.id.toolbar;

     

公共类HomeAct扩展了AppCompatActivity {

ActionBar homeActionbar;
String[] draweritems;
List<String> drawerItems;
int homeDrawerMenuItemsNumber = 1;
DrawerCustomListAdapter drawerCustomListAdapter;
DrawerLayout homeDLayout;
ListView homeDrawerListView;
ImageView mainToolbarMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Localize...
    if (!Locale.getDefault().getLanguage().equals("fa")) {
        String languageToLoad = "fa";
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        this.setContentView(R.layout.act_about);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    }
    setContentView(R.layout.act_home);
    homeDLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    homeDrawerListView = (ListView) findViewById(R.id.drawer_lv);

    //Set Custom ToolBar...
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    homeActionbar = getSupportActionBar();
    final LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.toolbar, null);
    homeActionbar.setCustomView(v);
    homeActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    //Set Custom ToolBar Objects...
    //MainToolBar Menu Image Object...
    mainToolbarMenu = (ImageView) findViewById(R.id.nav_drawer_menu);
    mainToolbarMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (homeDLayout.isDrawerOpen(GravityCompat.START))
                homeDLayout.closeDrawer(GravityCompat.START);
            else homeDLayout.openDrawer(GravityCompat.START);
        }
    });

    //Set Custom Home Navigation Drawer...
    draweritems = getResources().getStringArray(R.array.main_titles);
    drawerItems = new ArrayList<String>(Arrays.asList(draweritems));
    drawerCustomListAdapter = new DrawerCustomListAdapter(this, draweritems);
    homeDrawerListView.setAdapter(drawerCustomListAdapter);
    // homeDrawerListView.setAdapter(new ArrayAdapter<String>(
    //        this,R.layout.drawer_list_row,homeDrawerMenuItems));


    // homeDrawerListView.setAdapter(new ArrayAdapter<String>(
    //        this,android.R.layout.simple_list_item_1,homeDrawerMenuItems));

    homeDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedItem = getResources().getStringArray(R.array.main_titles)[position];
            Toast.makeText(getBaseContext(), "برروی " + selectedItem + "کلیک شد!", Toast.LENGTH_SHORT).show();
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

} }
     

包ir.safarbazha.safarbazha.Core.adapter;

    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.List;

    import ir.safarbazha.safarbazha.R;
     

公共类DrawerCustomListAdapter扩展了BaseAdapter {

public Context context;
public List<String> drawerItems;
private static LayoutInflater inflater = null;

public void DrawerCustomListAdapter(
        Context context, List<String> drawerItems
) {
    this.context = context;
    this.drawerItems = drawerItems;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return drawerItems.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return drawerItems.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.drawer_list_row, null);
    TextView title = (TextView) vi.findViewById(R.id.drawer_lv_title);
    title.setText(drawerItems.get(position));
    ImageView icon = (ImageView) vi.findViewById(R.id.drawer_lv_icon);
    icon.setImageResource(R.drawable.ic_menu_left);
    return vi;
}

/*

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.drawer_list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.drawer_lv_title); // title
    ImageView icon=(ImageView)vi.findViewById(R.id.drawer_lv_icon); // thumb image

    // Setting all values in listview
    title.setText(R.string.app_name);
    icon.setImageResource(R.drawable.ic_account_circle);
    return vi;
}
*/ }