我试图在Android中创建一个简单的Activity,用户可以轻松注册我的sqlite数据库。 我想有机会选择存储在我的drawable文件夹中的某种头像,并在ImageView中设置该图像。
问题在于,当我点击“设置头像”按钮时,我只能看到没有我想要的内容的空白片段。
这是我的代码:
行模型
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_margin="16dp"
>
<ImageView
android:id="@+id/avatar_img"
android:layout_width="0dp"
android:layout_weight="0.9"
android:layout_height="50dp"
android:src="@drawable/user" />
<TextView
android:id="@+id/avatar_title"
android:text="Avatar"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="5dp"/>
RegisterActivity布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:label="Sign In"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="it.docdev.openlibrary.activities.RegisterActivity">
<!-- Login progress -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:srcCompat="@drawable/add_user" />
<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="@+id/login_form"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="@+id/user_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginBottom="16dp"
android:inputType="text"
android:maxLines="1" />
<android.support.design.widget.TextInputLayout
android:id="@+id/pass_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginBottom="16dp"
android:inputType="textPassword"
android:maxLines="1"
/>
<android.support.design.widget.TextInputLayout
android:id="@+id/repeat_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView
android:id="@+id/password2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Repeat Password"
android:inputType="textPassword"
android:maxLines="1"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/uk"
android:layout_width="75dp"
android:layout_height="83dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
app:srcCompat="@drawable/uk" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Avatar"
android:layout_toRightOf="@id/uk"
android:layout_marginTop="48dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="32dp"
/>
<Button
android:id="@+id/setavatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/name"
android:layout_marginTop="32dp"
android:layout_marginLeft="16dp"
android:text="Set Avatar"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/register"
style="?android:textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:background="@color/colorAccent"
android:text="Register"
android:layout_marginBottom="8dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
列表布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="411dp"
android:orientation="vertical"
>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</RelativeLayout>
我的适配器类
package it.docdev.openlibrary.layout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import it.docdev.openlibrary.R;
public class Adapter extends ArrayAdapter<String> {
private Context c;
private String[] avatars;
private int[] images;
private LayoutInflater inflater;
public Adapter(Context c, String[] avatars, int[] images) {
super(c, R.layout.avatarmodel, avatars);
this.c = c;
this.avatars = avatars;
this.images = images;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.avatarmodel, null);
}
TextView name = (TextView) convertView.findViewById(R.id.avatar_title);
ImageView img = (ImageView) convertView.findViewById(R.id.avatar_img);
name.setText(avatars[position]);
img.setImageResource(images[position]);
return convertView;
}
}
My Fragment Class
package it.docdev.openlibrary.layout;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import it.docdev.openlibrary.R;
public class AvatarFragment extends DialogFragment {
private ListView lv;
private String[] avatars = {"Avatar 1", "Avatar 2", "Avatar 3", "Avatar 4", "Avatar 5", "Avatar 6", "Avatar 7", "Avatar 8", "Avatar 9", "Avatar 10", "Avatar 11", "Avatar 12", "Avatar 13", "Avatar 14", "Avatar 15"};
private int[] images = {R.drawable.avatar1, R.drawable.avatar2, R.drawable.avatar3, R.drawable.avatar4, R.drawable.avatar5, R.drawable.avatar6, R.drawable.avatar7, R.drawable.avatar8, R.drawable.avatar9, R.drawable.avatar10, R.drawable.avatar11, R.drawable.avatar12, R.drawable.avatar13, R.drawable.avatar14, R.drawable.avatar15};
public View onCreateView(LayoutInflater l, ViewGroup container, Bundle savedInstaceState) {
View root = l.inflate(R.layout.list, container, false);
lv = (ListView) root.findViewById(R.id.listview1);
getDialog().setTitle("Choose Your Avatar");
Adapter a = new Adapter(getActivity(), avatars, images);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), avatars[pos], Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
请帮帮我。 谢谢