gpt4 book ai didi

java - 带按钮的 Android 列表

转载 作者:行者123 更新时间:2023-11-30 01:55:56 25 4
gpt4 key购买 nike

我看过一个教程(在 youtube 上)在 Android 中创建一个带有按钮的列表,这是我的代码:

package com.example.giacomob.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Giacomo B on 05/08/2015.
*/
public class Page1 extends Activity {
// private ArrayList<String> data = new ArrayList<String>();
final int n_campi = 8; //definisco la costante del numero di campi per fermata
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);

// String dato1 = getIntent().getExtras().getString("NomeDati1");
//Intent intent = getIntent(); // Point 1
// Bundle bundle = intent.getExtras(); // Point 2
// String data1 = bundle.getString("NomeDati1"); // Point 3
String dato1 = getIntent().getExtras().getString("NomeDati1"); //preleva la stringa
Toast.makeText(this, dato1, Toast.LENGTH_SHORT).show(); //PROVA: questo mi fa comparire una specie di label notifica trasparente con il valore di "data1"
Log.d("TAG", "data1:" + dato1); //credo sia una specie di debug

// seconda stringa
String destination_nf = getIntent().getExtras().getString("Destinazione_scelta");
String destination_f = destination_nf.trim(); //tolgo gli spazi a inzio e fine
String destination = destination_f.substring(0, 1).toUpperCase() + destination_f.substring(1,destination_f.length()).toLowerCase(); //Faccio in modo che l'utente mi passi una stringa con prima maiuscola e le altre minuscole
Toast.makeText(this, destination, Toast.LENGTH_SHORT).show();



// String salve = getIntent().getExtras().getString("ciao");

int c=0, j=0, n=0;
boolean verify = false;

//FARE CONTROLLO IN CASO IL FILE XML E' VUOTO E QUINDI LA STRINGA E' VUOTA
String[] arr = dato1.split("\\|");
for (int i = 0; i < arr.length; i++) {
System.out.println(i + " => " + arr[i]);
}
final ArrayList<String> listp = new ArrayList<String>();
for (int i=3; i<arr.length-1; i=i+n_campi) {

if(arr[i].equals(destination)) {
System.out.println(i + " des " + arr[i]);
for(; j<n_campi+c; j++)
if(j==0+c) {
n++;
listp.add("Informazioni fermata numero " +n);
listp.add("Coordinata X: " + arr[j]);
}
else if(j==1+c)
listp.add("Coordinata Y: " + arr[j]);
else if (j==2+c)
listp.add("Nazione di destinazione: " + arr[j]);
else if (j==3+c)
listp.add("Paese di destinazione: " + arr[j]);
else if (j==4+c)
listp.add("Via di destinazione: " + arr[j]);
else if (j==5+c)
listp.add("Id Corriera: " + arr[j]);
else if (j==6+c)
listp.add("Paese Fermata: " + arr[j]);
else if (j==7+c) {
listp.add("Via fermata: " + arr[j]);
listp.add(" ");
}
/*switch(j)
{
case(0):
listp.add("Coordinata X" + arr[j]);
case(1):
listp.add("Coordinata Y" + arr[j]);
case(2):
listp.add("Nazione di destinazine" + arr[j]);
case(3):
listp.add("Paese di destinazione" + arr[j]);
case(4):
listp.add("Via di destinazione" + arr[j]);
case(5):
listp.add("Id Corriera" + arr[j]);
case(6):
listp.add("Paese Fermata" + arr[j]);
case(7):
listp.add("Via fermataX" + arr[j]);
}*/
// listp.add(arr[j]); // in questo modo, recupero la lista nel layout solo se è presente una corrispondenza tra paese nel file XML e in quello fornito dall'utente
verify = true;

}
else
{
j=j+n_campi;
}

c = j;
}
if(verify == false)
Toast.makeText(this,"Non sono stati trovati mezzi per " + destination, Toast.LENGTH_SHORT).show();


/* for (int i = 0; i < arr.length; ++i) {
listp.add(arr[i]);
}*/
// recupero la lista dal layout
final ListView mylist = (ListView) findViewById(R.id.listView1);

// creo e istruisco l'adattatore
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listp);


// inietto i dati
// mylist.setAdapter(adapter);

mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, adapter));


Button b_load = (Button) findViewById(R.id.button_send2);
b_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

Intent openTrack = new Intent(Page1.this, Track.class);

//String ciao = "ahahah";
//String data1 = ReadXMLFile.readXMLFile(Page1.this);
openTrack.putExtra("Destination", getIntent().getExtras().getString("NomeDati1"));
startActivity(openTrack);

}
});

}

private class MyListAdaper extends ArrayAdapter<String> {
private int layout;



public MyListAdaper(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null ){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
// viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail)
viewHolder.title = (TextView) convertView.findViewById(R.id.textList);
viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
viewHolder.button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getContext(), "Bottone cliccato" + position, Toast.LENGTH_SHORT).show();
}

});
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}

return convertView;
}

}
public class ViewHolder {
//ImageView thumbnail; //forse non serve
TextView title;
Button button;
}
}

当我在适配器中插入数组时出现的问题,在这一行:

 mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, adapter));

问题出在适配器中,事实上,如果我创建一行,例如:

private ArrayList<String> data = new ArrayList<String>();

我插入“数据”代替“适配器”,没有错误。但是当我实现代码时,我需要插入“适配器”。重要代码在底部:

  private class MyListAdaper extends ArrayAdapter<String> {
private int layout;



public MyListAdaper(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null ){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
// viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail)
viewHolder.title = (TextView) convertView.findViewById(R.id.textList);
viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
viewHolder.button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getContext(), "Bottone cliccato" + position, Toast.LENGTH_SHORT).show();
}

});
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}

return convertView;
}

}
public class ViewHolder {
//ImageView thumbnail; //forse non serve
TextView title;
Button button;
}

我该怎么办?我必须修改“适配器”或类 MyListAdapter?

这是我的名为 activity_page_1 的 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"
android:weightSum="1">

<TextView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/string_destination"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="359dp"
android:layout_below="@+id/textList"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />

<Button
android:id="@+id/list_item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:layout_weight=".50"
/>



<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/string_loadfermata"
android:id="@+id/button_send2"
android:layout_gravity="center_horizontal"
android:onClick="sendMessage"/>
</LinearLayout>

我遇到这个问题是因为首先我创建了没有按钮的列表,在我插入按钮和类“myListAdapter”之后

最后更新:

package com.example.giacomob.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Giacomo B on 05/08/2015.
*/
public class Page1 extends Activity {
// private ArrayList<String> data = new ArrayList<String>();
final int n_campi = 8; //definisco la costante del numero di campi per fermata
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);

// String dato1 = getIntent().getExtras().getString("NomeDati1");
//Intent intent = getIntent(); // Point 1
// Bundle bundle = intent.getExtras(); // Point 2
// String data1 = bundle.getString("NomeDati1"); // Point 3
String dato1 = getIntent().getExtras().getString("NomeDati1"); //preleva la stringa
Toast.makeText(this, dato1, Toast.LENGTH_SHORT).show(); //PROVA: questo mi fa comparire una specie di label notifica trasparente con il valore di "data1"
Log.d("TAG", "data1:" + dato1); //credo sia una specie di debug

// seconda stringa
String destination_nf = getIntent().getExtras().getString("Destinazione_scelta");
String destination_f = destination_nf.trim(); //tolgo gli spazi a inzio e fine
String destination = destination_f.substring(0, 1).toUpperCase() + destination_f.substring(1,destination_f.length()).toLowerCase(); //Faccio in modo che l'utente mi passi una stringa con prima maiuscola e le altre minuscole
Toast.makeText(this, destination, Toast.LENGTH_SHORT).show();



// String salve = getIntent().getExtras().getString("ciao");

int c=0, j=0, n=0;
boolean verify = false;

//FARE CONTROLLO IN CASO IL FILE XML E' VUOTO E QUINDI LA STRINGA E' VUOTA
String[] arr = dato1.split("\\|");
for (int i = 0; i < arr.length; i++) {
System.out.println(i + " => " + arr[i]);
}
final ArrayList<String> listp = new ArrayList<String>();
for (int i=3; i<arr.length-1; i=i+n_campi) {

if(arr[i].equals(destination)) {
System.out.println(i + " des " + arr[i]);
for(; j<n_campi+c; j++)
if(j==0+c) {
n++;
listp.add("Informazioni fermata numero " +n);
listp.add("Coordinata X: " + arr[j]);
}
else if(j==1+c)
listp.add("Coordinata Y: " + arr[j]);
else if (j==2+c)
listp.add("Nazione di destinazione: " + arr[j]);
else if (j==3+c)
listp.add("Paese di destinazione: " + arr[j]);
else if (j==4+c)
listp.add("Via di destinazione: " + arr[j]);
else if (j==5+c)
listp.add("Id Corriera: " + arr[j]);
else if (j==6+c)
listp.add("Paese Fermata: " + arr[j]);
else if (j==7+c) {
listp.add("Via fermata: " + arr[j]);
listp.add(" ");
}
/*switch(j)
{
case(0):
listp.add("Coordinata X" + arr[j]);
case(1):
listp.add("Coordinata Y" + arr[j]);
case(2):
listp.add("Nazione di destinazine" + arr[j]);
case(3):
listp.add("Paese di destinazione" + arr[j]);
case(4):
listp.add("Via di destinazione" + arr[j]);
case(5):
listp.add("Id Corriera" + arr[j]);
case(6):
listp.add("Paese Fermata" + arr[j]);
case(7):
listp.add("Via fermataX" + arr[j]);
}*/
// listp.add(arr[j]); // in questo modo, recupero la lista nel layout solo se è presente una corrispondenza tra paese nel file XML e in quello fornito dall'utente
verify = true;

}
else
{
j=j+n_campi;
}

c = j;
}
if(verify == false)
Toast.makeText(this,"Non sono stati trovati mezzi per " + destination, Toast.LENGTH_SHORT).show();


/* for (int i = 0; i < arr.length; ++i) {
listp.add(arr[i]);
}*/
// recupero la lista dal layout
final ListView mylist = (ListView) findViewById(R.id.listView1);

// creo e istruisco l'adattatore
// final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listp);
// final ListView a = (ListView) adapter;

// inietto i dati
// mylist.setAdapter(adapter);

mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, listp));


Button b_load = (Button) findViewById(R.id.button_send2);
b_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

Intent openTrack = new Intent(Page1.this, Track.class);

//String ciao = "ahahah";
//String data1 = ReadXMLFile.readXMLFile(Page1.this);
openTrack.putExtra("Destination", getIntent().getExtras().getString("NomeDati1"));
startActivity(openTrack);

}
});

}

private class MyListAdaper extends ArrayAdapter<String> {
private int layout;



public MyListAdaper(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null ){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
// viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.list_item_thumbnail)
//viewHolder.title = (TextView) convertView.findViewById(R.id.cazzo);
//viewHolder.button = (Button) convertView.findViewById(R.id.list_item_btn);
viewHolder.button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getContext(), "Bottone cliccato" + position, Toast.LENGTH_SHORT).show();
}

});
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.title.setText(getItem(position));
}

return convertView;
}

}
public class ViewHolder {
//ImageView thumbnail; //forse non serve
TextView title;
Button button;
}
}

最佳答案

问题是因为你的MyListAdaper只有一个构造函数如下:

public MyListAdaper(Context context, int resource, List<String> objects)

请注意,第三个参数是一个List,而不是一个ArrayAdapter。您的代码:

final ListView mylist = (ListView) findViewById(R.id.listView1);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listp);
mylist.setAdapter(new MyListAdaper(this, R.layout.activity_page1, adapter));

你看到你分配了adapter (ArrayAdapter) 到 List。所以你遇到了问题。

如果你真的想这样做,请为你的 MyListAdaper 创建一个新的构造函数,如:

public MyListAdaper(Context context, int resource, ArrayAdapter<String> objects)

虽然我觉得没必要。从您的代码来看,我认为您不需要这样做。通常将数据列表(例如 List<String> )传递给适配器就足够了。


我也意识到你有一些不会导致编译错误的问题,但可能不是你想要的,例如,你不能放:

mainViewHolder.title.setText(getItem(position));

else大括号。如果您真的想显示商品编号。对于每一行,你需要把它从if中取出来和 else .

您可能需要看一下 ListView 的更多教程,例如:

Android ListView Tutorial for beginners

Android ListView Advanced Tutorial but useful

希望这会有所帮助。

关于java - 带按钮的 Android 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32239083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com