自arrayAdapter与listFragment [英] Custom arrayAdapter with listFragment

查看:180
本文介绍了自arrayAdapter与listFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个ListFragment。我看了API演示(FragmentLayout)。它可以在一个简单的例子,现在我想将它应用到我现有的项目。

I'm trying to make a ListFragment. I looked the Api Demo (FragmentLayout). it works on a simple example and now i want to apply it to my existing project.

下面是我的code。我创建内部类(RecipeList&安培; RecipeDetail)作为API演示

Here is my code. I create inner classes (RecipeList & RecipeDetail) as in the Api Demo.

public class InfoActivity extends MenuActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_fragment_layout);

    // ...


}

    public static class RecipeList extends ListFragment {

    private int mCurrentSelectedItemIndex = -1;
    private boolean mIsTablet = false;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        accountData = new ArrayList<Account>();


        new AccountSyncTask() {
            @Override
            public void onPostExecute(
                    final ArrayList<ArrayList<String>> result) {

                    // For each retrieved account   
                                            Bd.insert(retrievedAccount);
                    accountData.add(retrievedAccount);
                }

                accountListAdapter = new AccountListAdapter(
                        InfoActivity.this, R.layout.accountlist_detail,
                        accountData);

                accountListAdapter = new AccountListAdapter(
                        activityContext, R.layout.accountlist_detail,
                        accountData);
                setListAdapter(accountListAdapter);


            }
        }.execute(sessionName, null, "getAllObjectOnServer",
                String.valueOf(nbRow));


        if (savedInstanceState != null) {
            mCurrentSelectedItemIndex = savedInstanceState.getInt(
                    "currentListIndex", -1);
        }

        // This is a tablet if this view exists
        View recipeDetails = getActivity()
                .findViewById(R.id.recipe_details);
        mIsTablet = recipeDetails != null
                && recipeDetails.getVisibility() == View.VISIBLE;


        if (mIsTablet) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }


        if (mIsTablet && mCurrentSelectedItemIndex != -1) {
            showRecipeDetails();
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCurrentSelectedItemIndex = position;
        showRecipeDetails();
    }

    private void showRecipeDetails() {
        if (mIsTablet) {

            // Set the list item as checked
            getListView().setItemChecked(mCurrentSelectedItemIndex, true);

            // Get the fragment instance
            RecipeDetail details = (RecipeDetail) getFragmentManager()
                    .findFragmentById(R.id.recipe_details);
            // Is the current visible recipe the same as the clicked? If so,
            // there is no need to update
            if (details == null
                    || details.getRecipeIndex() != mCurrentSelectedItemIndex) {

                details = RecipeDetail
                        .newInstance(mCurrentSelectedItemIndex);

                FragmentTransaction ft = getFragmentManager()
                        .beginTransaction();
                ft.replace(R.id.recipe_details, details);

                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else {

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
    }

}

public static class RecipeDetail extends Fragment {

    private int mRecipeIndex;

    public static RecipeDetail newInstance(int recipeIndex) {
        // Create a new fragment instance
        RecipeDetail detail = new RecipeDetail();
        // Set the recipe index
        detail.setRecipeIndex(recipeIndex);
        return detail;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }


        View v = inflater
                .inflate(R.layout.recipe_details, container, false);
        //..

        return v;
    }

    public int getRecipeIndex() {
        return mRecipeIndex;
    }

    public void setRecipeIndex(int index) {
        mRecipeIndex = index;
    }

}

我有一个自定义ArrayAdapter(我在ListFragment项目包含4 textViews和一个可点击的ImageButton)。

I have a custom ArrayAdapter (my items in the ListFragment contain 4 textViews and a clickable imageButton).

AccountListAdapter:

AccountListAdapter :

public class AccountListAdapter extends ArrayAdapter<Account> {

private final Context context;
private final int layoutResourceId;
private final ArrayList<Account> data;


public AccountListAdapter(Context context, int layoutResourceId,
        ArrayList<Account> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;


}



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

    AccountHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);

        holder = new AccountHolder();

        convertView.setClickable(true);
        convertView.setFocusable(true);

        holder.txtName = (TextView) convertView.findViewById(R.id.nom);
        holder.txtId = (TextView) convertView.findViewById(R.id.id);

        convertView.setTag(holder);

    } else {
        holder = (AccountHolder) convertView.getTag();
    }

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.i("click", "index = " + position);

        }

    });

    holder.txtName.setText(data.get(position).getName());
    holder.txtId.setText(data.get(position).getId());

    convertView.setBackgroundResource(R.drawable.list_selector);

    ImageButton img = (ImageButton) convertView.findViewById(R.id.check);
    img.setTag(position);

    return convertView;
}

static class AccountHolder {
    TextView txtName;
    TextView txtId;
}

}

问题:
当我点击listFragment的项目,

Problem : When i click on an Item of the listFragment,

public void onListItemClick(ListView l, View v, int position, long id) {
        mCurrentSelectedItemIndex = position;
                    Log.i("click", "here";
        showRecipeDetails();
    } 

不叫,但在AccountListAdapter定义的项目听者作品

is not called but the listener on an item defined in AccountListAdapter works

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.i("click", "index = " + position);

        }

    });

为什么onListitemClick从不叫什么名字?

Why is onListitemClick never called ?

另一个问题是:它(为了填充列表)消耗在我ListFragment的onActivityCreated功能另一个线程Web服务的正确方法?

Another question : is it a proper way to consume a web service in another thread in the onActivityCreated function of my ListFragment (in order to populate the list) ?

Thx提前

修改 =为我制造showRecipeDetails静态和调用它在我的自定义ArrayAdapter听众的时刻。

EDIT = For the moment i made "showRecipeDetails" static and call it in the listener in my custom ArrayAdapter.

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RecipeList.showRecipeDetails(position);
}}

我不满意我的解决方案,我interessed任何其他解决方案

I'm not satisfied with my solution, i'm interessed to any other solution

推荐答案

OnItemClickListeners必须首先​​您要记录点击为ListView有关。在你的 onActivityCreated(..)方法,地点 getListView()。setOnItemClickListener(本)的地方,并把实现OnItemClickListener 公共静态类RecipeList扩展ListFragment

OnItemClickListeners must first be associated with the ListView you want to record clicks for. In your onActivityCreated(..) method, place getListView().setOnItemClickListener(this) somewhere and put implements OnItemClickListener after public static class RecipeList extends ListFragment.

这篇关于自arrayAdapter与listFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆