2016-12-07 52 views
1

我有一个TextViewRatingBarListView,并且我想要在ListView的每个列表上保存RatingBar的值,所以可以使用自定义适配器?Android - 如何处理自定义适配器中的ratingBar?

我用做喜欢的代码:

PertanyaanAdapter.java

package flix.yudi.pertanyaan3; 

import android.app.Activity; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.RatingBar; 
import android.widget.TextView; 

import java.util.List; 

class PertanyaanAdapter extends ArrayAdapter<Pertanyaan> { 

    private AppCompatActivity activity; 
    private List<Pertanyaan> movieList; 

    PertanyaanAdapter(AppCompatActivity context, int resource, List<Pertanyaan> objects) { 
     super(context, resource, objects); 
     this.activity = context; 
     this.movieList = objects; 
    } 

    @Override 
    public Pertanyaan getItem(int position) { 
     return movieList.get(position); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.item_listview, parent, false); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
      //holder.ratingBar.getTag(position); 
     } 

     holder.ratingBar.setOnRatingBarChangeListener(onRatingChangedListener(position)); 

     holder.ratingBar.setTag(position); 
     holder.ratingBar.setRating(getItem(position).getRatingStar()); 
     holder.movieName.setText(getItem(position).getAsk()); 

     return convertView; 
    } 

    private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final int position) { 
     return new RatingBar.OnRatingBarChangeListener() { 
      @Override 
      public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { 
       Pertanyaan item = getItem(position); 
       assert item != null; 
       item.setRatingStar(v); 
       Log.i("Adapter", "star: " + v); 
      } 
     }; 
    } 

    private static class ViewHolder { 
     private RatingBar ratingBar; 
     private TextView movieName; 

     ViewHolder(View view) { 
      ratingBar = (RatingBar) view.findViewById(R.id.rate_img); 
      movieName = (TextView) view.findViewById(R.id.text); 
     } 
    } 
} 

Pertanyaan.java

package flix.yudi.pertanyaan3; 
public class Pertanyaan { 

    private float ratingStar; 
    private String ask; 

    Pertanyaan(int ratingStar, String ask) { 
     this.ratingStar = ratingStar; 
     this.ask = ask; 
    } 

    float getRatingStar() { 
     return 0; 
    } 

    void setRatingStar(float ratingStar) { 
     this.ratingStar = ratingStar; 
    } 

    public String getAsk() { 
     return ask; 
    } 

} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    ListView listView; 
    ArrayList<Pertanyaan> listPertanyaan; 
    ArrayAdapter<Pertanyaan> adapter2; 
    ProgressDialog pDialog; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView)findViewById(R.id.list_view); 
     listPertanyaan = new ArrayList<>(); 
     getpertanyaan get= new getpertanyaan(); 
     get.execute(); 
     adapter2 = new PertanyaanAdapter(this, R.layout.item_listview, listPertanyaan); 
     listView.setAdapter(adapter2); 
    } 

    protected void onStart() { 
     super.onStart(); 

    } 

    private class getpertanyaan extends AsyncTask<Void, Void, Integer> { 
     ArrayList<Pertanyaan> list; 
     protected void onPreExecute() { 
      pDialog=new ProgressDialog(MainActivity.this); 
      pDialog.setTitle("Nama Dosen"); 
      pDialog.setMessage("Menampilkan nama dosen... Mohon tunggu...!"); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
      super.onPreExecute(); 
      list = new ArrayList<>(); 
     } 

     @Override 
     protected Integer doInBackground(Void... params) { 
      InputStream is = null; 
      String result = ""; 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://flix.16mb.com/send_data.php"); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       // Get our response as a String. 
       is = entity.getContent(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      //convert response to string 
      try { 
       BufferedReader reader = null; 
       if (is != null) { 
        reader = new BufferedReader(new InputStreamReader(is, "utf-8")); 
       } 
       String line; 
       if (reader != null) { 
        while ((line = reader.readLine()) != null) { 
         result += line; 
        } 
       } 
       if (is != null) { 
        is.close(); 
       } 
       //result=sb.toString(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      // parse json data 
      try { 
       JSONArray jArray = new JSONArray(result); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject jsonObject = jArray.getJSONObject(i); 
        list.add(new Pertanyaan(0,jsonObject.getString("ask"))); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     protected void onPostExecute(Integer result) { 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      listPertanyaan.addAll(list); 
      adapter2.notifyDataSetChanged(); 
     } 
    } 
} 

但是当我运行应用程序时,RatingBar是碰不得的,只是每个ListView名单是触摸,但是我想挖掘RatingBar

我做错了什么?请帮帮我。

编辑: item_listview.xml

<RatingBar 
     android:id="@+id/rate_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text" 
     android:numStars="5" 
     android:stepSize="1" 
     style="@style/Widget.AppCompat.RatingBar.Indicator" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" /> 
+0

'浮动getRatingStar()'不应该可能返回0 – Blackbelt

+0

@Blackbelt还是不行,先生,我已经改变了我的回归0到回报ratingStar,有没有另外一种方法? – dondo

+0

你是什么意思*,但当我运行应用程序时,RatingBar是不可触摸的* – Blackbelt

回答

2

RatingBar是只读的,因为你正在使用的风格。

style="@style/Widget.AppCompat.RatingBar.Indicator"

属性android:isIndicator设置为true,这使得它的行为,在路上遇到。您可以摆脱线,或试图迫使android:isIndicator为false您item_listview.xml

+0

这是工作先生,谢谢!但另一个问题是允许先生? – dondo

+0

当然。拍摄 – Blackbelt

+0

当用户点击按钮时,有一种方法可以在'ListView'的每个列表中保存'RatingBar'的值?我实际上想要将'RatingBar'的所有值都发送到'MySQL'中,但我认为这将会很难 – dondo