Android代码,用于显示带有来自数据库的图像和文本的微调器

时间:2016-08-02 05:10:52

标签: android xml

使用数据库显示带有图像和文本的微调器的Android代码我已经使用了代码但是使用这个我只能在微调器中显示文本如何从数据库中显示文本和图像。我已经浏览了很多示例,但所有示例都只通过手动显示文本和图像已存在于数组列表中。但我想要从数据库中提取这些内容,并在spinner中显示,就像我们可以显示图像和文本的列表视图一样。

adapter1=new ArrayAdapter<String>this,R.layout.spinner_layout,R.id.txt,listItems);        
   // sp.setPrompt("Select Group");
    sp.setAdapter(adapter1);

但是使用这个我只能在微调器中显示文本如何从数据库中显示文本和图像

Xml代码

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_parent_rounded_corner" >

   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="40dp"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:gravity="center_vertical" >

    <EditText
        android:id="@+id/gfnam"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
       android:singleLine="true"
        android:layout_weight="0.49"
          android:gravity="center"
       android:background="@android:color/transparent"
        android:hint="Search by Group Name..."
        android:textSize="16dp" />

    <Button
        android:id="@+id/search"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
       android:gravity="center"
       android:background="#999900cc"
       android:textColor="#fff"
        android:text="Search"
        android:textSize="12dp" />
    </LinearLayout>  

    <LinearLayout
   android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="40dp"
     android:orientation="vertical"
     > <!--     android:background="#FFFFFF"  -->  

      <LinearLayout
      android:layout_width="fill_parent"
     android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <Button
        android:id="@+id/addEvent"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="80dp"
        android:layout_height="35dp"
        android:gravity="center"
       android:background="#990975a2"
       android:textColor="#fff"
        android:text="Select Group"
        android:textSize="12dp" />      
   <Spinner
     android:id="@+id/spinner"
     android:layout_width="fill_parent"
     android:layout_height="35dp"
      android:layout_gravity="center_horizontal"
      android:layout_weight="0.50"
    android:layout_marginTop="5dp"
     android:background="@drawable/pic1_parent_rounded_corner"
     android:layout_centerInParent="true"
    android:popupBackground="@drawable/pic1_parent_rounded_corner"
   android:textSize="13dp">
  </Spinner>       
  </LinearLayout>

 <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/lType"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="20dp"
       android:layout_marginRight="20dp"
        android:textSize="13dp"
        android:hint="   Write Post(Max 35 char)"
        android:maxLength="35"
        android:background="@android:color/transparent"
        android:inputType="textMultiLine" />
    <requestFocus />

       <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/edit1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:singleLine="true"
        android:background="@android:color/transparent"
        android:textSize="13dp"
        android:hint="   URL"
        android:inputType="textUri"
        />

    <RelativeLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="230dp"
    android:layout_marginTop="3dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
       android:gravity="center"
     > <ImageView
        android:id="@+id/Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
         android:layout_marginTop="10dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:adjustViewBounds="true"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:visibility="visible" />
    </RelativeLayout>
    </LinearLayout>

  <LinearLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
     android:background="@android:color/transparent"
     >  <LinearLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_marginBottom="10dp"
    android:background="@android:color/transparent"
     > 
       <Button
            android:id="@+id/gallem"
            android:layout_width="60dp"
        android:layout_height="60dp"
        android:textSize="12dp"
            android:background="@drawable/gallerys"
             android:textColor="#ffffff"/> 

          <Button
            android:id="@+id/camer"
            android:layout_width="60dp"
        android:layout_height="60dp"
        android:textSize="12dp"
             android:textColor="#ffffff" 
            android:layout_marginLeft="5dp"                                                                                      android:background="@drawable/cammera"/> 
            <Button android:id="@+id/imgup"
             android:layout_width="60dp"
        android:layout_height="60dp"
        android:textSize="12dp"
             android:background="@drawable/post"
            android:textColor="#ffffff" 
            android:layout_marginLeft="5dp"
              />
          <Button
            android:id="@+id/imgcan"
             android:layout_width="60dp"
             android:layout_height="60dp"
             android:textSize="12dp"
            android:textColor="#ffffff" 
            android:layout_marginLeft="5dp"
             android:background="@drawable/cancel"
           />                  
       </LinearLayout>  
     </LinearLayout>
     </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

试试这个

微调器列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal">   

        <TextView
            android:id="@+id/spinnerTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView 
            android:id="@+id/spinnerImages"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>    

</LinearLayout> 

SpinnerAdapter

public class SpinnerAdapter extends ArrayAdapter<String> {

    private Context ctx;
    private String[] contentArray;
    private Integer[] imageArray;

    public SpinnerAdapter(Context context, int resource, String[] objects,
            Integer[] imageArray) {
        super(context,  R.layout.spinner_value_layout, R.id.spinnerTextView, objects);
        this.ctx = context;
        this.contentArray = objects;
        this.imageArray = imageArray;
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.spinner_value_layout, parent, false);

        TextView textView = (TextView) row.findViewById(R.id.spinnerTextView);
        textView.setText(contentArray[position]);

        ImageView imageView = (ImageView)row.findViewById(R.id.spinnerImages);
        imageView.setImageResource(imageArray[position]);

        return row;    
    }    
}

在您的活动中创建数据并调用微调器

 String[] textArray = { "option1", "option2", "option3", "option4" };
    Integer[] imageArray = { R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4};

SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_value_layout, textArray, imageArray);
        spinner.setAdapter(adapter);

答案 1 :(得分:0)

您想使用Async任务,这听起来既困难又复杂,但您真正需要做的就是:

  1. 将其定义为新的AsyncTask
  2. 调用onPrexecute并在此部分中包含微调器
  3. 使用所有图片获取/文字设置
  4. 致电doInBackground
  5. 致电finish() onPostExecute
  6. 如果所有其他变量(如良好的连接性等)都按顺序排列,这将使所有内容始终显示。 希望这能帮到你!