2016-06-29 4 views
0

두 개의 클래스, 사용자의 새 인스턴스를 만드는 Person 클래스 및 Stopwatch의 새 인스턴스를 만드는 Stopwatch 클래스가 있습니다 (Button, Button, TextView 변수가 있음). , 사람).BaseAdapter 클래스의 새 인스턴스를 만들 수 없습니다.

내 주요 기능에는 두 개의 텍스트 상자, 단추 및 목록보기가 있습니다. 내가하고 싶은 것은 EditText의 성과 이름을 입력 한 다음 저장을 클릭하는 것입니다. 저장되면 해당 데이터를 내 개인의 특정 인스턴스에 저장하려고합니다. 그런 다음 그 사람을 사용하여 스톱워치 인스턴스 내의 Button 텍스트를 업데이트합니다. 각 스톱워치 인스턴스에는 서로 다른 사람 인스턴스가 할당되어 각 스톱워치의 이름이 달라야합니다. 현재 저장 버튼에 대한 내 onclick 수신기는 아무 것도하지 않고 있으며 이유를 알지 못합니다. 지금 나는 스톱워치에 사람을 한 명 지정할 수있을 때까지 getCount를 하나에 하드 코드했습니다. 이 바보 같은 질문, 내가 자바에 새로 온 경우

죄송합니다

주요 기능 :

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final EditText firstName = (EditText)findViewById(R.id.FirstName); 
    final EditText lastName = (EditText)findViewById(R.id.LastName); 
    ListView listView = (ListView)findViewById(R.id.listView); 
    final Button save = (Button)findViewById(R.id.SaveUser); 


    BaseAdapter myAdapter = new BaseAdapter() { 
     @Override 
     public int getCount() { 
      return 1; 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @SuppressLint("ViewHolder") 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.stopwatchview,parent,false); 

      final TextView Screen = (TextView) convertView.findViewById(R.id.Clock_View); 
      final Button start = (Button) convertView.findViewById(R.id.button_start); 
      Button reset = (Button) convertView.findViewById(R.id.button_reset); 
      final Person person = new Person(); 
      final Stopwatch stopwatch = new Stopwatch(Screen,start,reset,person);; 
      Button save = (Button) findViewById(R.id.SaveUser); 
      final EditText first = (EditText) findViewById(R.id.FirstName); 
      final EditText last = (EditText) findViewById(R.id.LastName); 

      assert save != null; 
      save.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        person.setName(first.getText().toString(), last.getText().toString()); 
       } 
      }); 


      start.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (stopwatch.isTicking()) { 
         stopwatch.stop(); 
        } else { 
         stopwatch.start(); 
        } 
       } 
      }); 

      reset.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        stopwatch.reset(); 
       } 
      }); 
      return convertView; 
     } 
    }; 
    listView.setAdapter(myAdapter); 
} 
} 

스톱워치 클래스 :

import android.graphics.Color; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.widget.Button; 
import android.widget.TextView; 

public class Stopwatch { 
    TextView text; 
    Button start; 
    Button reset; 
    Person person; 


public Stopwatch(TextView t, Button Start, Button Reset, Person p){ 
    text = t; 
    start = Start; 
    reset = Reset; 
    person = p; 

} 

long starttime = 0L; 
long timeInMilliseconds = 0L; 
long timeSwapBuff = 0L; 
long updatedtime = 0L; 
int t = 1; 
int secs = 0; 
int mins = 0; 
int milliseconds = 0; 
Handler handler = new Handler(); 
Runnable callback; 

public void start(){ 
    callback = updateTimer(); 
    start.setText("pause"); 
    starttime = SystemClock.uptimeMillis(); 
    handler.postDelayed(callback, 0); 
    t = 0; 
} 

public void stop(){ 
    text.setTextColor(Color.BLUE); 
    start.setText(person.firstName); 
    timeSwapBuff += timeInMilliseconds; 
    handler.removeCallbacks(callback); 
    t = 1; 
} 

public void reset(){ 
    stop(); 
    starttime = 0L; 
    timeInMilliseconds = 0L; 
    timeSwapBuff = 0L; 
    updatedtime = 0L; 
    t = 1; 
    secs = 0; 
    mins = 0; 
    milliseconds = 0; 
    if(t==0){ 
     handler.removeCallbacks(callback); 
    } 
    text.setText("00:00:00"); 
} 

public boolean isTicking(){ 
    if(t==0){ 
     return true; 
    } 
    return false; 
} 

//Update Timer 
private Runnable updateTimer() { 
    return new Runnable() { 
     public void run() { 
      timeInMilliseconds = SystemClock.uptimeMillis() - starttime; 
      updatedtime = timeSwapBuff + timeInMilliseconds; 
      secs = (int) (updatedtime/1000); 
      mins = secs/60; 
      secs = secs % 60; 
      milliseconds = (int) (updatedtime % 1000); 
      text.setText("" + mins + ":" + String.format("%02d", secs) + ":" 
        + String.format("%03d", milliseconds)); 
      text.setTextColor(Color.RED); 
      handler.postDelayed(this, 0); 
     } 
    }; 
}; 

} 

사람 클래스 :

public class Person { 
String firstName; 
String lastName; 
String initials; 

public Person(){ 
    firstName = null; 
    lastName = null; 
    initials = null; 
}; 

public void setName(String FirstName, String LastName){ 
    firstName = FirstName; 
    lastName = LastName; 
    setInitials(FirstName, LastName); 
} 

private void setInitials(String firstName, String lastName) { 
    String firstLetter = firstName.substring(0,1); 
    String lastLetter = lastName.substring(0,1); 
    initials = firstLetter.concat(lastLetter); 
} 

} 

답변

0

주요 문제는 BaseAdapter가 데이터로 채워지지 않는다는 것입니다. 빈 데이터에 액세스하고 있습니다. 이것은 BaseAdapter를 제대로 사용하지 않기 때문입니다.

먼저 개인 데이터 모음을 정의합니다. 확장 된 BaseAdapter가 사용하는

public class MyBaseAdapter extends BaseAdapter { 

    ArrayList<Person> mPersonList = new ArrayList()<>; 
    LayoutInflater inflater; 
    Context context; 


    public MyBaseAdapter(Context context, ArrayList<Person> myList) { 
     this.myList = myList; 
     this.context = context; 
     inflater = LayoutInflater.from(this.context); 
    } 

    @Override 
    public int getCount() { 
     return myList.size(); 
    } 

    @Override 
    public ListData getItem(int position) { 
     return myList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     MyViewHolder mViewHolder; 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.layout_list_item, parent, false); 
      mViewHolder = new MyViewHolder(convertView); 
      convertView.setTag(mViewHolder); 
     } else { 
      mViewHolder = (MyViewHolder) convertView.getTag(); 
     } 

    // get data in position 
     ListData currentListData = getItem(position); 

    // set data to view 
     mViewHolder.edtFirstName.setText(currentListData.getFirstName()); 
    // add your other data to other view. 

     return convertView; 
    } 

    private class MyViewHolder { 
     EditText edtFirstName; 
    // Add the other view here. 

     public MyViewHolder(View item) { 
      edtFirstName = (EditText) item.findViewById(R.id.FirstName); 
     // add the other view here. 
     } 
    } 
} 

수정하면 Person 클래스를 세터와 게터를 추가하여 :이 같은 BaseAdapter 확장하여

ArrayList<Person> mPersonList; 

그런 다음 어댑터를 만들 :이 배열을 사용하여 달성 할 수

당신의 BaseAdapter에 대한 PersonList에 대한
public class Person { 
    private String firstName; 
    private String lastName; 
    private String initials; 

    public Person(){ 
    this("", ""); 
    }; 

    public Person(String FirstName, String LastName){ 
    firstName = FirstName; 
    lastName = LastName; 
    setInitials(FirstName, LastName); 
    } 

    private void setInitials(String firstName, String lastName) { 
    String firstLetter = firstName.substring(0,1); 
    String lastLetter = lastName.substring(0,1); 
    initials = firstLetter.concat(lastLetter); 
    } 

    private void setFirstName(String firstName) { 
    this.firstName = firstName; 
    } 

    private String firstName() { 
    return this.firstName; 
    } 

    //ADD the other setter and getter like setFirstName and firstName method. 
} 

채우기 일부 데이터 :

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ... 
    ... 
    ArrayList<Person> mPersonList = new ArrayList<Person>(); 
    // this is a simple data 
    Person person1 = new Person("John", "Keley"); 
    Person person2 = new Person("Chris", "Maloy"); 
    Person person3 = new Person("Andery", "Dreyfus"); 
    Person person4 = new Person("Portgas", "D. Ace"); 
    Person person5 = new Person("Bankai", "Zankupatto"); 
    mPersonList.add(person1); 
    mPersonList.add(person2); 
    mPersonList.add(person3); 
    mPersonList.add(person4); 
    mPersonList.add(person5); 
} 

다음 사람 데이터를 추가 한 후 어댑터를 추가하십시오. // mPersonList 데이터가 추가 된 후이 값이 아래에 있어야합니다. listView.setAdapter (새 MyBaseAdapter (this, mPersonList)); ListView using BaseAdapter - Android

:

...

완전한 튜토리얼 당신은 볼 수 있습니다

관련 문제