我是安卓新手。我在网上搜索并找到了如何做一个简单的数组适配器,它将在列表视图中显示保存在您设备上的所有联系人。代码运行良好,直到我被要求在后台执行。
当我在 AsyncTask 中更新代码以执行此操作时,列表视图的项目添加了默认的白色作为背景色,当您单击该项目时,您可以看到行中写入的内容。
public class MainActivity extends Activity {
ListView lv;
ArrayAdapter<String> adapter;
public String num1;
ArrayList<String> arr = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
progress=(ProgressBar) findViewById(R.id.progressBar1);
new background().execute();
public class background extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
displayContacts();
return "success";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "Connection successfull "+result, Toast.LENGTH_SHORT).show();
String [] values=arr.toArray(new String[arr.size()]);
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
registerForContextMenu(lv);
progress.setVisibility(View.GONE);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
String num= adapter.getItem(aInfo.position);
this.num1=num;
menu.setHeaderTitle("Options for " + num);
menu.add(1, 1, 1, "Send ");
menu.add(1, 2, 2, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
// Implements our logic
// Toast.makeText(this, "Item id ["+itemId+"]",
// Toast.LENGTH_SHORT).show();
if (itemId == 1) {
Toast.makeText(this, "number is:"+this.num1, Toast.LENGTH_LONG).show();
}
return true;
}
private void displayContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_LONG).show();
arr.add(phoneNo);
}
pCur.close();
}
}
}
}
displayContacts() 会将所有联系人添加到 ArrayList arr 中,用于在列表视图中显示。
我搜索了更改列表视图项目的文本颜色并做到了,但文本颜色仍然没有变化。我只能更改列表项行的背景颜色。
这是一张图片,显示了如何在列表视图中创建 6 个项目,但背景为白色,单击的项目显示项目行内的文本。
activity_main.xml 代码是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>