android中使用arrayadapter类的自定义列表视图

当我单击列表行项目然后在行imageview中显示图像时,如何使用像Android中的iphone这样的刻度线选择行项目?


if(getItem(position)!=null){

img.setvisibilty(View.Visible);}        

else{System.out.println("imagenull");}

我使用此功能,但图像仅显示在最后一行。请帮助我如何使用刻度线图像选择项目。


public class DistanceArrayAdapter extends ArrayAdapter<Constant>{

public static String category,state,miles;

public ImageView img;

private Context context;

private int current = -1;

ArrayList<Constant> dataObject;


public DistanceArrayAdapter(Context context, int textViewResourceId,

ArrayList<Constant> dataObject) {

super(context, textViewResourceId, dataObject);

this.context=context;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

View rowView=convertView;

if(rowView==null){

LayoutInflater inflater = (LayoutInflater) context

            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


rowView = inflater.inflate(R.layout.category_row, parent, false);


}

    //TextView textView = (TextView) rowView.findViewById(R.id.text1);

TextView textView1 = (TextView) rowView.findViewById(R.id.text2);

    //textView.setText(""+getItem(position).id);

textView1.setText(""+getItem(position).caption);

img=(ImageView)rowView.findViewById(R.id.img);

img.setVisibility(View.GONE);

if(position%2==1)

{

rowView.setBackgroundResource(R.color.even_list);

}

else

    {

        rowView.setBackgroundResource(R.color.odd_list);

    }

rowView.setOnClickListener(new OnClickListener(){


        @Override

        public void onClick(View v) {


            if(img.getVisibility()==View.GONE)

            {

                img.setVisibility(View.VISIBLE);

                System.out.println("1");

            }

if(img.getVisibility()==View.VISIBLE){

img.setVisibility(View.GONE);

System.out.println("12");

}


miles=getItem(position).caption;

System.out.println("miles"+miles);

}

});

return rowView;

}

}


POPMUISE
浏览 928回答 3
3回答

桃花长相依

在ListView上设置选择模式&nbsp; &nbsp; //if using ListActivity or ListFragment&nbsp;getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);&nbsp; &nbsp; //or&nbsp; &nbsp; &nbsp;myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);&nbsp; &nbsp; //myListView is reference to your ListView

千万里不及你

我使用数组适配器为具有复选框的列表创建了一个示例示例。在Android中使用ArrayAdatpter使用CheckBox实现ListView您也可以找到示例代码。这将被输出这是我的代码片段我有一个具有列表视图的活动public class MainActivity extends Activity {&nbsp;private ListView mainListView = null;&nbsp;private Planet[] planets = null;&nbsp;private ArrayAdapter<Planet> listAdapter = null;&nbsp;@Override&nbsp;public void onCreate(Bundle savedInstanceState) {&nbsp; super.onCreate(savedInstanceState);&nbsp; setContentView(R.layout.main);&nbsp; mainListView = (ListView) findViewById(R.id.mainListView);&nbsp; // When item is tapped, toggle checked properties of CheckBox and&nbsp; // Planet.&nbsp; mainListView&nbsp; .setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp;@Override&nbsp; &nbsp;public void onItemClick(AdapterView<?> parent, View item,&nbsp; &nbsp; int position, long id) {&nbsp; &nbsp;Planet planet = listAdapter.getItem(position);&nbsp; &nbsp;planet.toggleChecked();&nbsp; &nbsp;PlanetViewHolder viewHolder = (PlanetViewHolder) item&nbsp; &nbsp; .getTag();&nbsp; &nbsp;viewHolder.getCheckBox().setChecked(planet.isChecked());&nbsp; &nbsp;}&nbsp; });&nbsp; // Create and populate planets.&nbsp; planets = (Planet[]) getLastNonConfigurationInstance();&nbsp; if (planets == null) {&nbsp; &nbsp;planets = new Planet[] { new Planet("Mercury"),&nbsp; &nbsp; &nbsp;new Planet("Venus"), new Planet("Earth"),&nbsp; &nbsp; &nbsp;new Planet("Mars"), new Planet("Jupiter"),&nbsp; &nbsp; &nbsp;new Planet("Saturn"), new Planet("Uranus"),&nbsp; &nbsp; &nbsp;new Planet("Neptune"), new Planet("Ceres"),&nbsp; &nbsp; &nbsp;new Planet("Pluto"), new Planet("Haumea"),&nbsp; &nbsp; &nbsp;new Planet("Makemake"), new Planet("Eris") };&nbsp; }&nbsp; ArrayList<Planet> planetList = new ArrayList<Planet>();&nbsp; planetList.addAll(Arrays.asList(planets));&nbsp; // Set our custom array adapter as the ListView's adapter.&nbsp; listAdapter = new PlanetArrayAdapter(this, planetList);&nbsp; mainListView.setAdapter(listAdapter);&nbsp;}&nbsp;public Object onRetainNonConfigurationInstance() {&nbsp; return planets;&nbsp;}}这是我正在处理复选框内容的“自定义阵列适配器”public class PlanetArrayAdapter extends ArrayAdapter<Planet> {&nbsp;private LayoutInflater inflater;&nbsp;public PlanetArrayAdapter(Context context, List<Planet> planetList) {&nbsp;super(context, R.layout.simplerow, R.id.rowTextView, planetList);&nbsp;//Cache the LayoutInflate to avoid asking for a new one each time.&nbsp; inflater = LayoutInflater.from(context);&nbsp;}&nbsp;@Override&nbsp;public View getView(int position, View convertView, ViewGroup parent){&nbsp; Planet planet = (Planet) this.getItem(position);&nbsp; CheckBox checkBox;&nbsp; TextView textView;&nbsp; // Create a new row view&nbsp; if (convertView == null) {&nbsp; convertView = inflater.inflate(R.layout.simplerow, null);&nbsp; textView = (TextView) convertView.findViewById(R.id.rowTextView);&nbsp; checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);&nbsp; convertView.setTag(new PlanetViewHolder(textView, checkBox));&nbsp; // If CheckBox is toggled, update the planet it is tagged with.&nbsp; checkBox.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp;public void onClick(View v) {&nbsp; &nbsp; CheckBox cb = (CheckBox) v;&nbsp; &nbsp; Planet planet = (Planet) cb.getTag();&nbsp; &nbsp; planet.setChecked(cb.isChecked());&nbsp; &nbsp;}&nbsp; });&nbsp; }&nbsp; // Re-use existing row view&nbsp; else {&nbsp; PlanetViewHolder viewHolder = (PlanetViewHolder) convertView&nbsp; &nbsp; &nbsp;.getTag();&nbsp; &nbsp;checkBox = viewHolder.getCheckBox();&nbsp; &nbsp;textView = viewHolder.getTextView();&nbsp; }&nbsp; checkBox.setTag(planet);&nbsp; // Display planet data&nbsp; checkBox.setChecked(planet.isChecked());&nbsp; textView.setText(planet.getName());&nbsp; return convertView;&nbsp;}}请!让我知道您是否有任何麻烦。
打开App,查看更多内容
随时随地看视频慕课网APP