0

I am trying to set my string in ListView but list.setadpter(adapter) is giving an error is there any way to do it??Please help me Getting error on this line 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");
            }
        });
    }
}
}
Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
Ahsan Raza
  • 485
  • 5
  • 10
  • set the Adapter and find the ListView inside onCreate . You can add data to it later. – OneCricketeer Jun 04 '16 at 21:22
  • Also, you have an `Ayah` class, so extend an `ArrayAdapter` in your own class to make a [custom adapter layout](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – OneCricketeer Jun 04 '16 at 21:26

1 Answers1

0

Change the setAdapter like this way ->

next.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, surah.toCharArray()));

The problem here you have to use android default item list when you use ArrayAdapter.

Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
  • Still getting error can't resolve constructor(Arrayadapter annoy namus reterogit2.callback,myclass package name,int,char[]) – Ahsan Raza Jun 04 '16 at 19:18
  • You can see Surah is my string variable but setadapter gives me error of JAVA.lang,string something like this how can I add this string variable in LIstView?? – Ahsan Raza Jun 04 '16 at 19:20
  • declare the `next = (ListView) findViewById(R.id.Sorah);` in your activity , not in onClick – Zahidul Islam Jun 04 '16 at 19:20
  • I think the problem with `this` which should be a context . use `Next.this` instad of `this` as a first parameter of the `ArrayAdapter` – Zahidul Islam Jun 04 '16 at 19:25
  • Let me make it more simple I want to show String value on screen which is very large as you Know Surah of Quran that so why I am not using textView but is there any way that user can slide textView up or down to read the Full text???? – Ahsan Raza Jun 04 '16 at 19:27
  • I don't see any problem in your code. Log the size of surah.toCharArray().length , Also can you please post the `sorah` layout ? – Zahidul Islam Jun 04 '16 at 19:35
  • this is my Sorah layout. – Ahsan Raza Jun 04 '16 at 19:37
  • Layout is ok, Check the value of `surah.toCharArray().length` – Zahidul Islam Jun 04 '16 at 19:44
  • This would show one character on each lineitem... Is that really the expected behavior that is wanted? – OneCricketeer Jun 04 '16 at 21:24