如果我们连续有大量的textview,我们如何在自定义列表视图中单击该textview时更改textview的文本?

时间:2014-02-11 12:47:23

标签: android android-listview textview android-custom-view

附件是我的应用程序的屏幕截图的链接和显示我想要实现的功能的视频的链接。基本上我想要的是,当我点击任何功能区上的“更多”按钮时,它应该扩展自己和用从JSON文件中获取的数据替换文本。 功能区的高度应根据必须显示的文本量进行调整。 每个功能区都有自己的自定义文本,该文本是从JSON文件中提取的。

目前正在发生的事情是,当我点击一个功能区上的“更多”按钮时,它会在其自己的功能区中显示文本。其他丝带随机.Ribbon,意思是彩色背景。

详细信息,每个“更多”按钮都是一个文本视图,每个功能区都是唯一的。每个功能区都有自己的“更多”文本视图。 Iam目前为每个文本视图“更多”设置了两次ID。首先,它们是在XML文件中的主布局中预定义的,第二次是动态,当我在列表视图中为主布局充气时,我为其分配了一个唯一的ID每个“更多”。 但由于某些原因,在动态设置id后,单击“更多”时,代码有时会从R.java中获取主布局“更多”的预定义ID,有时它会获取我在运行时设置的动态设置ID。 setId()方法。

Link to show the functionality i want on "More" Click

Link to show the screenshot of my app activity(view)

这是我的列表适配器代码

    public class CustomListAdapter  extends BaseAdapter implements OnClickListener{
  ListView lv;
 public Activity activity;
   private ArrayList data;
    private static LayoutInflater inflater=null;
     public Resources res;
    List_Model tempValues=null;
    public Context context;
    ViewHolder holder;
    public String furl;
    ViewGroup vig;
    View vi;


       public CustomListAdapter(Activity activity, ArrayList data, Resources res) {
    super();

    this.activity = activity;
    context=activity.getApplicationContext();
    this.data = data;
    this.res = res;
      inflater = ( LayoutInflater)activity.
              getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       }

   @Override
   public int getCount() {
    // TODO Auto-generated method stub
    if(data.size()<=0)

        return 1;

    return data.size();
    }

   @Override
   public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
   }

   @Override
   public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
    }
       public static class ViewHolder{

       public TextView Text_shopName,Text_distance,Text_date1,Text_date2,Text_website;
     public TextView     Text_phoneNo,Text_Address,Text_Info,TextDirection,Text_more,Text_Like,Text_Bookmark,Text_Remind;
       public LinearLayout LayoutBase ,like_layLayout,layoutmore;;
       public WebView web,shareweb;
        public ListView lv;


        public ImageView image;
        public ImageButton flike_btn;

         public String dest_latitude,dest_longitude;

       }

   @Override
    public View getView(int position, View convertView, ViewGroup Parent) {
    // TODO Auto-generated method stub
      vi = convertView;
     vig=Parent;


           holder=new ViewHolder();
          if(vi==null)
         {


        vi=inflater.inflate(R.layout.raw_items,null);
        holder.lv=(ListView) vi.findViewById(R.id.listView1);

        holder.Text_more=(TextView) vi.findViewById(R.id.TextView_More);


          vi.setTag(holder);

         }
        else
      `enter code here` {
         holder=(ViewHolder) vi.getTag();


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

 Toast.makeText(activity, "No data", Toast.LENGTH_LONG).show();
     }
     else
     {
        tempValues=null;
        tempValues=(List_Model) data.get(position);


        **holder.Text_more.setId(position);**

      Log.i("TAg","holder id"+holder.Text_more.getId());
        //holder.Text_more.setTag(position);


        }

     holder.Text_more.setOnClickListener(new OnItemClickListener(position));

     return vi;



}
 private class OnItemClickListener  implements android.view.View.OnClickListener{           
     public int mPosition;
     private View vieww;

     OnItemClickListener(int position){
          mPosition = position;

         // view =vi;
          //Log.v("CustomAdapter","i am in onitem"+mPosition);
     }

    @Override
    public void onClick(View v) {
        //Log.v("CustomAdapter","i am in onclick");
           tempValues=(List_Model) data.get(mPosition);
        // TODO Auto-generated method stub
        /*switch(v.getId())
        {
           case R.id.TextView_more:

                        holder.Text_more.setText(tempValues.getMoreInfo()));

                      break;


        }*/

        if(v.getId()==mPosition)
        {
            TextView morrr=    (TextView) v.findViewById(mPosition);
            morrr.setText(tempValues.getMoreInfo());
            Log.v("textview","pos="+mPosition+tempValues.getMoreInfo());

        }
        else
        {
            Toast.makeText(activity, "text not added"+v.getId(), Toast.LENGTH_SHORT
                    ).show();
        }

    }




     }

2 个答案:

答案 0 :(得分:1)

我得到了上述查询的解决方案。它对我有用..  这是我的代码示例..

适配器类:

public class CustomListAdapter  extends BaseAdapter implements OnClickListener
  {
 public Activity activity;
  private ArrayList<JSONObject>data;

public Context context;  
  public String furl;
  LayoutInflater inflater=null;

 public CustomListAdapter(Activity activity, ArrayList<JSONObject>data) {
  super();

  this.activity = activity;
  context=activity.getApplicationContext();
  this.data = data;  
   inflater = ( LayoutInflater)activity.
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

 @Override
    public int getCount()
    {
     return data.size();
      }
  @Override
 public long getItemId(int position)
  {  
  return position;
  }


@Override
public Object getItem(int position) 
{
    return data.get(position);
}



       public static class ViewHolder
       {

        public TextView txtShort;
          public TextView txtLong;       
         public TextView txtMore;        
      }

   @Override
  public View getView(int position, View convertView, ViewGroup Parent) 
    {   
 View view=convertView;
 if (view==null) {
     convertView= inflater.inflate(R.layout.layout_row, null);
}

  final TextView txtShort=(TextView) convertView.findViewById(R.id.text_main);
  final TextView txtLong=(TextView) convertView.findViewById(R.id.text_detail);
  final TextView txtMore=(TextView) convertView.findViewById(R.id.text_more);


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

   Toast.makeText(activity, "No data", Toast.LENGTH_LONG).show();
   }
  else
 {
   try{
      final JSONObject jsonObject=data.get(position);
      txtShort.setText(jsonObject.getString("name"));
       if (jsonObject.getBoolean("isExpand")==true)
       {
           txtLong.setVisibility(View.VISIBLE); 
           txtShort.setVisibility(View.GONE);   
      }else
      {

          txtLong.setVisibility(View.GONE); 
         txtShort.setVisibility(View.VISIBLE);  
      }

       txtMore.setOnClickListener(new View.OnClickListener() 
       {

        @Override
        public void onClick(View v) 
        {

            try {

                if (jsonObject.getBoolean("isExpand")==true) 
                {
                    txtLong.setVisibility(View.GONE);   
                    txtShort.setVisibility(View.VISIBLE);
                    jsonObject.put("isExpand",false);
                }else
                {                           
                    txtLong.setVisibility(View.VISIBLE);    
                      txtShort.setVisibility(View.GONE);
                      jsonObject.put("isExpand",true);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
   }catch(Exception ex)
   {

   }
  }

List_Main_Activity类:

public class MainActivity extends Activity {

ListView listView=null;
ArrayList<JSONObject> arrayListjsonObj=new ArrayList<JSONObject>();
CustomListAdapter adapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView=(ListView)findViewById(R.id.listView1);
    insertjsonObject();
    adapter=new CustomListAdapter(MainActivity.this, arrayListjsonObj);
    listView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    return true;
}



public void insertjsonObject()
{
    arrayListjsonObj.clear();
    for (int i = 0; i < 50; i++) {
    try {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("name", "name "+i);
        jsonObject.put("address", "address "+i);
        jsonObject.put("isExpand", false);
        arrayListjsonObj.add(jsonObject);
    } catch (Exception e) 
    {

    }

}   

答案 1 :(得分:0)

仅尝试..将setId添加到更多...(textview)ViewHolder在CustomListAdapter中的ur arraylist size例如 - ArrayList data = 10 in for loop setId to textview more ... as 1001 and the below group as 2001 ---在ClickListener中获取ur mPosition的id。如果你的位置是3 - 那么你的id必须是3001,加上1000。然后settextby你的代码