ListVew对话框中的问题 [英] Problem in Dialog With ListVIew
问题描述
这是我的Dialog With ListView的代码,在这里我也保持状态,当用户点击列表时,背景将填满绿色,如下图所示。
但是我的问题在下面
1>我想增加我的行高,怎么办?
而我也想把图像放在所有的行旁边这是可能的吗?
这是我所有这些东西的代码。
package com.android.listselector;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
导入android.widget.AdapterView.OnItemClickListener;
public class ListSelector extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList< String>列表;
private Context mContext = ListSelector.this;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// setContentView(R.layout.selected_example);
String [] items = {lorem,ipsum,dolor,sit,amet,
consectetuer,adipiscing,elit,morbi vel,ligula,
vitae,arcu,aliquet,mollis,etiam,vel,erat,
placerat ,port itor,s,p ent
//填充模型 - 一个简单的列表
list = new ArrayList< String>(); (int i = 0; i< items.length; i ++){
list.add(items [i]);
}
//创建我们的SelectedAdapter
selectedAdapter = new SelectedAdapter(this,0,list);
selectedAdapter.setNotifyOnChange(true);
对话框对话框= new Dialog(mContext);
dialog.setContentView(R.layout.selected_example);
dialog.setTitle(自定义对话框);
ListView listview =(ListView)dialog.findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
dialog.show();
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0,View view,
int position,long id ){
//用户单击列表项,使其选择
selectedAdapter.setSelectedPosition(position);
}
});
}
//向上移动事件处理程序
//向下移动事件处理程序
//将所选项目up移动到ViewList。
private void moveUp(){
int selectedPos = selectedAdapter.getSelectedPosition();
if(selectedPos> 0){
String str = list.remove(selectedPos);
list.add(selectedPos - 1,str);
//设置适配器中的选定位置
selectedAdapter.setSelectedPosition(selectedPos - 1);
}
}
//在ViewList中移动所选项目down。
private void moveDown(){
int selectedPos = selectedAdapter.getSelectedPosition();
if(selectedPos< list.size() - 1){
String str = list.remove(selectedPos);
list.add(selectedPos + 1,str);
//设置适配器中的选定位置
selectedAdapter.setSelectedPosition(selectedPos + 1);
}
}
public class SelectedAdapter extends ArrayAdapter< String> {
//用于在ListView中保留所选位置
private int selectedPos = -1; //未命名的初始化值
public SelectedAdapter(Context context,int textViewResourceId,
List< String> objects){
super(context,textViewResourceId,objects);
}
public void setSelectedPosition(int pos){
selectedPos = pos;
//通知此更改的视图
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
查看v = convertView;
//只有在空值为
时才会夸张视图if(v == null){
LayoutInflater vi =(LayoutInflater)this.getContext()
.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row,null);
}
//获取文本视图
TextView label =(TextView)v.findViewById(R.id.txtExample);
//根据选择的状态更改行颜色
if(selectedPos == position){
label.setBackgroundColor(Color.GREEN);
} else {
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/ *
* //使用除.toString()之外的其他东西MyClass myobj =
*(MyClass)this.getItem(position);
* label.setText(myobj.myReturnsString());
* /
return(v);
}
}
}
,这里是我的代码中使用的布局
<?xml version =1.0encoding = UTF-8\" >?;
< LinearLayout
xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =fill_parent
android:orientation =vertical>
< ListView
android:id =@ + id / listExample
android:layout_width =fill_parent
android:layout_height = fill_parent
android:background =#CCCCCC
android:choiceMode =singleChoice
/>
< LinearLayout
android:orientation =horizontal
android:layout_width =wrap_content
android:layout_height =wrap_content
android: layout_gravity =bottom
>
< Button
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_weight =1
android:id =@ + id / btnMoveUp
/>
< Button
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_weight =1
android: id =@ + id / btnMoveDown
/>
< / LinearLayout>
p>
<?xml version =1.0encoding =utf-8?>
< TextView xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parentandroid:layout_height =wrap_content
android:id =@ + id / txtExampleandroid:textSize =18spandroid:textColor =#000000
android:background =#FF0000>
< / TextView>
是的,我得到解决方案在这里我所有的答案
首先我必须通过这个
$ pre>
修改selected_row xml文件xml version =1.0encoding =utf-8?>
< RelativeLayout
xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =wrap_contentandroid:layout_height =100dip >
< TextView android:layout_width =fill_parent
android:layout_height =40pxandroid:id =@ + id / txtExample
android:textSize =18spandroid:textColor =#000000
android:gravity =center_vertical
/>
< ImageView android:layout_width =wrap_content
android:layout_height =40pxandroid:id =@ + id / imgbeside
android:scaleType =centerandroid:layout_centerVertical =true
android:layout_alignParentRight =trueandroid:src =@ drawable / selector
android:layout_marginRight =10dip/>
以及我的java文件我把以下内容放在这行下面
TextView label =(TextView)v.findViewById(R.id.txtExample);
ImageView mImageview =(ImageView)v.findViewById(R.id.imgbeside);
这将为我工作。谢谢所有的回复。
Here is the my code for the Dialog With ListView and here i also maintain the state when the user click on list it background will be fill with green as you can see in my image below
But problem that i have is below
1>I want to increase my row height .How can do?
2>and i also want to put image beside in all row is this possible?
Here is my code for all this stuff.
package com.android.listselector;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ListSelector extends Activity {
private SelectedAdapter selectedAdapter;
private ArrayList<String> list;
private Context mContext = ListSelector.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.selected_example);
String[] items = { "lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula",
"vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat",
"placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus" };
// populate the model - a simple a list
list = new ArrayList<String>();
for (int i = 0; i < items.length; i++) {
list.add(items[i]);
}
// create our SelectedAdapter
selectedAdapter = new SelectedAdapter(this, 0, list);
selectedAdapter.setNotifyOnChange(true);
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.selected_example);
dialog.setTitle("Custom Dialog");
ListView listview = (ListView) dialog.findViewById(R.id.listExample);
listview.setAdapter(selectedAdapter);
dialog.show();
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// user clicked a list item, make it "selected"
selectedAdapter.setSelectedPosition(position);
}
});
}
// move up event handler
// move down event handler
// Move selected item "up" in the ViewList.
private void moveUp() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos > 0) {
String str = list.remove(selectedPos);
list.add(selectedPos - 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos - 1);
}
}
// Move selected item "down" in the ViewList.
private void moveDown() {
int selectedPos = selectedAdapter.getSelectedPosition();
if (selectedPos < list.size() - 1) {
String str = list.remove(selectedPos);
list.add(selectedPos + 1, str);
// set selected position in the adapter
selectedAdapter.setSelectedPosition(selectedPos + 1);
}
}
public class SelectedAdapter extends ArrayAdapter<String> {
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SelectedAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
}
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition() {
return selectedPos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// only inflate the view if it's null
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.selected_row, null);
}
// get text view
TextView label = (TextView) v.findViewById(R.id.txtExample);
// change the row color based on selected state
if (selectedPos == position) {
label.setBackgroundColor(Color.GREEN);
} else {
label.setBackgroundColor(Color.WHITE);
}
label.setText(this.getItem(position).toString());
/*
* // to use something other than .toString() MyClass myobj =
* (MyClass)this.getItem(position);
* label.setText(myobj.myReturnsString());
*/
return (v);
}
}
}
and here are the layout used in my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnMoveUp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btnMoveDown"
/>
</LinearLayout>
here is other one
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/txtExample" android:textSize="18sp" android:textColor="#000000"
android:background="#FF0000">
</TextView>
Yes i get solution by here all my answer
first i have to modify selected_row xml file by this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="100dip">
<TextView android:layout_width="fill_parent"
android:layout_height="40px" android:id="@+id/txtExample"
android:textSize="18sp" android:textColor="#000000"
android:gravity="center_vertical"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="40px" android:id="@+id/imgbeside"
android:scaleType="center" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="@drawable/selector"
android:layout_marginRight="10dip" />
and also in my java file i put the following thing below this line
TextView label = (TextView) v.findViewById(R.id.txtExample);
ImageView mImageview = (ImageView) v.findViewById(R.id.imgbeside);
this will work for me.Ok Thanks For all reponse.
这篇关于ListVew对话框中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!