私は ListView にデータを表示しています。私はString[]に変換した2 つの異なるArrayListからデータを取得しています。それらをリストすることはできますが、ListView の onClick をクリックした要素を ListView から削除し、notifyDataSetChanged()を使用しているので、ListView は更新されます新しい要素ですが、ここで ListView をクリックすると要素がキックされませんが、残りますが、AlertDialog ボックスを開くと更新され、要素が自動的に削除されます。次のコードを使用しています。
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class Incoming_call extends Activity {
Button phonebook,log,manuall;
Intent intents;
ListView incoming_list;
ArrayList<String> array ;
ArrayList<String> mobileNumbers;
String name ,editext;
ArrayList<String> array_ed ;
ArrayAdapter<String> ad;
ArrayAdapter<String> adapter;
public static final int PICK_CONTACT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.incoming);
phonebook = (Button)findViewById(R.id.phonebook1);
log = (Button)findViewById(R.id.call_log1);
manuall = (Button)findViewById(R.id.manually1);
incoming_list = (ListView)findViewById(R.id.list1);
array = new ArrayList<String>();
phonebook.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intents = new Intent(Intent.ACTION_PICK);
intents.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intents, PICK_CONTACT);
}
});
log.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Incoming_call.this,ListView2Activity.class);
startActivity(i);
}
});
manuall.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Incoming_call.this);
builder.setCancelable(true);
builder.setTitle("My Title");
builder.setInverseBackgroundForced(true);
builder.setIcon(R.drawable.ic_launcher);
final EditText ed = new EditText(Incoming_call.this);
ed.setInputType(InputType.TYPE_CLASS_PHONE);
builder.setView(ed);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Log.d("log",ed.getText().toString());
array.add(ed.getText().toString());
ad = new ArrayAdapter<String>(Incoming_call.this,android.R.layout.simple_list_item_1,array);
incoming_list.setAdapter(ad);
ad.notifyDataSetChanged();
dialog.dismiss();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
incoming_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder adb=new AlertDialog.Builder(Incoming_call.this);
adb.setMessage("Selected Item is = "+incoming_list.getItemAtPosition(arg2));
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
array.remove(arg2);
}
});
adb.show(); }
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("log", "requestCode="+requestCode+", resultCode="+resultCode+", data="+data);
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK)
{
switch(requestCode)
{
case PICK_CONTACT:
{
Log.d("log","pick");
Cursor cursor = managedQuery(data.getData(), null, null, null, null);
cursor.moveToNext();
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Log.d("log","name = "+name );
cursor.close();
ArrayList<String> homeNumbers = new ArrayList<String>();
mobileNumbers = new ArrayList<String>();
ArrayList<String> workNumbers = new ArrayList<String>();
Log.d("log","array");
ContentResolver cr = getContentResolver();
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
Log.d("log","cursor phone");
if(phones != null && phones.moveToFirst()) {
Log.d("log","if");
while (phones.isAfterLast() == false) {
Log.d("log","while");
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
Log.d("log","home");
// array.add("Name is :" +name + "Number :"+ number);
homeNumbers.add(number);
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
Log.d("log","mobile");
Toast.makeText(this, "Contect LIST = "+name +number, Toast.LENGTH_LONG).show();
// array.add("Name is :" +name + "Number :"+ number);
mobileNumbers.add(number);
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
// array.add("Name is :" +name + "Number :"+ number);
Log.d("log","work");
workNumbers.add(number);
break;
} //switch end
phones.moveToNext();
phones.close();
} // while end
} // if end
Log.d("lmt", "Name :"+name + " Number : " + mobileNumbers);
if(mobileNumbers.equals(null) || mobileNumbers.equals(""))
{
Toast.makeText(getApplicationContext(), "Dont have mobile Numbers", Toast.LENGTH_LONG).show();
}
else
{
array.add(name + " " +mobileNumbers);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
incoming_list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(Incoming_call.this, name +mobileNumbers, Toast.LENGTH_LONG).show();
} // case end
} // switch end
} // if end
} // onActivityResult End
} // All End