1

Android開発は初めてです。

XML に比べて JSON キャリアの軽さを考慮して、単純なアプリケーションで JSON オブジェクトと配列を使用することを純粋に好みます。

ListView を設定するために ArrayAdapter に課題がありました。

これが私が克服する方法であり、それに関するあなたの提案が必要です。

Extend the Adaptor class.

次に、JSONArray をコンストラクターに渡します。
ここで、コンストラクターは、JSONArray の長さを設定するダミーの String 配列を使用して super を呼び出します。
コンストラクターの引数をクラスに格納して、後で使用できるようにします。

public myAdaptor(Context context, int resource, JSONArray array)
{
    super(context, resource, new String[array.length()]);
    // Store in the local varialbles to the adapter class.
    this.context = context;
    this.resource = resource;
    this.profiles = objects;
}

getView ()は、JSONArray から JSONObjects を取得してビューを構築するジョブを実行します。

public View getView(int position, View convertView, ViewGroup parent)
{
    View view;
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, parent, false);
    }
    else
    {
        view = convertView;
    }

    // Here 
    JSONObject item = (JSONObject) profiles.getJSONObject(position);

    // READY WITH JSONObject from the JSONArray
    // YOUR CODE TO BUILD VIEW OR ACCESS THE 
}

現在、改善/提案/思慮深い質問はありますか??

4

4 に答える 4

3

文字列配列を作成して getCount メソッドを実装する代わりに、null を super に渡すことができます。

public myAdaptor(Context context, int resource, JSONArray array)
{
    super(context, resource, null);
    // Store in the local varialbles to the adapter class.
    this.context = context;
    this.resource = resource;
    this.profiles = array;
}

public int getCount(){
   return profiles.length();
}
于 2013-12-09T09:37:29.090 に答える
0

This is the listview adapter class.

public class Adapter extends BaseAdapter {
    Context context = null;
    ArrayList<OffersAvailable> offers = null;


    public Adapter(Context context, ArrayList<OffersAvailable> offer) {
        this.context = context;
        this.offers = offer;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return offers.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return offers.get(position).getTitle();
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View v;
        final TextView tvpoints;
        final TextView tv,tv_quantity;
        if (convertView == null) {
            LayoutInflater li = ((Activity) context).getLayoutInflater();
            v = li.inflate(R.layout.design_userlist, null);

        } else {
            v = convertView;
        }
        tvpoints = (TextView) v.findViewById(R.id.tvpointlist);
        tv_quantity= (TextView) v.findViewById(R.id.tv_quantity);
        tv = (TextView) v.findViewById(R.id.tvdatalist);
        ((Activity) context).runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                tv.setText(offers.get(position).getTitle().toUpperCase());
                tv_quantity.setText(offers.get(position).getQuatity().toUpperCase());
                tvpoints.setText(offers.get(position).getPoint() + "");
            }
        });

        return v;
    }

}

Object class

public class OffersAvailable {
    String title, point, quatity, description,nid;

    public String getNid() {
        return nid;
    }

    public void setNid(String nid) {
        this.nid = nid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public String getQuatity() {
        return quatity;
    }

    public void setQuatity(String quatity) {
        this.quatity = quatity;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

Use the json in the main class and store it inthe Arraylist of type OffersAvailable.

and pass it to the listviews adapter. if you are getting the response from the internet use asynchttpclient method.And parse the json.

于 2013-12-09T09:31:28.593 に答える
0

1 つのテキストビューを作成し、item.getString("key") で割り当て、その文字列をローカル文字列配列に追加して、そのビューを返します

于 2013-12-09T09:29:31.053 に答える