0

在我的应用程序中,我实现了ListView。 现在我想它设置为像这样:如果我选择特定指数,它应该保持为选定。 ans显示为ListView。 如果我选择另一个索引,那么现在新索引应该保持为选定状态。如何保持选中并突出显示的listView索引?

编辑

意味着我要设置为所选指数应继续作为选择,直到我选择另一所强调的。但不喜欢多选。

那么怎么办呢?

请帮我了点。 我已经实现了ListView像下面的代码:

phonemesListView = (ListView) findViewById(R.id.phonemsListView); 
private String[] Phonemes_List = new String[]{"P","B","T","D","K","G","N","M","ING","TH v","TH vl","F","V","S","Z","SH","CH","J","L","R rf","R b"}; 

phonemesListView.setAdapter(new ArrayAdapter<String>(this,R.layout.phonemes_list_row, R.id.phonemes,Phonemes_List)); 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view,final int Position,long id) { 
     phonemsText.setText(Phonemes_List[Position]); 
     Toast.makeText(getApplicationContext(), "Phonems: "+Phonemes_List[Position], Toast.LENGTH_SHORT).show(); 
//  view.setBackgroundColor(Color.RED); 
//  phonemesListView.setBackgroundColor(Color.BLUE); 
     jumposition = Position;    
     int temp = 0;    
     if(jumpCount == -1){     
      view.setBackgroundColor(Color.BLUE);     
      jumpCount = jumposition;     
      JumpView = view;     
      temp = 1;    
     }    
     if(temp == 0) {     
      if(jumpCount == jumposition) {      
       view.setBackgroundColor(Color.BLUE);      
       JumpView = view;     
      }     
      else{      
       JumpView.setBackgroundColor(Color.TRANSPARENT);      
       view.setBackgroundColor(Color.BLUE);      
       jumpCount = jumposition;      
       JumpView = view;     
      }    
     } 
    } 

感谢。

+0

这篇文章可以给你一些有识之士http://stackoverflow.com/questions/3825645/android-listview-with-multiple-select-and-custom-adapter并尝试listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); :) – 2012-01-17 12:43:48

+0

@SergeyBenner:请参阅更新的问题。 – 2012-01-17 12:57:26

回答

2

你可以只保存从数据持有人所使用,以填充您的ListView选择的位置。然后,您可以使用ListView的setSelection(position)属性来设置选定的位置,只要您想要保持选定状态。

+0

谢谢lalit。但我想要的是,选定的列表索引应该保持高位。 – 2012-01-17 13:08:13

+0

你可能需要实现OnScrollListener并在那里设置你的项目,但有一个说明'android listview在屏幕上不可见时回收列表中的项目“。所以任何不可见的东西都是空的。“和有关该信息http://stackoverflow.com/questions/4156217/android-listview-onscroll-question – 2012-01-17 13:14:24

+0

@Lalit:请参阅我的更新问题。 – 2012-01-17 13:19:39

0

你可以有一个在ListViewOnItemClickListener更新onItemClick选择的音素的列表,并呼吁他们adapter.notifyDataSetChanged()所以列表重新粉刷。

在适配器的getView方法,你可以更改的项目背景,如果音素中选择音素的列表。

问候。

+0

请参阅更新后的问题 – 2012-01-17 12:57:35

+0

@iDroidExplorer您正在检查模拟器还是移动设备内部。我已经检查了上面的代码没有发现错误...你能告诉我什么是确切的问题。 – 2012-01-19 10:53:15

1
First Create the List view by using the Base adapter : As Follows Create two layout files and One java file : 

    main.xml 
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#FFFFFF"> 
     <ListView 
      android:id="@+id/listviewText" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:cacheColorHint="#FFFFFF"></ListView> 
    </LinearLayout> 

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

     <TextView 
      android:id="@+id/textview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textSize="15dip" 
       android:focusable="false"   
></TextView> 

    </LinearLayout> 

    Activity code is 

    package com.pac.marico; 


import java.util.ArrayList; 



import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class ListViewColor extends Activity { 
    /** Called when the activity is first created. */ 
    ArrayList<String> arrayList; 
    Listviewlistneer listviewlistneer; 
    ListView listView; 

    int jumposition; 
    int jumpCount = -1; 
    View JumpView = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listView = (ListView)findViewById(R.id.listviewText); 
     arrayList = new ArrayList<String>(); 

     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 
     arrayList.add("ABC"); 
     arrayList.add("XYZ"); 
     arrayList.add("PQR"); 

     ListviewAdapter listviewAdapter = new ListviewAdapter(); 
     listView.setAdapter(listviewAdapter); 

     listviewlistneer = new Listviewlistneer(); 

    } 

    class ListviewAdapter extends BaseAdapter 
    { 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return arrayList.size(); 
     } 

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

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

     @Override 
     public View getView(int position, View view, ViewGroup parent) { 

      View rowView = view; 
      Viewholder viewholder = null; 


      if(rowView == null) 
      { 
       LayoutInflater layoutInflater = LayoutInflater.from(ListViewColor.this); 
       rowView = layoutInflater.inflate(R.layout.textview, null); 

       viewholder = new Viewholder(); 
       viewholder.textView = (TextView)rowView.findViewById(R.id.textview); 

       rowView.setTag(viewholder); 
      } 
      else 
      { 
       viewholder = (Viewholder)rowView.getTag(); 
      } 

      viewholder.textView.setTag(position); 
      viewholder.textView.setText(arrayList.get(position)); 


      if(jumpCount == position) 
      { 
       JumpView = rowView; 
          rowView.setBackgroundColor(Color.RED); 
        } 
      else 
      { 
       rowView.setBackgroundColor(Color.TRANSPARENT); 
        } 

      listView.setOnItemClickListener(listviewlistneer); 
      return rowView; 
     } 
    } 

    class Listviewlistneer implements OnItemClickListener 
    { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position,long id) { 

      jumposition = position; 
      int temp = 0; 

      if(jumpCount == -1) 
      { 
       view.setBackgroundColor(Color.RED); 

       jumpCount = jumposition; 
       JumpView = view; 

       temp = 1; 
      } 

      if(temp == 0) 
      { 
       if(jumpCount == jumposition) 
       { 
        view.setBackgroundColor(Color.RED); 
        JumpView = view; 
       } 
       else 
       { 
        JumpView.setBackgroundColor(Color.TRANSPARENT); 
        view.setBackgroundColor(Color.RED); 

        jumpCount = jumposition; 
        JumpView = view; 
       } 
      } 
     } 

    } 

    class Viewholder 
    { 
     TextView textView; 
    } 
} 

现在就试试吧。搜索和拉我的头发了几天之后

+0

好的,让我试试看。 – 2012-01-19 10:12:20

+0

是的,这是我想要的但仍然存在一些问题。虽然我正在查看列表视图然后有床出现。而当我选择一个,然后我可以选择另一个索引。现在有两个索引被选中。我不想要这样。 – 2012-01-19 10:47:57

+0

如果您将颜色设置为onItem clic,那么为什么我的代码无法使用?请参考它并让我知道。 – 2012-01-19 10:49:19

0

我刚刚发现activatedBackgroundIndicator也是ActionBarSherlock造型系统中可用。其中大部分需要在ICS开发和支持向后兼容性,使用ActionBarSherlock的开发者,因此使用ActionBarSherlock是最cases.So而不是使用android:background="?activatedBackgroundIndicator"将前11给android的版本错误,只要用一个很好的选择:android:background="?activatedBackgroundIndicator"

这里是例子xmle代码:

<?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="wrap_content" 
    //note the activatedBackgroundIndicator 
android:background="?activatedBackgroundIndicator" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:paddingBottom="2dip" 
android:paddingTop="2dip" > 

<TextView 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:textSize="15sp" /> 

<TextView 
    android:id="@+id/text2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:paddingRight="5dip" 
    android:textSize="20dip" /> 
    </LinearLayout> 
相关问题