在方法执行contactsList
之前是空的readContacts()
,换句话说,contactsView.setAdapter(adapter)
执行时,它contactsList
是空的,那么为什么这段代码仍能正确显示联系人的信息?
public class MainActivity extends AppCompatActivity {
ListView contactsView;
ArrayAdapter<String> adapter;
List<String> contactsList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView) findViewById(R.id.contacts_list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList);
contactsView.setAdapter(adapter);
readContacts();
}
private void readContacts() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
));
String number = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
));
contactsList.add(displayName + …
Run Code Online (Sandbox Code Playgroud) android android-listview android-adapter notifydatasetchanged
例如,我有一个Person
模型及其序列化程序
class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
sex = models.IntegerField()
phone = models.CharField(max_length=255)
class SimplePersonSerializer(serializer.ModelSerializer):
class Meta:
model = Person
fields = ('first_name', 'last_name')
Run Code Online (Sandbox Code Playgroud)
然后在我的视图函数中,我可以:
@api_view(['GET'])
def people(request):
people = Person.objects.all()
data = SimplePersonSerializer(people, many=True).data
return Response(data)
Run Code Online (Sandbox Code Playgroud)
但是,当我使用 对其进行分析时django-debug-toolbar
,它显示序列化程序要求SQL Server
选择Person
模型的所有字段,尽管我只需要first_name
和last_name
。
我知道我可以改变people = Person.objects.all()
,以people = Person.objects.all().only('first_name', 'last_name')
使其。但我想知道我是否可以在序列化程序中执行此操作。
我想使用 javascript 制作一个 3d 时间选择器(带透视图),如下所示(如 iOS 的闹钟)。
我想在单击按钮时触发滚动动画。
我找到了很多时间选择器,但似乎它们只能在人为触摸时滚动,这不是我想要的。
如果我使用 CSS 动画rotateX
,则无法更改每个项目在旋转时的不透明度。
所以似乎必须自己写动画,但我不知道如何。
我怎么能做到?
我想用右边的按钮创建一个带有margin_right的操作栏.像这样
我使用动作按钮来完成它.但按钮在右边没有margin_right.
android:layout_marginRight在这里不起作用.
这是我的styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarSize">50dp</item>
<item name="android:actionBarStyle">@style/my_actionbar_style</item>
<item name="android:actionButtonStyle">@style/my_action_button</item>
</style>
<style name="my_actionbar_style" parent="android:Widget.ActionBar">
<item name="android:background">@color/actionbar_backgroud</item>
<item name="android:titleTextStyle">@style/myTitleStyle</item>
<item name="android:displayOptions">showTitle</item>
</style>
<style name="myTitleStyle">
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">20sp</item>
</style>
<style name="my_action_button">
<item name="android:background">@drawable/button_selector</item>
<item name="android:width">70dp</item>
<item name="android:textSize">13sp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginRight">10dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是我的menu.xml:
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="AppCompatResource">
<item
android:id="@+id/action_search"
android:title="submit"
android:showAsAction="always" /></menu>
Run Code Online (Sandbox Code Playgroud) 例如,我有一个博客基础上Django
,我已经有几个功能,为用户login
:edit_profile
, ,share
.
但现在我需要实施一个任务系统.
10 score
每天奖励20 score
30 score
我不想将奖励代码与普通功能代码混合使用.所以我决定使用message queue
.伪代码可能如下所示:
@login_required
def edit_profile(request):
user = request.user
nickname = ...
desc = ...
user.save(...)
action.send(sender='edit_profile', payload={'user_id': user.id})
return Response(...)
Run Code Online (Sandbox Code Playgroud)
奖励可以订阅此操作
@receiver('edit_profile')
def edit_profile_reward(payload):
user_id = payload['user_id']
user = User.objects.get(id=user_id)
mission, created = Mission.objects.get_or_create(user=user, type='complete_profile')
if created:
user.score += 20
user.save()
Run Code Online (Sandbox Code Playgroud)
但我不知道这是不是正确的方法.如果是这样,我message queue
应该使用什么?django-channel
/ django-q
或其他什么?如果没有,最佳做法是什么?