我的 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);
}
}
//================================================= ========================================
// 显示 listView 的我的 Activity
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();
}
}
//================================================= =======================================
// 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;
}
}