内置数组的基础适配器

时间:2014-11-06 11:10:22

标签: android arrays

我有以下代码:

public class Adapter_Larand extends BaseAdapter{
private Context mContext;
private ArrayList<String> nume_cartee;
private ArrayList<ArrayList<Integer>> capitole;

public Adapter_Larand(Context context, ArrayList<String> nume_carti, ArrayList<ArrayList<Integer>> capitole) 
{
    this.mContext = context;
    this.nume_cartee = nume_carti;
    this.capitole = capitole;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    if (convertView==null)
    {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.box_larand, null);
    }

    TextView nume_carte = (TextView) convertView.findViewById(R.id.nume_carte);
    nume_carte.setText(nume_cartee.get(position));


    return convertView;
}

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

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

从我的片段发送到此适配器2参数:nume_carti(数组包含书籍列表)和capitole(包含这些书籍章节数组的数组)。

关系是:

nume_carti.get(position) - &gt;这本书的名字

capitole.get(position) - &gt;包含该书章节的数组

我的代码只是逐行列出了这本书的名称。

我需要什么:在我列出的每一本书中都是一个网格视图(或者可以帮助我的任何思考)。此网格视图应包含章节。并且每章都有一个setOnItemClickListener

例如:

Book 1
1 2 3 4 5 6
Book 2
1 2 3 4 5 6 7 8 9 10
Book 3
1 2 3 4 

有任何建议吗?

1 个答案:

答案 0 :(得分:0)

在getView方法中添加GridView。

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class Adapter_Larand extends BaseAdapter {
    private Context mContext;
    private ArrayList<String> nume_cartee;
    private ArrayList<ArrayList<Integer>> capitole;

    public Adapter_Larand(Context context, ArrayList<String> nume_carti, ArrayList<ArrayList<Integer>> capitole) {
        this.mContext = context;
        this.nume_cartee = nume_carti;
        this.capitole = capitole;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.box_larand, null);
        }

        TextView nume_carte = (TextView) convertView.findViewById(R.id.nume_carte);
        nume_carte.setText(nume_cartee.get(position));

        GridView gridView = (GridView) convertView.findViewById(R.id.gridView1);

        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(mContext, android.R.layout.simple_list_item_1, capitole.get(position));

        gridView.setAdapter(adapter);
        gridView.setNumColumns(capitole.get(position).size());
        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(mContext, ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

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

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

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

}