在Android的“自定义适配器"中设置Textview的文本时出现问题 [英] Issue in setting Textview's text in Custom Adapter for Android

查看:67
本文介绍了在Android的“自定义适配器"中设置Textview的文本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在自定义适配器的getView()方法内设置textview的setText属性.

I am not able to set textview's setText property inside getView() method of Custom Adapter.

我已经尝试过以下解决方案,但是它对我不起作用:

I already tried below solutions but it does not work for me :

  • Solution One
  • Solution Two
  • Solution Three

Listview.xml文件:

Listview.xml file :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imgLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:divider="@null"
    android:dividerHeight="5dp"
    android:scrollbars="none" />

</RelativeLayout>

下面是我的keep_resing xml文件:

Below is my keep_resing xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customRowLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/Btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/thumb_down_disabled" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/resing"
            android:textColor="#357EC7"
            android:textSize="10sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lyricsTxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

        <RelativeLayout
            android:id="@+id/parentLayout"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:background="@drawable/playing_view"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/helloTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="5dp"
                android:singleLine="true"
                android:text="Hello"
                android:textColor="#357EC7"
                android:textSize="14sp" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
                android:background="@drawable/volume" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/keepBtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/thumb_up_disabled" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/keep"
            android:textColor="#357EC7"
            android:textSize="10sp" />
    </LinearLayout>
</RelativeLayout>

下面是我的适配器代码:

Below is my adapter code :

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.stream.Position;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.PowerManager;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class KeepReSingAdapter extends BaseAdapter implements OnClickListener {

    /*********** Declare Used Variables *********/
    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;
    public Resources res;
    SongCue tempValues = null;
    int i = 0;

    private ArrayList<SongCue> songCue;
    private int cueIndex = 0;
    private String selectedSongId;
    private MediaPlayer mMediaPlayer;
    ViewHolder holder;
    View vi;
    boolean right_button_flag, left_button_flag;
    SessionManager session;

    // int left_button_flag;

    /************* CustomAdapter Constructor *****************/
    public KeepReSingAdapter(Activity a, ArrayList d, Resources resLocal) {

        /********** Take passed values **********/
        activity = a;
        data = d;
        res = resLocal;

        /*********** Layout inflator to call external xml layout () ***********/
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        selectedSongId = SessionManager.getInstance(
                activity.getApplicationContext()).getString(
                AppConstants.SONG_ID);

        right_button_flag = false;
        left_button_flag = false;

        // Session class instance
        session = new SessionManager(activity.getApplicationContext());

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder {

        public TextView lyricsTxt, helloTxt;
        // public TextView text1;
        public ImageButton leftBtn, rightBtn;
        public RelativeLayout parentLayout;
        public LinearLayout rowLayout;

        // public TextView textWide;
        // ImageView image;

    }

    /****** Depends upon data size called for each row , Create each ListView row *****/
    public View getView(final int position, View convertView, ViewGroup parent) {

        vi = convertView;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.keep_resing, null);

            holder = new ViewHolder();
            holder.lyricsTxt = (TextView) vi.findViewById(R.id.lyricsTxt);
            holder.helloTxt = (TextView) vi.findViewById(R.id.helloTxt);

            holder.leftBtn = (ImageButton) vi.findViewById(R.id.reSingBtn);
            holder.rightBtn = (ImageButton) vi.findViewById(R.id.keepBtn);
            holder.parentLayout = (RelativeLayout) vi
                    .findViewById(R.id.parentLayout);
            holder.rowLayout = (LinearLayout) vi
                    .findViewById(R.id.customRowLayout);





            vi.setTag(holder);

        } else {
            holder = (ViewHolder) vi.getTag();

        }

        if (data.size() <= 0) {



        } else {
            /***** Get each Model object from Arraylist ********/
            tempValues = null;
            tempValues = (SongCue) data.get(position);


            holder.lyricsTxt.setText(tempValues.getLyric());

            List<Song> AllSong = SplashScreen_Activity.songs;
            if (selectedSongId != null) {
                for (Song tempsong : AllSong) {
                    if (tempsong.songId.equals(selectedSongId)) {
                        songCue = (ArrayList<SongCue>) tempsong.songCues.songCue;
                    }
                }
            } else {

            }




            holder.parentLayout.setOnClickListener(new View.OnClickListener() {

               @Override
               public void onClick(View v) {

                Toast.makeText(activity.getApplicationContext(),
                        " " + position, Toast.LENGTH_SHORT).show();

                     holder.helloTxt.setText("Test");

                    Toast.makeText(activity.getApplicationContext(),
                            holder.helloTxt.getText().toString(),
                            Toast.LENGTH_LONG).show();


                   }
               });



        }
        vi.setOnClickListener(new OnItemClickListener(position));
        return vi;
    }


    @Override
    public void onClick(View v) {
        Log.v("CustomAdapter", "=====Row button clicked=====");

    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener implements OnClickListener {
        private int mPosition;

        OnItemClickListener(int position) {
            mPosition = position;
        }

        @Override
        public void onClick(View arg0) {


        }
    }

}

问题是,我无法将文本视图文本更改为测试".

Problem is, I am not able to change the text view text to "Test".

注意:如果我通过执行"holder.helloTxt.getText().toString()"在Toast中看到值,则显示为" 测试 ".

Note : If I see the value in Toast by doing "holder.helloTxt.getText().toString()" it shows me "Test".

我不知道为什么会出现此问题.我们将不胜感激任何帮助.

I don't know why this problem is arising.Any help will be highly appreciated.

谢谢

推荐答案

最后,我得到了问题的解决方案,并猜测这是代码中的次要更新.

Finally I got the solution to my question and guess what it was a minor updation in the code.

我将代码更新为:

final ViewHolder holder; 

CustomAdaptergetView()内部

而不是全局声明.

inside getView() of CustomAdapter instead of declaring it globally.

再次感谢大家的回答.

它帮助我详细研究了代码.

It helped me to look into my code in detail.

这篇关于在Android的“自定义适配器"中设置Textview的文本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆