Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用for循环在固定视图中打印字符串_Java_Android_For Loop_If Statement - Fatal编程技术网

Java 如何使用for循环在固定视图中打印字符串

Java 如何使用for循环在固定视图中打印字符串,java,android,for-loop,if-statement,Java,Android,For Loop,If Statement,我知道这个问题听起来可能很奇怪(请随意编辑标题),但我想基本上做到以下几点: 假设我有一个包含4个字符串的列表: ArrayList<String> carList= new ArrayList<>; carList.add("BMW"); carList.add("GMC"); carList.add("KIA"); carList.add("Honda"); ArrayList carList=新的ArrayList; carList.add(“宝马”);

我知道这个问题听起来可能很奇怪(请随意编辑标题),但我想基本上做到以下几点:

假设我有一个包含4个字符串的列表:

ArrayList<String> carList= new ArrayList<>;
 carList.add("BMW");
 carList.add("GMC");
 carList.add("KIA");
 carList.add("Honda");
ArrayList carList=新的ArrayList;
carList.add(“宝马”);
carList.添加(“GMC”);
carList.添加(“KIA”);
卡利斯特加上(“本田”);
现在我想将列表中的3个项目打印到3个文本视图中,第四个项目由于某种原因将被排除,并且其位置已知

int excludedIndex = 2; //for example.

for (int i = 0; i < carList.size(); i++) {
      if (i != excludedIndex) {
        textView_1.setText(carList.get(i));  // here it will put (BMW) in tv1
        textView_2.setText(carList.get(??)); // here it should put (GMC) in tv2
        textView_3.setText(carList.get(??)); // here it should put (Honda) in tv3
       }
    }
int excludedIndex=2//例如
对于(int i=0;i
基本上,您是在问:如何将列表中的元素(由其索引标识)映射到某个文本字段。这句话已经包含了一个可能的解决方案——使用
Map
,它知道哪个文本视图应该用于哪个索引

例如:

Map<Integer, TextView> viewsByIndex = ...
for (int i = ... ) {
  if (viewsByIndex.containsKey(i)) {
     viewsByIndex.get(i).setText(listItem(i));
Map viewsByIndex=。。。
for(int i=…){
if(视图索引容器(i)){
viewsByIndex.get(i).setText(列表项(i));

上面的代码不是编译/检查的-它只是一个灵感/伪代码,展示了如何以优雅的方式解决这个问题。

您不必使用for循环

ArrayList<String> carList= new ArrayList<>();
carList.add("BMW");
carList.add("GMC");
carList.add("KIA");
carList.add("Honda");


 textView_1.setText(carList.get(0));  // here it will put (BMW) in tv1
 textView_2.setText(carList.get(1)); // here it should put (GMC) in tv2
 textView_3.setText(carList.get(3)); // here it should put (Honda) in tv3
ArrayList carList=new ArrayList();
carList.add(“宝马”);
carList.添加(“GMC”);
carList.添加(“KIA”);
卡利斯特加上(“本田”);
textView_1.setText(carList.get(0));//这里它将把(BMW)放在tv1中
textView_2.setText(carList.get(1));//这里应该将(GMC)放在tv2中
textView_3.setText(carList.get(3));//在这里它应该把(本田)放在tv3中

java不是javascript。您正在设置索引,而不是列表中的元素。您不需要在那里设置循环。只需textView_1.setText(carList.get(0));doWhy不是recyclerview吗?在设置adapter@Stultuske很抱歉。这只是一个示例,但事实上列表大小未知,
excludedIndex
不是固定的,无法更改。@AlaaAbuZarifa在这种情况下,您需要循环,但工作方式保持不变。顺便说一句,它不是固定的,就是可以更改的。说“它不是固定的,也不能更改”赚得很少sense@Stultuske是的,我的意思是它不是固定的。这只是一个例子,但事实上列表大小未知,
excludedIndex
也不是固定的,无法更改。因此需要一个循环。因此,您必须使用recycler视图在recycler视图行中使用TextView。将数组传递给recycler视图适配器&为排除设置一个if-else条件。这会起到一定的作用。但在我的例子中,这是不够的,因为若
excludedIndex
的textview是空的,因为它的索引和某个textview的键匹配。@AlaaAbuZarifa除了GhostCat提供的解决方案之外,您所需要的只是一个标志,表明您将需要ad一旦标志为true,d 1将立即添加到索引中,并且当您使用该标志时,应将其设置为trueexcludedIndex@AlaaAbuZarifa我想你没有得到我的解决方案。你不必排除任何东西。因为你可以问地图是否知道索引。如果地图不知道索引,那么它会自动排除!哦,我现在知道了。不管怎样我用recyclerview解决它。但我想你的速度更好。