Java 在ListView行中设置数据

Java 在ListView行中设置数据,java,android,listview,adapter,Java,Android,Listview,Adapter,我的listView有个问题,我的listView每行包含两个按钮和一个textView 我有一个包含对象的arrayList 如何显示tv.setText(obj.getName())在一行中,它给我空值,而不是obj的名称 我怎样才能解决这个问题 ==================================== updating ======================================== in Activity (X) when the user press

我的listView有个问题,我的listView每行包含两个按钮和一个textView

我有一个包含对象的arrayList

如何显示
tv.setText(obj.getName())在一行中,它给我空值,而不是obj的名称

我怎样才能解决这个问题

==================================== updating ========================================

in Activity (X) when the user press Sendbutton it'll add the object to :

    Send.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {


    ......                              All_Static.MyCaseList.add(case1);
    .....                               


                                }
                            });
//======================================================================================== //适配器类

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.telephony.SmsManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyCasesListAdapter extends BaseAdapter {
    private MyPage myPage;
    static public List<MyCaseClass> listOfCases;
    Button ConfExpandRegion;
    // TODO test
    MyCaseClass entry;

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

    }

    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);

         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
         ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
        Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
        TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);
        tvCase.setText(entry.getName());
        Toast.makeText(myPage, "inside getview" + entry.getAge(), 0).show(); 
        // 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

                //For caseID testing
                /** entry = (MyCaseClass) v.getTag();
                int caseid= entry.getID();
                myPage.ShowingDialogCancel(caseid);
                Toast.makeText(myPage, "inside calling", 0).show(); **/

                MyCaseClass entry = (MyCaseClass) v.getTag();
                myPage.ShowingDialogCancel(entry); 


                }

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


        });

        // 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
        //For Sending SMS with cancel Request
        //For Notification -1-

    }

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


}
import java.util.List;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.telephony.smsmsmanager;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MyCasesListAdapter扩展了BaseAdapter{
私人网页;
静态公开病例列表;
纽扣区;
//TODO测试
MyCaseClass条目;
公共MyCasesListAdapter(MyPage MyPage,列表CaseParameter){
this.myPage=myPage;
this.listOfCases=listofcase参数;
}
public int getCount(){
返回listOfCases.size();
}
公共对象getItem(int位置){
返回cases.get列表(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组视图组){
entry=listOfCases.get(位置);
if(convertView==null){
LayoutInflater充气器=(LayoutInflater)myPage.getSystemService(Context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.mypage_行,空);
}
//这是行项目。。
//在此按钮上设置onClick侦听器
ConfExpandRegion=(按钮)convertView.findViewById(R.id.expand);
Button Cancelb=(Button)convertView.findViewById(R.id.cancelCase);
TextView tvCase=(TextView)convertView.findViewById(R.id.mypage_name);
tvCase.setText(entry.getName());
Toast.makeText(myPage,“inside getview”+entry.getAge(),0.show();
//成为一个可点击的按钮
ConfExpandRegion.setFocusableInTouchMode(假);
ConfExpandRegion.setFocusable(假);
ConfExpandRegion.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
myPage.ShowingDialogExpand();
}
});
//成为一个可点击的按钮
Cancelb.setFocusableInTouchMode(假);
Cancelb.setFocusable(假);
Cancelb.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
//用于caseID测试
/**entry=(MyCaseClass)v.getTag();
int caseid=entry.getID();
myPage.ShowingDialogCancel(caseid);
makeText(myPage,“内部调用”,0.show()**/
MyCaseClass条目=(MyCaseClass)v.getTag();
myPage.ShowingDialogCancel(条目);
}
//remove(view.getId());
});
//设置条目,以便可以捕获单击的项目并
//然后移除它
//或者,您可以使用项目的id/位置来捕获
//项目
//点击了。
ConfExpandRegion.setTag(条目);
取消b.setTag(条目);
//btnRemove.setId(位置);
返回视图;
}
公共void onClick(视图){
MyCaseClass条目=(MyCaseClass)view.getTag();
案例列表。删除(条目);
//remove(view.getId());
notifyDataSetChanged();
}
私有void showDialog(MyCaseClass条目){
//创建并显示对话框
//根据对话框按钮,单击“删除”或“不执行任何操作”
//用于发送带有取消请求的SMS
//供通知-1-
}
公共无效添加(MyCaseClass MyCaseClass){
//TODO自动生成的方法存根
添加(myCaseClass);
}
}
//=======================================================================================

// All_Static Class
import java.util.ArrayList;
import java.util.List;

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;
    }


}
//显示listView的我的活动

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyPage extends Activity {


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

    //for list
    ListView list;
    MyCasesListAdapter adapter;

    @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>();

            adapter = new MyCasesListAdapter(this, listOfMyCases);

            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) {
                  // here you can add functions
                  // Sending a Message to server that the plaintiff wants to expand report region
                   // Getting Case_ID + putting it inside ExpandlMsg
                   ExpandMsg = "Case_ID expand";
                   if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above 
                            sendSMS("+9000", 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(final MyCaseClass entry){
        final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
        alertDialog2.setTitle("Conformation?");
        alertDialog2.setMessage("Are you sure you want to cancel x cases?");

        alertDialog2.setButton("Yes", new DialogInterface.OnClickListener() {
               public  void onClick(DialogInterface dialog, int which) {

                   // Sending a Message to server that the plaintiff found the case
                  // For Sending SMS with cancel Request
                  // Getting Case_ID + putting it inside CancelMsg
                     CancelMsg = "Case_ID cancel";
                     MyCasesListAdapter.listOfCases.remove(entry);
                     adapter.notifyDataSetChanged();
                     All_Static.MyCaseList.clear();
                     Toast.makeText(MyPage.this, "id:"+ entry.getID(), 0)
                        .show();

                if (!b) {
                        try {
                            // Should write server number here + the chatting must be pushed above 
                            sendSMS("+9000", 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 

                   Toast.makeText(MyPage.this, "inside No", 0)
                    .show();

               }
            });


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

    }

}
import java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.app.ListActivity;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MyPage扩展活动{
按钮创建窗体;
按钮打开区域,取消B;
字符串ExpandMsg,CancelMsg;
布尔b;
mycase类mycase;
TextView-tvCase;
警报对话框警报对话框;
//名单
列表视图列表;
mycaseslistatadapter适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mypage);
//转到另一项活动
createForm=(按钮)findViewById(R.id.create\u new\u formbtn);
createForm.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Intent j=newintent(MyPage.this,CreateNewForm.class);
星触觉(j);
}
});
// ============================================================================================
//名单
L