从列表视图获取数据与BaseAdapter

从列表视图获取数据与BaseAdapter

本文介绍了从列表视图获取数据与BaseAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个活动(A)一按钮键,当用户preSS,它会增加一个对象静态列表。我已经有一个的ListView 显示静态列表的内容,另一个活动(B)。在活动的(B)中欲使用存储在静态列表中的特定行中的对象的属性。我该怎么办呢?我的code如下所示:

  //活动(A):
Send.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){
...
//添加到mycaseList
All_Static.MyCaseList.add(案例1);
...
}});// ================================================ ==============================//活性(B):公共类扩展了我的编目活动{     布尔returnvar;
    私人活动mContext;
    按钮的CreateForm;
    按钮ConfExpandRegion,Cancelb;
    串ExpandMsg,CancelMsg;
    布尔B:
    MyCaseClass mycase;
    TextView的tvCase;
    AlertDialog alertDialog;    //为列表
    ListView控件列表;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mypage);
        //移动到另一个活动
        =的CreateForm(按钮)findViewById(R.id.creat_new_formbtn);
        createForm.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){                意向J =新意图(MyPage.this,CreateNewForm.class);
                startActivity(J);            }
        });        // ================================================ ============================================
        //为列表
            名单=(ListView控件)findViewById(R.id.mypage_list);
            list.setClickable(真);            最终名单< MyCaseClass> listOfMyCases =新的ArrayList< MyCaseClass>();            MyCasesListAdapter适配器=新MyCasesListAdapter(这一点,listOfMyCases,MyPage.this);            对于(MyCaseClass玛:All_Static.getMyCaseList())
                adapter.add(新MyCaseClass(米));            //填充后适配器..列表分配给适配器
            list.setAdapter(适配器);
            list.setOnItemClickListener(新OnItemClickListener(){                公共无效onItemClick(适配器视图<>为arg0,观景,INT位置,长指数){
                }
            });        // ================================================ ========================================    }    公共无效sendSMS(串号,弦乐味精)抛出异常{
        如果(!B){
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(数字,空,味精,NULL,NULL);
        }
        B =真实的;
    }    // ================================================ ========================    公共无效ShowingDialogExpand(){
        最后AlertDialog alertDialog =新AlertDialog.Builder(本).create();
        alertDialog.setTitle(构象);
        alertDialog.setMessage(你确定要展开报表区域?);
        alertDialog.setButton(是,新DialogInterface.OnClickListener(){
               公共无效的onClick(DialogInterface对话,诠释它){                   ExpandMsg =CASE_ID扩大;
                   如果(!B){
                        尝试{
                            //应该写在这里的服务器号+聊天必须大于推
                            sendSMS(0000,ExpandMsg);
                            Toast.makeText(MyPage.this,请求发送,Toast.LENGTH_LONG)
                                    。显示();
                        }赶上(例外五){
                            // TODO自动生成catch块
                            Toast.makeText(MyPage.this,e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }                    }
                   //ConfExpandRegion.setEnabled(false);
               }            });            alertDialog.setButton2(否,新DialogInterface.OnClickListener(){
               公共无效的onClick(DialogInterface对话,诠释它){
                //这里你可以添加功能
                // 没做什么
               }
            });            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
    }    公共无效ShowingDialogCancel(int i)以{
        最后AlertDialog alertDialog2 =新AlertDialog.Builder(本).create();
        alertDialog2.setTitle(构象?);
        alertDialog2.setMessage(你确定要取消点¯x案件?);        字符串str =ID:+ I;
        Toast.makeText(MyPage.this,STR,0)
        。显示();
        alertDialog2.setButton(是,新DialogInterface.OnClickListener(){
               公共无效的onClick(DialogInterface对话,诠释它){                     CancelMsg =CASE_ID取消;
                     returnvar = TRUE;                   如果(!B){
                        尝试{
                            //应该写在这里的服务器号+聊天必须大于推
                            sendSMS(0000,CancelMsg);
                            Toast.makeText(MyPage.this,对消请求派,Toast.LENGTH_LONG)
                                    。显示();                        }赶上(例外五){
                            // TODO自动生成catch块
                            Toast.makeText(MyPage.this,e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }                    }
                   }
            });            alertDialog2.setButton2(否,新DialogInterface.OnClickListener(){
               公共无效的onClick(DialogInterface对话,诠释它){
                //这里你可以添加功能
                // 没做什么
                   returnvar = FALSE;
                   Toast.makeText(MyPage.this,内无,0)
                    。显示();
               }
            });            alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog2.show();    }}
// ================================================ =================================//我BaseAdapter
公共类MyCasesListAdapter延伸BaseAdapter {
    私人我的页面我的页面;
    私人列表< MyCaseClass> listOfCases;
    私人活动parentActivity;    // TODO测试
    MyCaseClass进入;    // TODO删除它不是小鬼。
    公共MyCasesListAdapter(){        超();    }    公共MyCasesListAdapter(我的页面我的页面,列表与LT; MyCaseClass> listOfCaseParameter,活动parentActivity){
        this.myPage =我的页面;
        this.listOfCases = listOfCaseParameter;
        this.parentActivity = parentActivity;
    }    公众诠释的getCount(){
        返回listOfCases.size();
    }    公共对象的getItem(INT位置){
        返回listOfCases.get(位置);
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,ViewGroup中的ViewGroup){         进入= listOfCases.get(位置);
         //this.getitem(position)
        如果(convertView == NULL){            LayoutInflater吹气=(LayoutInflater)MYPAGE
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row,NULL);
          }
        //这是行项目..
        //设置onClick的监听器此按钮
        按钮ConfExpandRegion =(按钮)convertView.findViewById(R.id.expand);
        按钮Cancelb =(按钮)convertView.findViewById(R.id.cancelCase);
        TextView的tvCase =(TextView中)convertView.findViewById(R.id.mypage_name);        //要成为一个可点击的按钮
        ConfExpandRegion.setFocusableInTouchMode(假);
        ConfExpandRegion.setFocusable(假);
        ConfExpandRegion.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                myPage.ShowingDialogExpand();
            }
        });        //要成为一个可点击的按钮
        Cancelb.setFocusableInTouchMode(假);
        Cancelb.setFocusable(假);
        Cancelb.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                条目=(MyCaseClass)v.getTag();
                INT caseid = entry.getID();
                myPage.ShowingDialogCancel(caseid);                Toast.makeText(我的空间,​​里面呼唤,0).show();                // MyCaseClass条目=(MyCaseClass)v.getTag();
                //listOfCases.remove(entry);                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });        //设置项,这样就可以捕捉单击了哪个项目,
        //然后将其删除
        //作为替代方案,可以使用该项目的ID /位置捕捉
        //项目
        //被点击。
        ConfExpandRegion.setTag(输入);
        Cancelb.setTag(输入);        // btnRemove.setId(位置);        返回convertView;
    }    公共无效的onClick(查看视图){
        MyCaseClass条目=(MyCaseClass)view.getTag();
        listOfCases.remove(输入);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();    }    私人无效的ShowDialog(MyCaseClass进入){
        //创建并显示您的对话
        //根据对话框按钮点击删除或什么都不做
    }    公共无效添加(MyCaseClass myCaseClass){
        // TODO自动生成方法存根
        listOfCases.add(myCaseClass);
    }}
// ================================================ ===========================
// StaticClass
公共类All_Static {    公共静态列表< MyCaseClass> MyCaseList =新的ArrayList< MyCaseClass>();    公共静态列表< MyCaseClass> getMyCaseList(){
        返回MyCaseList;
    }    公共静态无效setMyCaseList(列表< MyCaseClass> myCaseList){
        MyCaseList = myCaseList;
    }    // ================================================ ===========================
    公共静态列表< HelpersClass> HelperList =新的ArrayList< HelpersClass>();    公共静态列表< HelpersClass> getHelperList(){
        返回HelperList;
    }    公共静态无效setHelperList(列表< HelpersClass> helperList){
        HelperList = helperList;
    }
}


解决方案

序列化对象,并把它传递给活动B,同时通过意向叫它

I've an activity(A) that has a Button and when the user press it, it will add an Object to static List. I've another activity(B) that has a ListView to show the content of the static list. In the activity(B) I want to use the attribute of an object that is stored in that static list in the specific row. How can I do that? My code is shown below:

//Activity (A):
Send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
...
//  Add to mycaseList
All_Static.MyCaseList.add(case1);
...
}});

//==============================================================================

//Activity (B):

public class MyPage extends Activity {

     boolean returnvar ;
    private Activity mContext;
    Button createForm;
    Button ConfExpandRegion, Cancelb;
    String ExpandMsg, CancelMsg;
    boolean b;
    MyCaseClass mycase;
    TextView tvCase;
    AlertDialog alertDialog;

    //for list
    ListView list;

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


        // Moving to another activity
        createForm = (Button) findViewById(R.id.creat_new_formbtn);
        createForm.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent j = new Intent(MyPage.this, CreateNewForm.class);
                startActivity(j);

            }
        });

        // ============================================================================================
        // for list


            list = (ListView) findViewById(R.id.mypage_list);
            list.setClickable(true);

            final List<MyCaseClass> listOfMyCases = new ArrayList<MyCaseClass>();

            MyCasesListAdapter adapter = new MyCasesListAdapter(this, listOfMyCases, MyPage.this);

            for (MyCaseClass m : All_Static.getMyCaseList())
                adapter.add(new MyCaseClass(m));

            // after fill the adapter.. assign the list to the adapter
            list.setAdapter(adapter);


            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {


                }
            });

        // ========================================================================================

    }



    public void sendSMS(String number, String msg) throws Exception {
        if (!b) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, msg, null, null);
        }
        b = true;
    }

    // ========================================================================

    public void ShowingDialogExpand(){
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Conformation");
        alertDialog.setMessage("Are you sure you want to Expand Report Region?");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {

                   ExpandMsg = "Case_ID expand";
                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above
                            sendSMS("0000", ExpandMsg);
                            Toast.makeText(MyPage.this, "Request Sent", Toast.LENGTH_LONG)
                                    .show();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(MyPage.this, e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                   //ConfExpandRegion.setEnabled(false);
               }

            });

            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing




               }
            });

            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.show();
    }

    public void  ShowingDialogCancel(int i){
        final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
        alertDialog2.setTitle("Conformation?");
        alertDialog2.setMessage("Are you sure you want to cancel x cases?");

        String str = "ID:" + i;
        Toast.makeText(MyPage.this, str , 0)
        .show();
        alertDialog2.setButton("Yes", new DialogInterface.OnClickListener() {
               public  void onClick(DialogInterface dialog, int which) {

                     CancelMsg = "Case_ID cancel";
                     returnvar=true;

                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above
                            sendSMS("0000", CancelMsg);
                            Toast.makeText(MyPage.this, "Cancelation request sent", Toast.LENGTH_LONG)
                                    .show();

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(MyPage.this, e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                   }
            });

            alertDialog2.setButton2("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
                // Do nothing
                   returnvar = false;
                   Toast.makeText(MyPage.this, "inside No", 0)
                    .show();


               }
            });

            alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog2.show();

    }

}


//=================================================================================

// My BaseAdapter
public class MyCasesListAdapter extends BaseAdapter {
    private MyPage myPage;
    private List<MyCaseClass> listOfCases;
    private Activity parentActivity;

    // TODO test
    MyCaseClass entry;

    // TODO delete it not imp.
    public MyCasesListAdapter() {

        super();

    }

    public MyCasesListAdapter(MyPage mypage, List<MyCaseClass> listOfCaseParameter, Activity parentActivity) {
        this.myPage = mypage;
        this.listOfCases = listOfCaseParameter;
        this.parentActivity = parentActivity;
    }

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

    public Object getItem(int position) {
        return listOfCases.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup viewGroup) {

         entry = listOfCases.get(position);
         //this.getitem(position)
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) myPage
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mypage_row, null);
          }


        // this is row items..
        // Set the onClick Listener on this button
        Button ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
        Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
        TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);

        // To be a clickable button
        ConfExpandRegion.setFocusableInTouchMode(false);
        ConfExpandRegion.setFocusable(false);


        ConfExpandRegion.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                myPage.ShowingDialogExpand();
            }
        });

        // To be a clickable button
        Cancelb.setFocusableInTouchMode(false);
        Cancelb.setFocusable(false);
        Cancelb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                entry = (MyCaseClass) v.getTag();
                int caseid= entry.getID();
                myPage.ShowingDialogCancel(caseid);

                Toast.makeText(myPage, "inside calling", 0).show();

                //MyCaseClass entry = (MyCaseClass) v.getTag();
                //listOfCases.remove(entry);

                // listPhonebook.remove(view.getId());
                notifyDataSetChanged();
            }
        });

        // Set the entry, so that you can capture which item was clicked and
        // then remove it
        // As an alternative, you can use the id/position of the item to capture
        // the item
        // that was clicked.
        ConfExpandRegion.setTag(entry);
        Cancelb.setTag(entry);

        // btnRemove.setId(position);

        return convertView;
    }

    public void onClick(View view) {
        MyCaseClass entry = (MyCaseClass) view.getTag();
        listOfCases.remove(entry);
        // listPhonebook.remove(view.getId());
        notifyDataSetChanged();

    }

    private void showDialog(MyCaseClass entry) {
        // Create and show your dialog
        // Depending on the Dialogs button clicks delete it or do nothing
    }

    public void add(MyCaseClass myCaseClass) {
        // TODO Auto-generated method stub
        listOfCases.add(myCaseClass);
    }



}
//===========================================================================
// StaticClass
public class All_Static {

    public static List<MyCaseClass> MyCaseList = new ArrayList<MyCaseClass>();

    public static List<MyCaseClass> getMyCaseList() {
        return MyCaseList;
    }

    public static void setMyCaseList(List<MyCaseClass> myCaseList) {
        MyCaseList = myCaseList;
    }

    //===========================================================================
    public static List<HelpersClass> HelperList = new ArrayList<HelpersClass>();

    public static List<HelpersClass> getHelperList() {
        return HelperList;
    }

    public static void setHelperList(List<HelpersClass> helperList) {
        HelperList = helperList;
    }


}
解决方案

Serialize your Object and pass it to activity B while calling it through intent

这篇关于从列表视图获取数据与BaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:14