我正在尝试在ListView
中设置我的字符串,但list.setadpter(adapter)
发出错误是否有任何方法可以做到?请帮助我
在这一行上获得错误
next.setAdapter(new ArrayAdapter(this,R.id.Sorah,surah.toCharArray()));
enter code herepackage com.example.ahsan.quranapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ahsan.quranapp.api.MyAPI;
import com.example.ahsan.quranapp.model.Ayah;
import com.example.ahsan.quranapp.model.MyResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Response;
public class Next extends AppCompatActivity{
Retrofit retrofit;
MyAPI service;
ListView next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sorah);
retrofit = new Retrofit.Builder()
.baseUrl("http://api.globalquran.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(MyAPI.class);
}
@Override
public void onClick(View v) {
Intent i = getIntent();
Intent i2 = getIntent();
String str =i2.getStringExtra("str");
//final EditText ed = (EditText) findViewById(R.id.ayah);
//final String str = ed.getText().toString().trim();
//final String str = num.toString().trim();
Call<MyResponse> call = service.getSurah(str);
call.enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
next = (ListView) findViewById(R.id.Sorah);
List<Ayah> list = new ArrayList<Ayah>(response.body().getQuran().getQuranSimple().values());
String surah = "";
for (Ayah ayah: list) {
surah += ayah.getSurah() + ":" + ayah.getAyah() + " ";
surah += ayah.getVerse() + '\n';
}
surah = surah.replace("\n", System.getProperty("line.separator"));
next.setAdapter(new ArrayAdapter<String>(this, R.id.Sorah, surah.toCharArray()));
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
TextView tv = (TextView) findViewById(R.id.verse);
tv.setText("Op Failed");
}
});
}
}
}
答案 0 :(得分:0)
以这种方式更改setAdapter - &gt;
next.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, surah.toCharArray()));
这里的问题是你使用ArrayAdapter
时必须使用android默认项目列表。