我在一个活动上有两个列表视图,一切正常,但如果我想从listview 1和listview 2获取点击的项目,我只能从listview 1中获取项目
activity_smokes:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollojt"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
>
<ListView
android:id="@+id/listView1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_weight=".5">
</ListView>
<ListView
android:layout_width="0px"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_weight=".50"
android:layout_marginTop="100dp"
/>
</LinearLayout>
</ScrollView>
抽烟:
CustomAdapter adapter, adapterCT;
public smokes CustomListView = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
public ArrayList<ListModel> CustomListViewValuesArrCT = new ArrayList<ListModel>();
String[] Difficulties = {"Easy", "Medium", "Hard", "Very Hard"};
String Map;
String value;
private enum Maps {
Cobblestone, Chache, Dust, Inferno, Mirage, Overpass, Train;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smokes);
Map = getIntent().getStringExtra("Map");
Maps abc= Maps.valueOf(Map);
if(Map.equals("Dust")){
getSupportActionBar().setTitle("Dust 2");
} else {
getSupportActionBar().setTitle(Map);
}
CustomListView = this;
switch(Map) {
case Cobblestone:
setListData()
}
Resources res =getResources();
ListView listCT = (ListView)findViewById(R.id.listView);
adapterCT=new CustomAdapter(CustomListView, CustomListViewValuesArrCT,res);
listCT.setAdapter( adapterCT);
listCT.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListModel tempValuesCT = (ListModel) CustomListViewValuesArrCT.get(position);
Toast.makeText(smokes.this,
"" + tempValuesCT.getSmokeName(),
Toast.LENGTH_LONG)
.show();
}
});
ListView list= ( ListView )findViewById( R.id.listView1 ); // List defined in XML ( See Below )
/**************** Create Custom Adapter *********/
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListModel tempValues = (ListModel) CustomListViewValuesArr.get(position);
Toast.makeText(smokes.this,
"" + tempValues.getSmokeName(),
Toast.LENGTH_LONG)
.show();
}
});
}
public void setListData()
{
ListModel lmT = new ListModel("Banana to CT Cross", "i0", Difficulties[0]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Banana to Coils","i1",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Second Mid to Long","i3",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Second Mid to Short","i4",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
ListModel lmCT = new ListModel("A Pit to Short", "i2", Difficulties[1]);
CustomListViewValuesArrCT.add(lmCT);
lmCT = new ListModel("A Long to Mid", "i5", Difficulties[1]);
CustomListViewValuesArrCT.add(lmCT);
}
/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
static class CustomAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(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(LAYOUT_INFLATER_SERVICE);
}
/******** 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 text;
public TextView text1;
public ImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.imageView);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
holder.text1.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.text.setText( tempValues.getSmokeName() );
holder.text1.setText( tempValues.getDifficulty() );
}
return vi;
}
}
}
修改 我更新了代码,它现在正在为我工作!
答案 0 :(得分:0)
原因是您使用不同的数据集来填充列表视图,但在#import random module
import random
#main function
def main():
#program message
print("Rock, Paper, Scissors Game")
#initializing variables that would hold choices of user and computer
comp = 1
user = 1
while comp == user:
print("Enter your choice in range from 1 to 3")
#prompt user to enter choice
user = int(input("Your choice: "))
#randomly assign choice to computer
comp = random.randint(1,3)
#display choice of computer
print("Computer Choice : ",comp)
#display game drawn message, when same choices
if (comp == user):
print("Game Drawn. Select again")
#calling function to decide winner
winner (comp, user)
#winner function
def winner(comp, user):
#rock and scissor choice
if(comp == 1 and user ==3):
print("Computer win")
print("The rock smashes scissor")
elif(comp == 3 and user ==1):
print("User win")
print("The rock smashes scissor")
else:
#paper and rock choice
if(comp == 1 and user == 2):
print("User win")
print("The paper wraps rock")
elif (comp == 2 and user ==1):
print("Computer win")
print("Scissors cut paper")
elif (comp == 2 and user == 3):
print("User win")
print("Scissors cut paper")
else:
print("Invalid selection")
#calling main function
main()
中,您只从一个数据集中获取数据,尽管您从任何列表视图中选择了一个项目。
First List使用“CustomListViewValuesArrCT”数据集
onItemClick
第二个列表使用“CustomListViewValuesArr”数据集
ListView listCT = (ListView)findViewById(R.id.listView);
adapterCT=new CustomAdapter(CustomListView, CustomListViewValuesArrCT,res);
listCT.setAdapter( adapterCT);
<强> BUT 仅在列表项选择中从“CustomListViewValuesArr”获取数据。
ListView list= ( ListView )findViewById( R.id.listView1 );
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter(adapter);
您需要修复流程,以便在单击列表项后,从该数据集中获取数据,使用该数据集为该特定列表设置适配器。