我是android新手。 我在网上搜索并找到了如何做一个简单的数组适配器,它将在列表视图中显示设备上保存的所有联系人。 代码运行良好,直到我被要求在后台执行。
当我更新代码以在AsyncTask中执行此操作时,列表视图的项目将添加一个defalut白色作为背景颜色,您可以在单击该项目时看到行中写入的内容。
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个项目,但白色作为背景,单击的项目显示项目行内的文本。
Image showing 6 items of list view
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>
答案 0 :(得分:0)
您需要为列表视图使用自定义适配器。这就是你如何设置列表视图的样式。您还可以为名称等其他信息添加更多textview。并为每个元素分配不同的颜色。
快速解决方案检查以下问题并回答有关自定义适配器的问题: Custom Adapter for List View
祝你好运。