从BaseAdapter覆盖类中传递片段之间的数据

时间:2013-04-02 06:55:13

标签: interface android-fragments argument-passing baseadapter

我可能不是最好的方式,但我不知道如何以正确的方式解决这个问题。几乎我有一个片段tabhost几乎包含我的所有内容,包括新闻源,来自新闻源的用户可以点击其他用户图片打开他们的个人资料。到目前为止,我正在采取简单的方法,只是使用一个意图打开了一个全新的tabhost实例和所有片段,我通过一个额外的意图发送了个人资料ID,我的问题解决了,但是我从那里学到了这个不是一个很好的解决方案。所以我一直试图通过这个线程的建议来修复我的应用程序:

Communication between Fragments

我的问题是bc我的用于配置文件图像的onClick侦听器在我的LazyAdapter类中,它扩展了BaseAdapter,是什么最好的方法来切换片段并根据我的情况将正确的配置文件ID传递给新片段?

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;





public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    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.list_row, null);

        TextView name = (TextView)vi.findViewById(R.id.name); // title
        TextView message = (TextView)vi.findViewById(R.id.message); // artist name
        TextView created = (TextView)vi.findViewById(R.id.created); // duration
        SmartImageView thumb_image = (SmartImageView) vi.findViewById(R.id.list_image);



        HashMap<String, String> update = new HashMap<String, String>();
        update = data.get(position); 

        // Setting all values in listview
        name.setText(update.get("name"));
        message.setText(update.get("message"));
        created.setText(update.get("created"));
        thumb_image.setImageUrl(update.get("thumb_img"));
        name.setOnClickListener(new myOnClickListener(position));
        thumb_image.setOnClickListener(new myOnClickListener(position));
        return vi;
    }

    public class myOnClickListener implements OnClickListener{
        private int position;
        private String clicked_uid;
        public myOnClickListener(int position){
            this.position=position;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            HashMap<String, String> update = new HashMap<String, String>();
            update = data.get(position); 
            Log.i("Update Position:", update.toString());
            clicked_uid=update.get("uid");
            Log.d("Clicked UID:", clicked_uid+"");
            Intent i = new Intent(activity.getApplicationContext() , TabHostFragmentActivity.class);
            i.putExtra("profile_uid", clicked_uid);
            activity.startActivity(i);
            activity.finish();

        }

    }
}

0 个答案:

没有答案